poj 3187 : Backward Digit Sums
来源:互联网 发布:百世快运软件下载 编辑:程序博客网 时间:2024/06/11 17:58
Backward Digit Sums
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3923 Accepted: 2235
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:
Write a program to help FJ play the game and keep up with the cows.
3 1 2 4 4 3 6 7 9 16Behind 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.
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
数据较小,熟悉掌握杨辉三角有关规律则能A掉
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=11;int n,sum,num[maxn],c[maxn];bool vis[maxn],flag;int find_c(int a,int n){ int i=1,ans=1; while(i<=a) { ans=ans*n/i; n--;i++; } return ans;}void dfs(int dep,int val){ if(val>sum) return; if(dep==n) { if(val==sum&&!flag) { flag=true; for(int j=0;j<n;j++) printf("%d ",num[j]); } return; } for(int j=1;j<=n&&!flag;j++) { if(!vis[j]) { num[dep]=j; vis[j]=1; dfs(dep+1,val+c[dep]*j); vis[j]=0; } }}int main(){ scanf("%d%d",&n,&sum); for(int i=0;i<n;i++) c[i]=find_c(i,n-1); dfs(0,0); return 0;}
0 0
- POJ 3187 Backward Digit Sums
- POJ 3187 Backward Digit Sums
- POJ 3187 Backward Digit Sums
- poj 3187 Backward Digit Sums
- POJ-3187-Backward Digit Sums
- poj 3187 Backward Digit Sums
- POJ-3187-Backward Digit Sums
- poj 3187 : Backward Digit Sums
- poj 3187 Backward Digit Sums
- POJ 3187 Backward Digit Sums
- POJ 3187 Backward Digit Sums
- POJ-3187 Backward Digit Sums
- POJ 3187 Backward Digit Sums
- POJ 3187 Backward Digit Sums
- poj 3187 Backward Digit Sums
- poj 3187 Backward Digit Sums
- POJ 3187 Backward Digit Sums
- Backward Digit Sums(POJ-3187)
- 一个系列增删改的三层的操作
- performance optimization
- JBPM6 学习(一)---JBPM6使用Oracle11g实现
- android 获取路径目录方法以及判断目录是否存在,创建目录
- 向解决方案中添加一个已经存在的工程(EnvDTE)
- poj 3187 : Backward Digit Sums
- Python 学习笔记(一) python基础
- ssh安装配置
- unrecognized selector sent to instance 0x266070
- 动态规划-单调递增最长子序列(三)
- php修改php.ini重启nginx php.ini设置不生效
- 设计模式(一)Singleton 模式
- Ubuntu硬盘分区/格式化/挂载文件系统各种应用
- 【您正在pdf/A模式中查看本文档】去除方法