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个数1-n的一个序列按题目要求合并到最后要等于给出的值,dfs一下,至于每个数加了多少次,用了杨辉三角的知识,记得高中做过杨辉三角的题,第n行第i个元素为c(n)(i),这里了是三角形倒过来了,仍然适用。
 
代码:
 
#include <iostream>
#include <string> using namespace std;
int n,d,vis[],num[],flag = ;
int csum();
void print();
void dfs(int k)
{
if(flag)return;
if(k > n)
{
// cout<<csum()<<endl;
if(csum() == d)
{
print();
flag = ;
return;
}
}
for(int i = ;i <= n;i ++)
{
if(vis[i] == )
{
vis[i] = ;
num[k] = i;
dfs(k + );
vis[i] = ;
}
}
}
void print()
{
cout<<num[];
for(int i = ;i <= n;i ++)
cout<<' '<<num[i];
}
int counti(int k)
{
int c = ;
for(int i = ;i < k;i ++)
c *= (n - - i);
for(int i = ;i < k;i ++)
c /= (i + );
return c;
}
int csum()
{
int sum = ;
for(int i = ;i <= n;i ++)
{
sum += num[i] * counti(i - );
}
return sum;
}
int main()
{
cin>>n>>d;
dfs();
}

Backward Digit Sums的更多相关文章

  1. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

    1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved:  ...

  2. Backward Digit Sums(POJ 3187)

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

  3. Backward Digit Sums(暴力)

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

  4. 1653: [Usaco2006 Feb]Backward Digit Sums

    1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 285  Solved:  ...

  5. POJ3187 Backward Digit Sums 【暴搜】

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 25 ...

  6. POJ 3187 Backward Digit Sums 枚举水~

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

  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. (DFS、全排列)POJ-3187 Backward Digit Sums

    题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难 ...

  10. Enum:Backward Digit Sums(POJ 3187)

    反过来推 题目大意:就是农夫和这只牛又杠上了(怎么老是牛啊,能换点花样吗),给出一行数(从1到N),按杨辉三角的形式叠加到最后,可以得到一个数,现在反过来问你,如果我给你这个数,你找出一开始的序列(可 ...

随机推荐

  1. Android 数据库 ObjectBox 源码解析

    一.ObjectBox 是什么? greenrobot 团队(现有 EventBus.greenDAO 等开源产品)推出的又一数据库开源产品,主打移动设备.支持跨平台,最大的优点是速度快.操作简洁,目 ...

  2. [Vue]vue中各选项及钩子函数执行顺序

    在vue中,实例选项和钩子函数和{{}}表达式都是不需要手动调用就可以直接执行的. 一.生命周期图示 二.vue中各选项及钩子函数执行顺序 1.在页面首次加载执行顺序有如下: beforeCreate ...

  3. windows系统下,安装多个版本的jdk,java -version

    问题描述: 开始安装了 jdk8 后来装了jdk9,可以为项目配置不同的jdk,相安无事: 今天发现软件需要jdk8的环境,结果我的java -version始终是jdk9.0.1: 解决办法:使ja ...

  4. vs2012 在调试或运行的过程中不能加断点

    在使用VS2012 的过程中,突然发现在调试的过程中,不能加断点,显示断点未能绑定.在搜寻了很多解决方案后未能解决,3.23这一天,重装了VS也没有用. 便想着把网上所有的方法都试个遍也要解决这个问题 ...

  5. .net core mvc部署到IIS导出Word 提示80070005拒绝访问

    项目中相信大家经常会遇到导出Word.Excel等需求,在实际开发环境中,一般不会出现什么问题,但当发布到IIS上后可能会遇到各种各样的问题,一般都是权限的问题.前几天把公司项目发布后,出现Word导 ...

  6. 设计模式--责任链模式C++实现

    责任链模式C++实现 1定义 使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系.将这些对象链成一条链,并沿着这条链传递该请求/命令,直到有对象处理它为止 注:这里的请求.命令正 ...

  7. 解决Mybatis配置ORM映射 时分秒都为0

    方法一: Date类型的类成员变量使用java.sql.Timestamp 方法二: Mybatis的映射配置javatype=Timestamp.class

  8. mbstring.so下载安装

    linux下安装: $:cd /php7.0/ext/mbstring 切换到源码包目录下 $:/usr/local/php/bin/phpize 执行这句 $:./configure –with-p ...

  9. 【Windows】Python脚本随机启动

    Python脚本的管理在linux系统上市非常方便的,在windows则不是很方面.但是由于之前对于Windows这块的内容不是很了解,其实计划任务也是不错的,但和linux相比起来还是欠缺了那么点. ...

  10. Anaconda Install

    Linux 安装 首先下载Anaconda Linux安装包,然后打开终端输入: bash ~/Downloads/Anaconda3-2.4.0-Linux-x86_64.sh 注意:如果你接受默认 ...