P1118 [USACO06FEB]数字三角形Backward Digit Su…

题目描述

FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:

    3   1   2   4
4   3   6
7   9
16

Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.

Write a program to help FJ play the game and keep up with the cows.

有这么一个游戏:

写出一个1~N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置。下面是一个例子:

3 1 2 4

4 3 6

7 9 16 最后得到16这样一个数字。

现在想要倒着玩这样一个游戏,如果知道N,知道最后得到的数字的大小sum,请你求出最初序列a[i],为1~N的一个排列。若答案有多种可能,则输出字典序最小的那一个。

[color=red]管理员注:本题描述有误,这里字典序指的是1,2,3,4,5,6,7,8,9,10,11,12

而不是1,10,11,12,2,3,4,5,6,7,8,9[/color]

输入输出格式

输入格式:

两个正整数n,sum。

输出格式:

输出包括1行,为字典序最小的那个答案。

当无解的时候,请什么也不输出。(好奇葩啊)

输入输出样例

输入样例#1:

4 16

输出样例#1:

3 1 2 4

说明

对于40%的数据,n≤7;

对于80%的数据,n≤10;

对于100%的数据,n≤12,sum≤12345。

对于字典序最小的 排列 有一个神奇的性质

对于这个排列 AA2 ....A

我们可以发现 A1*C(n-1,0)+A2*C(n-1,1)+....+An*C(n-1,n-1)==sum // sum即为题目中给的和

看样例 3*1+1*3+2*3+1*4==16

数据n<=12  所以我们可以DFS枚举全排列 进行判断

当然 还要加个剪枝

 1 #include <cctype>
 2 #include <cstdio>
 3 #include <cstdlib>
 4
 5 const int MAXN=110;
 6
 7 int n,sum;
 8
 9 int C[MAXN][MAXN],ans[MAXN];
10
11 bool vis[MAXN];
12
13 inline void read(int&x) {
14     int f=1;register char c=getchar();
15     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
16     for(;isdigit(c);x=x*10+c-48,c=getchar());
17     x=x*f;
18 }
19
20 inline void DFS(int num,int tot) {
21     if(tot>sum) return;
22     if(num>n) {
23         if(sum==tot) {
24             for(int i=1;i<=n;++i) printf("%d ",ans[i]);
25             exit(0);
26         }
27     }
28     for(int i=1;i<=n;++i) {
29         if(!vis[i]) {
30             vis[i]=true;
31             ans[num]=i;
32             DFS(num+1,tot+C[n][num]*i);
33             vis[i]=false;
34         }
35     }
36
37 }
38
39 int hh() {
40     read(n);read(sum);
41     C[0][0]=1;
42     for(int i=1;i<=n;++i)
43       for(int j=1;j<=i;++j)
44         C[i][j]=C[i-1][j-1]+C[i-1][j];
45     DFS(1,0);
46     return 0;
47 }
48
49 int sb=hh();
50 int main(int argc,char**argv) {;}

代码

P1118 [USACO06FEB]数字三角形Backward Digit Su…相关推荐

  1. 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…

    https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...

  2. 洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...

  3. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \l ...

  4. P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法

    有这么一个游戏: 写出一个11至NN的排列a_iai​,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一 ...

  5. 洛谷P1118 [USACO06FEB]数字三角形Backward Digit Su…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...

  6. 洛谷P1118 [USACO06FEB]数字三角形 Backward Digit Su(dfs剪枝)

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \l ...

  7. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…(C++_DFS)

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \l ...

  8. 杨辉三角 [USACO06FEB]数字三角形Backward Digit Su…

    杨辉三角 简单的说一下就是两个未知数和的幂次方运算后的系数问题,比如(x+y)的平方=x的平方+2xy+y的平方,这样系数就是1,2,1这就是杨辉三角的其中一行,立方,四次方,运算的结果看看各项的系数 ...

  9. [USACO06FEB]数字三角形Backward Digit Su…

    题目描述 3 1 2 44 3 67 916 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少 ...

最新文章

  1. python数学公式代码导入_NumPy 数学函数及代数运算的实现代码
  2. python - 定时拍照并发送到qq
  3. 哪款浏览器好用_碉堡了!火狐浏览器发布重大更新,谷歌Chrome请hold住!
  4. Forbidden You don't have permission to access / on this server PHP
  5. java 并发框架源码_某网Java并发编程高阶技术-高性能并发框架源码解析与实战(云盘下载)...
  6. 快手直播伴侣因计算机丢失,在电脑上安装快手直播伴侣,却说因为计算机丢失MSVCP140.DOLL怎么办???...
  7. C++: find()函数的注意事项
  8. C语言的本质(7)——C语言运算符大全
  9. linux测试libreoffice,LibreOffice 7.0 开放测试,最终版本将于8月初发布
  10. c语言题库及详解答案 免费下载,C语言题库(带详解答案).pdf
  11. 【微信小程序】微信Web开发者工具的部分界面功能
  12. scrapy爬取阳光热线问政平台
  13. 《RO 仙境传说》NFT 推出
  14. lowess和loess方法
  15. 如何在PPT中嵌入交互式图表?LightningChart助力炫酷展示
  16. IE hasLayout详解
  17. ai字母组合发音规律_使用和尚ai的俄语字母分类
  18. MyEclipse6.5下载地址(含注册码)
  19. 国泰煤炭运销管理系统
  20. linux降低磁盘的读,Linux磁盘I/O优化:vm.dirty_ratio - 米扑博客

热门文章

  1. 声纹识别X-Vector
  2. 分享一个PC端一键分享功能
  3. 友善之臂编linux内核,友善之臂mini 6410 n43平台的内核编译
  4. 图腾42u服务器机柜型号,图腾42u机柜一般多少斤
  5. 华为鸿蒙a股,来了!华为“鸿蒙”,这一次它会给A股带来哪些惊喜
  6. commit时报错missing Change-Id in message footer
  7. ArcGIS中的坡度分析问题的解决
  8. 淘宝卖家缺货退款的常见问题解答
  9. [CIO] 12个顶级项目管理证书——2018
  10. 安装虚拟机,”intel vt-x 处于禁用状态“ 如何开启