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. php htmlentities()函数 语法

    php htmlentities()函数 语法 作用:把字符转换为 HTML 实体 语法:htmlentities(string,flags,character-set,double_encode) ...

  2. 设置element表格透明样式

    1.element table 表格 修改背景为透明并去除边框 .el-table{ /* 表格字体颜色 */ color:white; /* 表格边框颜色 */ /* border: 0.5px s ...

  3. 基础复习之HTML (doctype、标签语义化)

    这段时间找实习看的眼花缭乱的,然后也被拒得落花流水,啊哈哈-还是写博客好玩儿-嘿嘿,下面正题 一.doctype 标准模式 (Full Standards Mode) 接近标准模式 (Almost S ...

  4. WEB服务端安全---注入攻击

    注入攻击是web领域最为常见的攻击方式,其本质是把用户输入的数据当做代码执行,主要原因是违背了数据与代码分离原则,其发生的两个条件:用户可以控制数据输入:代码拼接了用户输入的数据,把数据当做代码执行了 ...

  5. ubuntu snap 包管理器

    基本使用 snap find $package snap install $package [--channel=beta] snap list snap refresh $package [--ch ...

  6. Cocos2d 之FlyBird开发---GameData类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 现在是大数据的时代,绝大多数的游戏也都离不开游戏数据的控制,简单的就是一般记录游戏的得分情况,高端大气上档次一点的就是记录和保存各方面的游 ...

  7. ESET激活码,可用。

    ESET Internet Security 12.1.31.0 Finalhttps://download.eset.com/com/eset/apps/home/eis/windows/v12/1 ...

  8. cmd中java的编译命令——java和javac、javap

    最近重新复习了一下java基础,这里便讲讲对于一个类文件如何编译.运行.反编译的.也让自己加深一下印象   如题,首先我们在桌面,开始->运行->键入cmd 回车,进入windows命令行 ...

  9. 【学习总结】Python-3-逻辑运算符

    参考:菜鸟教程-Python3运算符 逻辑运算符的计算规则划重点: 并不是只返回布尔型,有时会返回变量的数值 (优先级:not>and>or) 总结: '与或非'三件套中,not与数学逻辑 ...

  10. 试试监听输入框的值 (eq:在未输入前,按钮为灰色,输入内容后,按钮变蓝色)

    参考网址:https://blog.csdn.net/tel13259437538/article/details/78927071