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

Description

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.

Source

先把第一行每一个位置要加的次数求出来。会发现是一个杨辉三角,将这个杨辉三角打成表。每次枚举第一行的组成情况,直接用这个表计算结果。

#include <stdio.h>
#include <string.h>
#include <algorithm> int lev[12][12];
int box[12], N, S; int main() {
int i, j, sum;
lev[1][1] = 1;
for(i = 2; i <= 10; ++i)
for(j = 1; j <= i; ++j)
if(j == 1 || j == i) lev[i][j] = 1;
else lev[i][j] = lev[i-1][j] + lev[i-1][j-1]; while(scanf("%d%d", &N, &S) == 2) {
for(i = 1; i <= N; ++i)
box[i] = i;
do {
sum = 0;
for(i = 1; i <= N; ++i)
sum += box[i] * lev[N][i];
if(sum == S) break;
} while(std::next_permutation(box + 1, box + N + 1)); for(i = 1; i <= N; ++i)
printf("%d%c", box[i], i == N ? '\n' : ' ');
}
return 0;
}

POJ3187 Backward Digit Sums 【暴搜】的更多相关文章

  1. (DFS、全排列)POJ-3187 Backward Digit Sums

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

  2. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  3. POJ3187 Backward Digit Sums

    给出杨辉三角的顶点值,求底边各个数的值.直接DFS就好了 #include<iostream> #include<cstdio> #include<cstring> ...

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

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

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

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

  6. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

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

  7. Backward Digit Sums(POJ 3187)

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

  8. Backward Digit Sums(暴力)

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

  9. 1653: [Usaco2006 Feb]Backward Digit Sums

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

随机推荐

  1. Centos7安装配置tomcat 9并设置自动启动

    原文:https://blog.csdn.net/stinkstone/article/details/78082725 1.官网下载安装包 这里选择适合Linux的安装包,下载到本地后上传到cent ...

  2. 两步改动CentOS主机名称

    在CentOS系统中,改动主机名称的过程例如以下: 1. 改动network文件 编辑network文件.配置例如以下: vi /etc/sysconfig/network 配置 NETWORKING ...

  3. DCI:DCI学习总结

    备注 之前把DCI的Role和四色原型的Role给弄混了,本文也不会比较这两种Role的区别(后面有机会再说),这里简单的记录一下对DCI的理解. 参考文章:http://www.cnblogs.co ...

  4. FFmpeg YUV2RGB

    AVFrame* YUV2RGB( AVCodecContext * avctx, AVFrame * frame ) { AVFrame* pFrameRGB=NULL; pFrameRGB=avc ...

  5. TCP常用网络和木马使用端口对照表,常用和不常用端口一览表

    [开始-运行- CMD , 输入 netstat -an 然后回车就可以查看端口] 端口: 服务:Reserved 说明:通常用于分析操作系统.这一方法能够工作是因为在一些系统中“”是无效端口,当你试 ...

  6. [Todo] Java及C++ Exception整理

    今天接触到一些Java Exception方面的内容,整理如下: http://developer.51cto.com/art/201111/304649.htm C++中Exception等的整理

  7. 关于在Visual Studio 2008/2010 和Opencv平台下出现LINK : fatal error

    http://blog.sina.com.cn/s/blog_9015f3230101bbef.html 关于在Visual Studio 2008/2010 和Opencv平台下出现LINK : f ...

  8. 怎样获取shell函数的返回值及shell命令的返回值?

    1.获取shell函数调用的返回值: #!/bin/sh info() { cat jlb.sh } res=`info` echo "state: "$? echo " ...

  9. Array.prototype.slice.call 和 slice以及call

    单独的简单介绍,后续再补上一些资料. 对象转换为数组. /** * slice : 数组->slice(截取) * 参数有两个,开始截取和结束截取,并返回原数组: * a.slice(1) || ...

  10. 解决Android 5.1系统以上通知状态栏小图标仅仅显示白色问题

    看上图,想必大家都有遇到过吧.近期俺也遇到了,找到了解决方法,如今分享下也做个记录哈. 问题发生的规则是Android5.1或者以上的手机系统使用了非常多的颜色的通知图标,就会出现,怎么解决呢,非常e ...