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 ...
随机推荐
- 【前端盲点】DOM事件流论证CSS盒模型是否具有厚度
前言 很久没有扯淡了,我们今天来扯淡吧. 我今天思考了一个问题,我们页面的dom树到底是如何渲染的,而CSS盒模型与javascript是否有联系,于是便想到一个问题: CSS的盒模型具有厚度么??? ...
- Python将Excel生成SHP
#!/usr/bin/env python # -*- coding: utf-8 -*- import gdal import xlrd import shapefile # open the ex ...
- 使用PDFCreate 和 Powershell 自动保存网页为PDF
先安装PDF Creator. http://rj.baidu.com/soft/detail/10500.html?ald 把他设置为默认打印机. 在IE中设置打印页面的边距,页眉页脚等. Powe ...
- SQLite Design and Concepts
API 分为两大类 core API. 基本的SQL操作 extension API. 创建自定义的SQL操作. 基本数据结构 需要了解的组成部分有连接.statments.B树.pager. 为了写 ...
- Android 内容提供者的实现
接着上文<Android 内容提供者简介>进一步实现内容提供者. 每个Content Provider类都使用URI(Universal Resource Identifier,通用资源标 ...
- iOS 播放GIF动态图片!!!!!
////////////////////////////////////////////////////////////////////////////////////////// //// Vie ...
- error=Error Domain=NSURLErrorDomain Code=-1003
我的情况:模拟器 可以返回数据 .真机不可以.我手机连接的同事的共享,我以为他的网段和后台的网段在同一个网段.一直在找错误,打开手机网络设置一看 ,原来不在同一网段.手机的网络必须要跟PC的在同一个 ...
- iOS开发之邓白氏编码申请流程
要申请企业证书,必须先申请邓白氏编码,在苹果网站有一个免费申请邓白氏编码的链接:https://developer.apple.com/program/enroll/dunsLookupForm.ac ...
- 【Android自定义控件】支持多层嵌套RadioButton的RadioGroup
前言 非常喜欢用RadioButton+RadioGroup做Tabs,能自动处理选中等效果,但是自带的RadioGroup不支持嵌套RadioButton(从源码可看出仅仅是判断子控件是不是Radi ...
- 【Android】魅族Flyme OS 3摄像头无法预览的问题
错误代码: 12-12 14:28:34.692: E/AndroidRuntime(1524): java.lang.RuntimeException: startPreview failed 12 ...