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. Dotfuscator混淆加密

    混淆加密 1. 需要安装Dotfuscator软件 2. 安装好后打开软件,找到编译好的DLL文件 3. 打开[setting]设置属性,如下图: 把 Disable String Encryptio ...

  2. WAF指纹识别和XSS过滤器绕过技巧

    [译文] -- “Modern Web Application Firewalls Fingerprinting and Bypassing XSS Filters” 0x1 前言 之前在乌云drop ...

  3. atitit.userService 用户系统设计 v4 q316 .doc

    atitit.userService 用户系统设计 v4 q316 .doc 1. 新特性1 2. Admin  login1 3. 用户注册登录2 3.1. <!-- 会员注册使用 --> ...

  4. JavaScript学习03 JS函数

    JavaScript学习03 JS函数 函数就是包裹在花括号中的代码块,前面使用了关键词function: function functionName() { 这里是要执行的代码 } 函数参数 函数的 ...

  5. Android Studio教程--给Android Studio安装Genymotion插件

    打开Android Studio,依次[File]-[Settings] 在打开的settings界面里找到plugins设置项,点击右侧的“Browser..”按钮 在搜索栏里输入genymotio ...

  6. GCD基础知识总结

    iOS三种多线程编程技术: 1.NSThread 2.NSOperation 3.GCD(Grand Central Dispatch) 从上到下,抽象度层次从低到高,抽象度越高的使用越简单,也是Ap ...

  7. Routine

    前端要学习三个部分:HTML,CSS,JavaScript(简称JS),因此首先明确三个概念:HTML是内容层,它的目的是表示一个HTML标签在页面里是个什么角色. CSS是样式层,它的目的是表示一块 ...

  8. WPF学习之路(四)路由

    路由事件概述 功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件. 实现定义:路由事件是一个 CLR 事件,可以由RouteEvent 类的实例提供 ...

  9. input的file 控件及美化

    在一些网站进行上传时,当单击了“浏览”按钮之后会弹出[选择文件]的对话框.想要实现这一功能,用input的file控件来实现就好啦~ <!doctype html> <html la ...

  10. jstack简单使用,定位死循环、线程阻塞、死锁等问题

    当我们运行java程序时,发现程序不动,但又不知道是哪里出问题时,可以使用JDK自带的jstack工具去定位: 废话不说,直接上例子吧,在window平台上的: 死循环 写个死循环的程序如下: pac ...