Backward Digit Sums
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5495   Accepted: 3184

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

Input

Line 1: Two space-separated integers: N and the final sum.

Output

Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

Sample Input

4 16

Sample Output

3 1 2 4

Hint

Explanation of the sample:

There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.

Source

白书里面的例题,虽然要求搜索,但是next_permutation()都可以过。。
题意:给定数字和,求原来的序列
3   1   2   4

4 3 6

7 9

16 给16,求3 1 2 4
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int n,a[],sum,b[][];
bool judge()
{
int i;
for(i=;i<n;i++)
b[][i]=a[i];
int k=n-,c=;
while(k--)
{
c++;
for(i=;i<k+;i++)
b[c][i]=b[c-][i]+b[c-][i+];
}
//cout<<b[c][0]<<endl;;
return b[c][]==sum;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&sum)!=EOF)
{
for(i=;i<n;i++)
a[i]=i+;
do{
if(judge())
break;
}while(next_permutation(a,a+n));
for(i=;i<n;i++)
printf("%d%c",a[i],i==(n-)?'\n':' ');
}
}

Backward Digit Sums(POJ 3187)的更多相关文章

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

    -->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...

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

  3. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  4. 【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums(暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1653 看了题解才会的..T_T 我们直接枚举每一种情况(这里用next_permutation,全排 ...

  5. POJ 3187 Backward Digit Sums 枚举水~

    POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

  6. poj--3187--Backward Digit Sums(dfs)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5667   Accepted: 32 ...

  7. 1653: [Usaco2006 Feb]Backward Digit Sums

    1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 285  Solved:  ...

  8. P1118 [USACO06FEB]Backward Digit Sums G/S

    P1118 [USACO06FEB]Backward Digit Sums G/S 题解:  (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N.  对每个 ...

  9. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

    1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved:  ...

随机推荐

  1. HTML中select标签单选多选详解

    select 元素可创建单选或多选菜单.当提交表单时,浏览器会提交选定的项目,或者收集用逗号分隔的多个选项,将其合成一个单独的参数列表,并且在将 <select> 表单数据提交给服务器时包 ...

  2. DJANGO用户名认证一例

    现在实例了用户登陆,就自带的功能.. urls.py ~~~~~~~~~~ (r'^login/$', login), (r'^logout/$',logout,{'next_page':'/logi ...

  3. 《Programming WPF》翻译 第5章 3.命名属性

    原文:<Programming WPF>翻译 第5章 3.命名属性 通过把同样的内嵌样式提升到资源中(正如第一章介绍的),我们可以给它一个名字,以及按名字使用它在我们的Button实例上, ...

  4. yii基础知识-

    控制器 是 CController 或其子类的实例.它在当用户请求时由应用创建. 当一个控制器运行时,它执行所请求的动作,动作通常会引入所必要的模型并渲染相应的视图. 动作 的最简形式,就是一个名字以 ...

  5. hdu 1853 最小费用流好题 环的问题

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Tota ...

  6. 导入旧数据需要 使用date插件

    "@version" => "1", "@timestamp" => "2016-09-12T08:31:06.630 ...

  7. rsyslog 收集系统日志

    <pre name="code" class="html">nginx 服务器配置: jrhwpt01:/root# cat /etc/rsyslo ...

  8. Hdu2425-Hiking Trip(优先队列搜索)

    Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost ...

  9. C++小知识之sprintf用法

    sprintf   字串格式化命令,主要功能是把格式化的数据写入某个字符串中.sprintf 是个变参函数,使用时经常出问题,而且只要出问题通常就是能导致程序崩溃的内存访问错误,但好在由sprintf ...

  10. MediaInfo源代码分析 1:整体结构

    MediaInfo 用来分析视频和音频文件的编码和内容信息,是一款是自由软件 (免费使用.免费获得源代码).之前编程的时候,都是直接调用它提供的Dll,这次突然来了兴趣,想研究一下它内部究竟是怎么实现 ...