(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<stdio.h>
 2 #include<cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 int fact[11],ncr[11][11],b[10]={1,2,3,4,5,6,7,8,9,10};//先将1——n准备好
 8 int main()
 9 {
10     int i,j;
11     fact[0]=fact[1]=1;//由于数据小,先预处理,计算好阶乘
12     for(i=2;i<=10;i++)
13     {
14         fact[i]=fact[i-1]*i;
15     }
16     for(i=1;i<=10;i++)
17     {
18         for(j=0;j<=i/2;j++)
19         {
20             ncr[i][j]=ncr[i][i-j]=(fact[i]/(fact[j]*fact[i-j]));//计算各个组合数
21         }
22     }
23     int n,he,de;
24     scanf("%d%d",&n,&he);
25     n--;
26     if(n==0)
27         printf("1\n");
28     else{
29             de=0;
30         for(i=0;i<=n;i++)//先判断一下初始递增的情况是否就满足,后面调用next_permutation貌似就直接从第二项开始了
31         {
32             de+=b[i]*ncr[n][i];
33         }
34         if(de==he)
35             {
36                 for(i=0;i<=n;i++)
37                 {
38                     printf("%d ",b[i]);
39                 }
40                 return 0;
41             }
42     while(next_permutation(b,b+n+1))//全排列
43     {
44         de=0;
45         for(i=0;i<=n;i++)
46         {
47             de+=b[i]*ncr[n][i];
48         }
49         if(de==he)
50             {
51                 for(i=0;i<=n;i++)
52                 {
53                     printf("%d ",b[i]);
54                 }
55                 break;
56             }
57     }
58     printf("\n");
59     }
60     return 0;
61 }
时间: 2024-10-06 22:34:29

(DFS、全排列)POJ-3187 Backward Digit Sums的相关文章

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

POJ 3187 Backward Digit Sums 题解 《挑战程序设计竞赛》

题目:POJ 3187 思路: 这道题很简单,用next_permutation枚举1~N的所有排列,然后依次相加,判断最后的和是否等于sum,是的话则break,即为字典序最前的. 1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int n; 7 int sum; 8 int mycase[11][11]; 9 10 int main() { 11 cin >> n &

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)

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. Fo

poj 3187 Backward Digit Sums(穷竭搜索dfs)

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

POJ 3187 Backward Digit Sums

http://poj.org/problem?id=3187 穷竭搜索 全排列 然后按规则求和 排列之前先按升序排序 这样可以保证第一个和为k的就是符合最小序列的结果 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int sum(int a[], int n) 8 { 9 int s[11]; 10 for (int i = 0

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

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

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