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

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) {;}

代码

时间: 2024-08-12 08:30:57

P1118 [USACO06FEB]数字三角形Backward Digit Su…的相关文章

做题记录: P1118 [USACO06FEB]数字三角形Backward Digit Su…

P1118 [USACO06FEB]数字三角形Backward Digit Su- /*思路:设一开始的n个数为a1.a2.a3...an, 一步一步合并就可以用a1..an表示出最后剩下来 的数,不难发现其中a1..an的系数恰好就是第n层 杨辉三角中的数.所以我们可以先处理出第n层杨 辉三角中的数,然后根据这一层中的数搜索即可.*/ #include<iostream> #include<cstdio> #include<fstream> #include<a

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

题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \le 10)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 num

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

有这么一个游戏: 写出一个11至NN的排列a_iai?,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一个例子: 3,1,2,43,1,2,4 4,3,64,3,6 7,97,9 1616 最后得到1616这样一个数字. 现在想要倒着玩这样一个游戏,如果知道NN,知道最后得到的数字的大小sumsum,请你求出最初序列a_iai?,为11至NN的一个排列.若答案有多种可能,则输出字典序最小的那一个. [

洛谷P1118 [USACO06FEB]数字三角形 搜索

洛谷P1118 [USACO06FEB]数字三角形Backward Digit Su-     搜索 这题我们发现每一个位置的加权就是 杨辉三角 yh[ n ][ i ] 然后我们就可以求 n! 暴力 ,但是会 TLE 额 好像是会T 因为12! 已经 4亿了然后我们加一个强力剪枝 如果当前求出来的 s 已经大于 sum了,因为没有负的加权,也就是说这一路是没有用了的,在继续搜下去也不能更新答案了,那么就直接退出 . 1 #include <cstdio> 2 #include <cma

[luogu p1118] [USACO06FEB]数字三角形

题面 题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from \(1\) to$ N(1 \le N \le 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 numb

[USACO06FEB]数字三角形

题目描述 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 lef

洛谷P1118 数字三角形游戏

洛谷1118 数字三角形游戏 题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置.下面是一个例子:     3   1   2   4       4   3   6         7   9          16 最后得到16这样一个数字. 现在想要倒着玩这样一个游戏,如果知道N,知道最后得到的数字的大小sum,请你求出最初序列a[i],为1-N的一个

BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved: 161[Submit][Status][Discuss] Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a cer

【POJ - 3187】Backward Digit Sums(搜索)

-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然后把相邻的数相加的到新的一行数.重复这一操作直至只剩一个数字.比如下面是N=4时的一种例子 3 1 2 4 4 3 6 7 9 16 在FJ回来之前,奶牛们开始了一个更难的游戏:他们尝试根据最后结果找到开始的序列.这已超过了FJ的思考极限. 写一个程序来帮助FJ吧 Input N和最后的和 Output 满足