POJ3187Backward Digit Sums[杨辉三角]

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

USACO 2006 February Gold & Silver


全排列中的数字对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=15;
int n,sum,c[N],vis[N],ans[N],flag=0;
void dfs(int d,int now){
    if(flag) return;
    if(d==n&&now==sum) {flag=1;for(int i=0;i<n;i++) printf("%d ",ans[i]);return;}
    if(d==n) return;
    for(int i=1;i<=n;i++){
        if(vis[i]) continue;
        if(now+i*c[d]<=sum){
            ans[d]=i; vis[i]=1;
            dfs(d+1,now+i*c[d]);
            vis[i]=0;
        }
    }
}
int main(int argc, const char * argv[]) {
    scanf("%d%d",&n,&sum);
    n--;
    c[0]=1;
    for(int i=1;i<=n;i++) c[i]=c[i-1]*(n-i+1)/i;
    n++;
    dfs(0,0);
    return 0;
}
时间: 2024-08-14 02:45:43

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

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

poj3187Backward Digit Sums(DFS)

题目链接: huangjing 思路: 这个题目想到dfs很容易,但是纠结在这么像杨辉三角一样计算那些值,这个我看的队友的,简直厉害,用递归计算出杨辉三角顶端的值....具体实现详见代码... 题目: Language: Default Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4285   Accepted: 2474 Description FJ and his cows

组合数杨辉三角(Irrelevant Elements uva1635)

Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbersranging from 0 to m - 1. He thinks that standard random number generators are not good enough, sohe has invented his own scheme that is intended to bri

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

杨辉三角

1 package com.llh.demo; 2 3 /** 4 * 杨辉三角 5 * 6 * @author llh 7 * 8 */ 9 public class Test { 10 /* 11 * 杨辉三角 12 */ 13 public static void main(String[] args) { 14 int[] a = new int[11]; 15 int num = 1; 16 // 17 for (int i = 1; i <= 10; i++) { 18 for (i

杨辉三角实例菱形实例

杨辉三角实例 public class Hui { public static void main (String [] args){ int [][] a =new int [10][10]; for(int i=0;i<a.length;i++){ for(int j=0;j<=i;j++){ if(j==0||i==j){ System.out.print(" "+(a[i][j]=1)); }else {a[i][j]=a[i-1][j-1]+a[i-1][j];

js算法集合(二) javascript实现斐波那契数列 (兔子数列) Javascript实现杨辉三角

js算法集合(二)  斐波那契数列.杨辉三角 ★ 上一次我跟大家分享一下做水仙花数的算法的思路,并对其扩展到自幂数的算法,这次,我们来对斐波那契数列和杨辉三角进行研究,来加深对Javascript的理解. 一.Javascript实现斐波那契数列 ①要用Javascript实现斐波那契数列,我们首先要了解什么是斐波那契数列:斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为

使用Java打印杨辉三角

package 杨辉三角; import java.util.Scanner; public class 三角 { private static Scanner scn; public static void main(String[] args) { scn = new Scanner(System.in); System.out.println("请输入数据"); int n = scn.nextInt(); //定义一个二维数组 int [][] array = new int

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

(1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个,即该位置上的元素在该行上最大,在该列上最小(注:一个二维数组也可能没有这样的鞍点). /** * * @author liuhui *@version Java上机实验三 *@time 2016.10.30 */ public class javatest2 { public static int