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. ubuntu16.04 安装power shell

    ubuntu16.04 安装power shell # Download the Microsoft repository GPG keys wget -q https://packages.micr ...

  2. iOS学习-字符串的删除替换

    字符串的常用处理,删除,替换.记录一下,方便查找. -------------------------------------------------------------------------- ...

  3. thinkphp3.2笔记(1)目录,控制器及url模式,地址解析

    一.目录 Application  :  tp默认的应用代码存储的目录 Public :     Tp 默认的存储静态资源的目录,img,css,js ThinkPHP  :   Tp  框架的源代码 ...

  4. 获得Python脚本所在目录

    如何获得Python脚本所在目录的位置   On this page... (hide) 1.  以前的方法 2.  正确的方法 3.  实例说明   (Edit) 1.  以前的方法 如果是要获得程 ...

  5. VS2013 The Debugger Resource DLL is out of date

    折磨了我一天的问题,亲们,记住,下载vs2013 update5 ,别的方法不用试了,完全没用!

  6. 十九 Python分布式爬虫打造搜索引擎Scrapy精讲—css选择器

    css选择器 1. 2. 3.  ::attr()获取元素属性,css选择器 ::text获取标签文本 举例: extract_first('')获取过滤后的数据,返回字符串,有一个默认参数,也就是如 ...

  7. syslinux启动盘制作

    # <font color=DarkCyan >syslinux启动盘制作</font> # ### 准备工具 ### 1. BOOTICEx64 软件 ##分区引导制作工具 ...

  8. RedHat/CentOS 7通过nmcli命令管理网络教程

    Red Hat Enterprise Linux 7 和CentOS 7 的网络管理实际上是对NetworkManager的管理,可通过nmcli命令进行控制,下面小编就给大家介绍下RedHat/Ce ...

  9. iOS-如何写好一个UITableView

    如何写好一个UITableView 字数5787 阅读3745 评论25 喜欢69 本文是直播分享的简单文字整理,直播共分为上.下两部分.第一部分:优酷 Or YouTube,第二部分:优酷 Demo ...

  10. day29 主机管理-堡垒机2-原生ssh会话记录

    day29课堂代码:https://github.com/liyongsan/git_class/tree/master/day29 课堂笔记: 通过原生Ssh 记录会话1. 在我们自己的堡垒机交互脚 ...