POJ3187Backward Digit Sums[杨辉三角]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6350 | Accepted: 3673 |
Description
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
Output
Sample Input
4 16
Sample Output
3 1 2 4
Hint
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
Source
全排列中的数字对sum贡献的次数就是杨辉三角第n-1列
//
// 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[杨辉三角]的更多相关文章
- POJ2167Irrelevant Elements[唯一分解定理 组合数 杨辉三角]
Irrelevant Elements Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2407 Accepted: 59 ...
- [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, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- poj3187-Backward Digit Sums(枚举全排列)
一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角) 3 1 2 4 //1~n 全排列中的一个排列 4 3 6 7 ...
- python生成器实现杨辉三角
def triangels(): """ 杨辉三角 """ lst = [1] n_count = 2 # 下一行列表长度 while Tr ...
- python 生成器生成杨辉三角
用Python写趣味程序感觉屌屌的,停不下来 #生成器生成展示杨辉三角 #原理是在一个2维数组里展示杨辉三角,空的地方用0,输出时,转化为' ' def yang(line): n,leng=0,2* ...
- HDNOIP201405杨辉三角
2016.1.27 试题描述 杨辉三角是形如如下的数字三角形: 1 1 1 1 2 1 …… 现在想求出杨辉三角第N行的N个数中,有多少个数能被给定的质数p整除. 输入 一行两个空格隔 ...
- Java的二维数组的应用及杨辉三角的编写
(1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...
- 杨辉三角用java实现
代码如下: public class ErArray { public static void main(String[] args) { //杨辉三角 int[][] num = new int[1 ...
随机推荐
- 编写jquery常用插件的基本格式
写jquery插件需要明白的那些知识点 1.jquery中$是神马?$.fn又是神马? 稍微有jquery经验的都知道在jquery中$等价于jQuery,在控制台一试便知: 我们在jquery(1. ...
- java--POI解析excel兼容性问题
近日,使用POI解析excel,发现2003版本的excel解析与2007版本的excel解析存在问题.特此总结: 1.所需jar包 : 2.java类代码(读取excel文件): public vo ...
- web基础
1.认识webapp程序? 请求方式不同:基于事件触发------基于http协议下的http请求和http响应.点击百度一下-----发送了请求:不仅会携带问题,ip地址,主机号.请求是客户 ...
- sharepoint 2013 持续爬网
能否对所有类型的内容源都使用连续爬网?不能.连续爬网仅适用于 SharePoint 型内容源.所有其他类型的内容源将继续选择增量爬网和完全爬网. 使用连续爬网是否会给存储库增加额外负载?连续爬网的资源 ...
- swift开发多线程篇 - 多线程基础
swift开发多线程篇 - 多线程基础 iOS 的三种多线程技术 (1)NSThread 使用NSThread对象建立一个线程非常方便 但是!要使用NSThread管理多个线程非常困难,不推荐使用 ...
- redis数据类型
Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...
- 敏捷软件开发(4)--- TEMPLATE METHOD & STRATEGY 模式
1.TEMPLATE METHOD 泛型,也就是这个模式,是可以基于泛型的. 我们往往会有一些算法,比如排序算法.它的算法部分,我可以把它放在一个基类里面,这样具体类型的比较可以放在子类里面. 看如下 ...
- Asp.Net Web Form 前后台传值
1,后台往前台传值----单个变量直接传递到页面元素 前台代码 <b><%=strCompanyName%>费用明细</b> 后台代码 public partial ...
- 区别和详解:jQuery extend()和jQuery.fn.extend()
1.认识jQuery extend()和jQuery.fn.extend() jQuery的API手册中,extend方法挂载在jQuery和jQuery.fn两个不同对象上方法,但在jQuery内部 ...
- C#处理猜拳问题(非窗体)
//猜拳,5局3胜,要求使用公用变量. namespace 结构体复习_公用变量 { class Program {public int rz=0; public int dz = 0; public ...