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. 【layer】关于layer打开就是最大化的使用

    使用layer时候 想在弹出层 在打开的时候默认就是最大值 perContent = layer.open({ type:2, title: userName+nowDate+"的" ...

  2. Java:初学 Java,遇到的问题及解决方案小结。

    背景 失败是成功之母,前提是没有被遗忘的失败,本文记录这几天学习 Java,自己遇到的几个问题和解决方案,希望能达到强化记忆的作用. 开发环境是:Eclipse + Tomcat7.0. 第一个:依赖 ...

  3. Flex入门(一)——基本理论

    Flash作为一款美丽动画的产品,是大家所喜爱的,他能够给我们带来更好的视觉效果.可是怎样使其非常快非常好的让Java程序猿,.net程序猿等所接受,并进行相关开发.Macromedia公司在2004 ...

  4. oracle sql 优化大全

    转自: http://panshaobinsb.iteye.com/blog/1718233 http://yulimeander.blog.sohu.com/115850824.html 最近遇到了 ...

  5. ecside 列表排序问题

    ecside列表首先点击某一列排序,没有问题,再点第二列的时候没有起作用.原因是第二次排序的时候还包含着第一次排序的字段.所以排序始终是第一次的排序结果. 修改ecside.js 的ECSideUti ...

  6. 关于FFmpegInterop项目的编译

    如果是从git 上下载的最新版本 FFmpeg,那么打开VS2015编译时会报出一个错误    C4996    'av_free_packet': 被声明为已否决    FFmpegInterop  ...

  7. iOS: iOS各种设备信息获取

    Author:si1ence Link:http://www.jianshu.com/p/b23016bb97af 为了统计用户信息.下发广告,服务器端往往需要手机用户设备及app的各种信息,下面讲述 ...

  8. JS 与Flex交互:html中的js 与flex中的actionScript通信

    Flex与JavaScript交互的问题,这里和大家分享一下,主要包括Flex调用JavaScript中的函数和JavaScript调用Flex中的函数两大部分内容. Flex 与JavaScript ...

  9. Centos7 安装pycharm

    可以通过浏览器访问http://www.jetbrains.com/pycharm/,选择需要下载的版本,也可以通过wget下载可执行程序. [root@localhost ~] wget https ...

  10. RS报表自动适应屏幕分辨率大小

    问题:同一个报表,由于用户电脑显示器大小,分辨率大小不同显示的不一样,看起来不完整或者很不协调 原因:设计报表大小属性的时候使用了固定值属性,比如限制为宽:1200px 那么在电脑屏幕小分辨率很小的情 ...