Backward Digit Sums
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6350   Accepted: 3673

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


全排列中的数字对sum贡献的次数就是杨辉三角第n-1列
杨辉三角是从0开始
组合数公式递推
dfs+剪枝
//
// main.cpp
// poj3187
//
// Created by Candy on 9/12/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
using namespace std;
const int N=;
int n,sum,c[N],vis[N],ans[N],flag=;
void dfs(int d,int now){
if(flag) return;
if(d==n&&now==sum) {flag=;for(int i=;i<n;i++) printf("%d ",ans[i]);return;}
if(d==n) return;
for(int i=;i<=n;i++){
if(vis[i]) continue;
if(now+i*c[d]<=sum){
ans[d]=i; vis[i]=;
dfs(d+,now+i*c[d]);
vis[i]=;
}
}
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&n,&sum);
n--;
c[]=;
for(int i=;i<=n;i++) c[i]=c[i-]*(n-i+)/i;
n++;
dfs(,);
return ;
}

POJ3187Backward Digit Sums[杨辉三角]的更多相关文章

  1. POJ2167Irrelevant Elements[唯一分解定理 组合数 杨辉三角]

    Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2407   Accepted: 59 ...

  2. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. poj3187-Backward Digit Sums(枚举全排列)

    一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角)  3 1 2 4 //1~n 全排列中的一个排列  4 3 6  7 ...

  5. python生成器实现杨辉三角

    def triangels(): """ 杨辉三角 """ lst = [1] n_count = 2 # 下一行列表长度 while Tr ...

  6. python 生成器生成杨辉三角

    用Python写趣味程序感觉屌屌的,停不下来 #生成器生成展示杨辉三角 #原理是在一个2维数组里展示杨辉三角,空的地方用0,输出时,转化为' ' def yang(line): n,leng=0,2* ...

  7. HDNOIP201405杨辉三角

    2016.1.27 试题描述 杨辉三角是形如如下的数字三角形: 1 1    1 1   2    1 …… 现在想求出杨辉三角第N行的N个数中,有多少个数能被给定的质数p整除. 输入 一行两个空格隔 ...

  8. Java的二维数组的应用及杨辉三角的编写

    (1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...

  9. 杨辉三角用java实现

    代码如下: public class ErArray { public static void main(String[] args) { //杨辉三角 int[][] num = new int[1 ...

随机推荐

  1. 实例之JavaScript

    使用JavaScript实现5秒倒计时 <html> <head> <meta charset="utf-8"> <title>&l ...

  2. sharepoint2010问卷调查(1)-实现问卷的图片调查(采用JS实现)

      1. 首先建立个图片库上传图片 2. 采用:评估范围(一系列选项或 Likert 范围)制作,如下图: http://win-i07fillcfom:8003/DocLib2/1.jpg http ...

  3. 为ListView组件加上快速滑块以及修改快速滑块图像

    本文转载自:http://blog.csdn.net/ouyang_peng/article/details/46919723 作者:欧阳鹏  欢迎转载,与人分享是进步的源泉! 转载请保留原文地址:h ...

  4. System.currentTimeMillis()与SystemClock.uptimeMillis()

    1.System.currentTimeMillis()获取的是系统的时间,可以使用SystemClock.setCurrentTimeMillis(long millis)进行设置.如果使用Syst ...

  5. Swift使用注意

    二.函数的可选参数 参数名:参数类型? = 默认值 // 调用的时候会发现生成了两个函数,一个带imageName,一个不带,选择不带的,调用此函数时将使用参数值nil convenience ini ...

  6. Vysor:在电脑里控制你的安卓手机

    Vysor是一款可以使用电脑操作安卓手机的谷歌浏览器插件,在chrome中安装了Vysor插件以后,用户就可以使用电脑打开手机的屏幕,在手机的屏幕界面中,用户不仅可以观看,甚至可以使用电脑直接操作手机 ...

  7. 【代码笔记】iOS-点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  8. 开源游戏 “Elvish Bird”

    简介: 这个游戏是我在今年(2014/03)课余时闲着无聊做的一个冒险类小游戏,总共花了5个工作日才完成,为了游戏的效率,做了很多优化,目前在IE8以上浏览器能够流畅运行,运行时如果屏幕分辨率不兼容, ...

  9. 在Asp.net MVC使用jqGrid--代码少点再少点

    本示例显示了如何动态生成前端jqGrid代码,一般情况仅一行代码: <%=Html.jqGrid<TestModel>(@"#jqT", "Test&q ...

  10. HTML基础(四)——设置超链接的样式示例

     ***设置超链接的样式示例  a:link 超链接被点前状态 a:visited 超链接点击后状态 a:hover 悬停在超链接时 a:active 点击超链接时 在定义这些状态时,有一个顺序l v ...