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. (转)iOS5:[UIDevice uniqueIdentifier]的替代方案

    背景: 大多数应用都会用到苹果设备的UDID号,UDID通常有以下两种用途: 1)用于一些统计与分析目的:[第三方统计工具如友盟,广告商如ADMOB等] 2)将UDID作为用户ID来唯一识别用户,省去 ...

  2. 共享IP云主机(VPS)玩转wdcp

    目前国内有不少性能还不错的共享IP VPS,但因为没有独立IP,所以环境配置起来会比较麻烦. 因为本人自己现在用的就是共享IP的vps,所以把一些配置方法分享一下,供大家参考. 首先是系统的选择,根据 ...

  3. CRT远程连接server字符输出乱码解决一例

    CRT远程连接server字符输出乱码解决一例 环境: server:Centos 6.2 远端:win 7 CRT版本号:7.1.1 现象回想: 1.server端:中文字符显示正常,例如以下: 2 ...

  4. 简析MFC中CString用作C字符串

      MFC中CString是一个方便的字符串操作的类, 然而很多函数需要传递字符指针, 这就需要进行CString和普通字符串的转换. 1.CString用作C字符串常量. 直接使用强制类型转换即可, ...

  5. C++11 : 外部模板(Extern Template)

    在C++98/03语言标准中,对于源代码中出现的每一处模板实例化,编译器都需要去做实例化的工作:而在链接时,链接器还需要移除重复的实例化代码.显然,让编译器每次都去进行重复的实例化工作显然是不必要的, ...

  6. Java基础知识强化49:10个实用的但偏执的Java编程技术

    1. 将String字符串放在最前面 为了防止偶发性的NullPointerException 异常,我们通常将String放置在equals()函数的左边来实现字符串比较,如下代码: // Bad ...

  7. Word02-隐藏回车换行符

    Word文档中每次输入回车后,会显示一个换行标志符,影响页面显示效果. 如下图,换行符隐藏前后: 设置方法:(选项-->显示-->段落标记前面的√去掉即可)

  8. C# 无边框窗体移动代码

    C# 无边框窗体移动代码 Point _frmPoint = new Point(); //移动前窗体左上角坐标 Point _mousePoint = new Point(); //按下鼠标时坐标 ...

  9. [c#]asp.net开发微信公众平台(7)前6篇的整体框架demo源码

    这里给出的demo是具备整体框架的微信公众平台源码, 所谓demo就是拿过去就可以直接演示使用的东西,  当然不会具备非常详细的具体到业务层面.数据层面的东西, 每个人都可以在此基础上自由发挥,  只 ...

  10. angular ng-bind-html 对src路径失效 解决方案

    json内容 ;<img src="/newsfile/1506271512489.jpg" width="600" height="450&q ...