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.

 
 
这个题竟然是深搜,看得题目中说用全排列字典序输出有点吓人...
我们先来看一下杨辉三角形的性质。第n行第m个数的值是C上m 下n-1 (组合数打不出来啊...),由于杨辉三角形的形成过程的逆过程。我们可以知道sum的最终和
sum=0 C n-1*ans[0]+1 C n-1*ans[1]+2 C n-1*ans[2]+3 C n-1*ans[3]+4 C n-1*ans[4]+......
好好理解这个逆过程因为杨辉三角形一开始都是用1加起来的,所以我们从底下往上加的时候,每个最底层的数在求和时算了多少次?那个次数就是底层数字对应杨辉三角形的值。
剩下的交给深搜了。

 #include <cstdio>
#include <cstring>
using namespace std;
int a[][],n,sum,vis[],ans[];
bool f;
void setnum ()//生成杨辉三角形
{
for (int i=;i<n;++i)
{
a[i][]=;
a[i][i]=;
}
for (int i=;i<n;++i)
for (int j=;j<i;++j)
a[i][j]=a[i-][j-]+a[i-][j];
}
void printAns ()
{
for (int i=;i<n;++i)
{
printf("%d",ans[i]);
if (i!=n-)
printf(" ");
}
printf("\n");
}
void dfs (int nowsum,int step)//这样的搜索是刚好字典序的
{
if (step==n)
{
if (nowsum==sum)
{
f=true;
printAns();
}
return ;
}
if (f||nowsum>sum)
return ;
for (int i=;i<=n;++i)
{
if (vis[i])
continue;
vis[i]=;
ans[step]=i;
dfs(nowsum+i*a[n-][step],step+);
vis[i]=;//每次搜完以后要清空状态
}
}
int main()
{
scanf("%d%d",&n,&sum);
setnum();
f=false;
memset(vis,,sizeof vis);
memset(ans,,sizeof ans);
dfs(,);
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(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

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

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

  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. Backward Digit Sums(POJ 3187)

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

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

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

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

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

  9. Backward Digit Sums(暴力)

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

随机推荐

  1. nil,Nil,NULL和NSNull的区别

  2. webbrowser控件显示word文档

    参照某网站上的步骤(http://www.kuqin.com/office/20070909/968.html)首先,在Visual Studio中创建一个C#语言的Windows应用程序,然后在左侧 ...

  3. angualr6 引入iframe

    项目开发中需要在angular项目中嵌入iframe窗口,上网搜索了相关文档,不是很多,但是总算是把功能实现了,现记录一下,便于后期查看: step1:在.html中放入需要承载内容的div,并定义好 ...

  4. Unity编程标准导引-3.1 Component 组件脚本及其基本生命周期

    本文为博主原创文章,欢迎转载,请保留出处:http://blog.csdn.net/andrewfan 3.1组件 Component 组件是Unity中最核心的一个概念,它是一切编程的基础.没有组件 ...

  5. 小程序 js 判断 字符串 为空 null

    判断字符串是否为空 1 2 3 4 5 var strings = ''; if (string.length == 0) { alert('不能为空'); } 判断字符串是否为“空”字符即用户输入了 ...

  6. 请教怎么查询ORACLE的历史操作记录!

    请问如何查询ORACLE的历史操作记录!!!!!我用的是linux oracle 11g r2,想查一下前几天的数据库的历史操作记录,例如对表的insert,delete,update等等的操作记录, ...

  7. git add 添加错文件的撤销方法

    git add 添加 多余文件 这样的错误是由于,有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤销操作 ...

  8. ceph-cluster map

    知道cluster topology,是因为这5种cluster map. ====================================== 知道cluster topology,是因为这 ...

  9. PAT 2019-3 7-4 Structure of a Binary Tree

    Description: Suppose that all the keys in a binary tree are distinct positive integers. Given the po ...

  10. 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)(A B G I L)

    A:签到题,正常模拟即可. #include<bits/stdc++.h> using namespace std; ; struct node{ int id, time; }; nod ...