Description

FJ and his cows enjoy playing a mental game. They write down the numbers from  to N ( <= N <= ) 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=) might go like this:              

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 : Two space-separated integers: N and the final sum.

Output

Line : An ordering of the integers ..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

 

Sample Output

   

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

 
注意杨辉三角的求法,要用二维来求,其它的就没什么了。。。
 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 16
int n,sum;
int vis[N];
int a[N][N];
int flag;
bool check(){
for(int i=;i<n;i++){
for(int j=;j<n-i;j++){
a[i][j]=a[i-][j]+a[i-][j+];
}
}
return a[n-][]==sum;
}
void dfs(int num){ if(num==n){
if(check())
flag=;
return;
}
for(int i=;i<=n;i++){
if(!vis[i]){
vis[i]=;
a[][num]=i;
dfs(num+);
if(flag==)
return;
vis[i]=;
}
}
}
int main()
{
while(scanf("%d%d",&n,&sum)==){ if(n==)
{
printf("%d\n",sum);
continue;
} memset(vis,,sizeof(vis));
flag=;
dfs();
for(int i=;i<n;i++){
printf("%d",a[][i]);
if(i!=n-)
printf(" ");
}
printf("\n");
}
return ;
}

poj 3187 Backward Digit Sums(穷竭搜索dfs)的更多相关文章

  1. POJ 3187 Backward Digit Sums 枚举水~

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

  2. 穷竭搜索:POJ 3187 Backward Digit Sums

    题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...

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

  4. POJ 3187 Backward Digit Sums

    暴力DFS+验证. 验证如果是暴力检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理. #include<cstdio> #include<cstring& ...

  5. POJ 3187 Backward Digit Sums (递推,bruteforce)

    第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...

  6. poj 2718 Smallest Difference(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  7. poj 2718 切数问题 穷竭搜索

    题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小 思路:暴力比较差最小值 stl中的next_permutation()函数进行排列  注意:这个函数必须从小到大才可以排序 ...

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

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

  9. Backward Digit Sums(POJ 3187)

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

随机推荐

  1. 【C#正则基础】正则表达式

    1. 代表任意多个字符:(.*?)2. 代表网页里的<body>*</body>任意的标签内容,替换以后网页源码就只剩纯文本:<[^>]*>3. 代表网页中的 ...

  2. String 和 InputStream 互转方式

    /** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @return String */ public ...

  3. mfc socket编程

    socket编程用法---- 随着计算机网络化的深入,计算机网络编程在程序设计的过程中变得日益重要.由于C++语言对底层操作的优越性,许多文章都曾经介绍过用VC++进行Socket编程的方法.但由于都 ...

  4. Jquery手机发送短信之后,进入倒计时状态

    在做手机网站开发的时候,难免发生意外.这时候,就是你展示人格魅力的时候啦! 下面是自己写的一个发送验证码给手机之后,进入的一个倒计时的效果 js代码,我可是连<script type=" ...

  5. csv 导入到 access中去

    Csv中有500万数据,导入到Access中去,每6万条数据为1Table 先是参照着http://support.microsoft.com/kb/257819/zh-cn来写 1.找不到可安装的  ...

  6. $http post传值的问题

    var app = angular.module("myApp", [], function ($httpProvider) { $httpProvider.defaults.he ...

  7. 解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移

    解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移 解决办法1:设置tabBarItem的imageInsets属性 代码示例: childContro ...

  8. JQuery结合Ajax实现双击Table表格,使Table变成可编辑,并保存到数据库中

    本文属于原创,转载请标明出处! 近期在做项目时,要实现通过双击Table表格的TR,使Table行变成可编辑,来实现修改数据并保存到数据库中的功能,无需多说,直接贴代码吧.希望能得到各位同仁指正. f ...

  9. struts2 JS获取上传文件的绝对路径,兼容IE和FF

    因为file控件上传失败后会自动清空,所以使用文本框来保存上传路径,而且在不同的浏览器下,控件的样式也需要兼容.下面是自己用到的实例 // 初始化判断浏览器的版本,根据版本的不同使用不同的样式func ...

  10. 生产环境 tomcat中启动缓慢

    具体的原因没研究,大概是一个随机数种子生成的速度拖慢了,直接copy一份解决方案,属于备忘材料 解决 有两种解决办法: 1)在Tomcat环境中解决 可以通过配置JRE使用非阻塞的Entropy So ...