P1118 [USACO06FEB]数字三角形Backward Digit Su…
来源:互联网 发布:sql identity是什么 编辑:程序博客网 时间:2024/05/19 13:57
题目描述
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行,为字典序最小的那个答案。
当无解的时候,请什么也不输出。(好奇葩啊)
输入输出样例
4 16
3 1 2 4
说明
对于40%的数据,n≤7;
对于80%的数据,n≤10;
对于100%的数据,n≤12,sum≤12345。
对于字典序最小的 排列 有一个神奇的性质
对于这个排列 A1 A2 ....An
我们可以发现 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…
- |洛谷|DFS|P1118 [USACO06FEB]数字三角形Backward Digit Su…
- 洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…
- 洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…
- 洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…
- P1118 [USACO06FEB]数字三角形Backward Digit Su…
- 洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…
- 洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…
- P1118 [USACO06FEB]数字三角形Backward Digit Su…
- [USACO06FEB]数字三角形Backward Digit Su…
- 杨辉三角 [USACO06FEB]数字三角形Backward Digit Su…
- 【搜索】洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Sums
- P1118 数字三角形游戏
- [P1118]数字三角形
- 洛谷P1118 数字三角形
- p1118 数字三角形
- P1118 数字三角形
- 洛谷P1118 数字三角形游戏
- 三种求乘法逆元方法详解
- P1463 [SDOI2005]反素数ant
- cocos2d Anroid 打包Apk
- 【NOIP2016】蚯蚓 --队列模拟
- 1624 取余最长路 (前缀和+set二分)
- P1118 [USACO06FEB]数字三角形Backward Digit Su…
- 1640 天气晴朗的魔法
- 51Nod 1378 夹克老爷的愤怒
- 1737 配对
- [SDOI2009]Elaxia的路线 SPFA+Topo
- 1090 3个数和为0
- BZOJ 1040: [ZJOI2008]骑士
- codevs 1245 最小的N个和
- 刘英俊筛法 简单易懂 远胜于线性筛法