1118 Backward Digit Sums(数字三角形)

难度:普及/提高-

题目类型:DFS

提交次数:1

涉及知识: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. 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 #include<iostream>
 2 using namespace std;
 3 int n, sum;
 4 int a[13][13];
 5 void yanghui(){
 6     int i, j;
 7     a[0][1] = 1;
 8     a[1][1] = 1;
 9     a[1][2] = 1;
10     for(i = 2; i < n; i++)
11         for(j = 1; j <= n; j++)
12             a[i][j] = a[i-1][j]+a[i-1][j-1];
13
14 }
15 bool flag = 0;
16 int ans[13];
17 bool visited[13];
18 int summ;
19 void dfs(int step){
20     if(step == n+1&&summ==sum){
21         flag = 1;
22         for(int i = 1; i <= n; i++)
23             cout<<ans[i]<<" ";
24         return;
25     }
26     if(summ>=sum) return;
27     if(!flag){
28         for(int i = 1; i <= n; i++){
29             if(!visited[i]){
30                 visited[i] = true;
31                 ans[step] = i;
32                 summ += i*a[n-1][step];
33                 dfs(step+1);
34                 summ -= i*a[n-1][step];
35                 visited[i] = false;
36                 ans[step] = 0;
37             }
38         }
39     }
40
41 }
42 int main(){
43     cin>>n>>sum;
44     yanghui();
45     /*for(int i = 0; i < n; i++){
46         for(int j = 1; j <= i+1; j++)
47             cout<<a[i][j]<<" ";
48         cout<<endl;
49     }*/
50     dfs(1);
51     return 0;
52 }

备注:

感觉这道题也很巧妙~

不管是令人拍案叫绝的递推式,还是从题目抽丝剥茧看到本质,大概都是算法艺术的魅力的冰山一角。

题解一直在说杨辉三角,可愚钝如我并不知道这有什么联系。直到百度到了一个分析才开窍……杨辉三角就是每个数字加的次数。杨辉三角预处理在a数组中,当step == n+1并且每个数字乘上权值(呈杨辉三角第n-1层分布)等于sum,就可以输出答案了。这时程序的使命已经结束,递归应层层跳出,所以设置了一个flag标记。

另外有一个小小的减枝。

弄明白这道题的关键是杨辉三角之后,程序就独立写出来啦,提交一次就过了。

时间: 2024-10-23 06:13:34

1118 Backward Digit Sums(数字三角形)的相关文章

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

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

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

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

Backward Digit Sums

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)

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