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. 添加链接服务器 SQL SERVER

    使用sql语句: exec sp_addlinkedserver @server='serverontest',@provider='sqloledb',@srvproduct='',@datasrc ...

  2. 输入一个字符串,输出时数字倒序。例如:输入"hello2345wo7654",输出则为"hello5432wo4567"

    public class ReserveString { public static void main(String[] args) { System.out.println("Pleas ...

  3. HID Keyboard & Mouse descriptor.

    在USB中,USB HOST是通过各种描述符来识别设备的,有设备描述符,配置描述符,接口描述符,端点描述符,字符串描述符,报告描述符等等.USB报告描述符(Report Descriptor)是HID ...

  4. Linux命令 tar 打包解压缩

    --打包:tartar -cvf 命名文件.tar 要压缩的目录tar -xvf 要解压的包.tartar -tf  要查看的包.tartar -f 包.tar -r file             ...

  5. Tree( 树) 组件[2]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一. 异步加载如果想从数据库里获取导航内容, 那么就必须 ...

  6. centos上如何安装mysql

    centos可以使用yum安装mysql 但是版本很低,且不灵活. 本文将介绍如何使用安装包安装mysql http://dev.mysql.com/downloads/mysql/ 下载mysql ...

  7. JCo 指南

    http://blog.csdn.net/asdfak/article/details/5834731 JAVA 调用SAP端接口 Java Connector and BAPI 前些日子想去深入的研 ...

  8. iOS_SN_地图的使用(2)

    上一篇讲的是地图的基本使用,和注意事项,这一篇主要讲POI检索.百度地图SDK提供三种类型的POI检索:周边检索.区域检索和城市内检索.下面将以周边检索为例,向大家介绍如何使用检索服务. - (voi ...

  9. myeclipseb笔记(4):拷贝文件的相应配置

    在MyEclipse中,经常需要用到拷贝工程文件,但是直接拷贝的话,就会出现访问不了的情况,如下: 原文件learn/StudManage/login.jsp,访问: 拷贝工程,改名,访问: 就出现了 ...

  10. uva 12626 - I ❤ Pizza

    #include <iostream> #include <cstdio> #include <string> #include <algorithm> ...