【穷竭】POJ3187-Backward Digit Sums

【思路】

利用杨辉三角形,每一个数字被加的次数等于它在杨辉三角形中对应的那个数字。注意这道题的意思是,最底层是N的全排序,而不是指1..10都可以。生成杨辉三角形的时候第一次我用了二重循环模拟生成,后来学习到,杨辉三角形中,第n行第k个数字为Cnk。不过在第二个程序中我的杨辉三角形没有预处理,导致了很多时间的浪费。用了深搜和STL两种方法。深搜因为能够剪枝所以明显要比Next_permuation快。

 1 /*232K    0MS*/
 2 /*模拟+深搜*/
 3 #include<iostream>
 4 #include<cstdio>
 5 using namespace std;
 6 const int MAXN=10+5;
 7 int n,sum;
 8 int a[MAXN][MAXN];
 9 int ans[MAXN];
10 int vis[MAXN];
11 int f;
12
13 void gettri()
14 {
15     for (int i=0;i<n;i++)
16     {
17         a[i][0]=1;
18         a[i][i]=1;
19     }
20     for (int i=2;i<n;i++)
21         for (int j=1;j<i;j++)
22             a[i][j]=a[i-1][j-1]+a[i-1][j];
23 }
24
25 void print()
26 {
27     for (int i=0;i<n;i++) cout<<ans[i]<<‘ ‘;
28     cout<<endl;
29 }
30
31 void getnum(int step,int nowsum)
32 {
33     if (step==n)
34     {
35         if (nowsum==sum)
36         {
37             f=1;
38             printf();
39         }
40         return;
41     }
42     if (f||nowsum>sum) return;
43     for (int i=1;i<=n;i++)
44     {
45         if (vis[i]==1) continue;
46         vis[i]=1;
47         ans[step]=i;
48         getnum(step+1,nowsum+i*a[n-1][step]);
49         vis[i]=0;
50     }
51 }
52
53 int main()
54 {
55     scanf("%d%d",&n,&sum);
56     gettri();
57
58     memset(vis,0,sizeof(vis));
59     f=0;
60     getnum(0,0);
61     return 0;
62 }
 1 /*232K    469MS*/
 2 /*组合+STL*/
 3 #include<iostream>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 const int MAXN=10+3;
 9 int n,sum;
10 int ans[MAXN];
11
12 int c(int n,int k)
13 {
14     int cresult=1;
15     for (int i=0;i<k;i++) cresult=cresult*(n-i)/(i+1);
16     //这里不能写成cresult=cresult*(n-i)/(k-i),因为如果从大到小可能无法整除,精确度会导致错误
17     return cresult;
18 }
19
20 int main()
21 {
22     scanf("%d%d",&n,&sum);
23     for (int i=0;i<n;i++) ans[i]=i+1;
24     do
25     {
26         int result=0;
27         for (int i=0;i<n;i++) result+=ans[i]*c(n-1,i);
28         if (result==sum)
29         {
30             for (int i=0;i<n;i++) cout<<ans[i]<<‘ ‘;
31             cout<<endl;
32             break;
33         }
34     }while (next_permutation(ans,ans+n));
35     return 0;
36 }
时间: 2024-10-29 05:13:58

【穷竭】POJ3187-Backward Digit Sums的相关文章

POJ3187 Backward Digit Sums 【暴搜】

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 2575 Description 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 ad

(DFS、全排列)POJ-3187 Backward Digit Sums

题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难推得第一行为n个数a1\a2\--\an时求得的和为i=0∑n-1 ai*(n-1Ci) 根据此公式,考虑到数据量比较小,只需要将原本按递增顺序依次排列好的1--n按next_permutation给出的递增全排列顺序逐个代入,如果结果与m相等就停止循环即可. 参考代码: 1 #include<st

POJ3187 Backward Digit Sums

给出杨辉三角的顶点值,求底边各个数的值.直接DFS就好了 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cmath&g

【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 满足

bzoj1653:Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 318  Solved: 239[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

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(next_permutation)

Description 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

Backward Digit Sums(暴力)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 3280 Description 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 ad

POJ 题目Backward Digit Sums(next_permutation)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4807   Accepted: 2772 Description 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 ad