poj3187 Backward Digit Sums
2015-10-23 15:45
447 查看
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
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.
第一反应是dfs,写了一份交上去,可惜超时。。。
超时的程序:
怎么优化呢,生成排列是不能优化了。所以优化求和的过程,这个过程就是杨辉三角的求和:
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
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.
第一反应是dfs,写了一份交上去,可惜超时。。。
超时的程序:
#include <cstdio> #include <cstdlib> #define MAX_N 15 int a[MAX_N],che[MAX_N],n,sum; bool used[MAX_N]; int sum_up() { for(int i = 1;i <= n - 1;i++) { che[i] = a[i] + a[i + 1]; } int j = n - 1; while(j != 1) { for(int i = 1;i <= j - 1;i++) { che[i] += che[i + 1]; } j--; } return che[1]; } bool check() { if(sum_up() == sum)return true; return false; } void dfs(int x,int num) { a[x] = num; if(x == n) { if(check()) { for(int i = 1;i <= n;i++) { printf("%d ",a[i]); } exit(0); } } for(int i = 1;i <= n;i++) { if(!used[i]) { used[i] = true; dfs(x + 1,i); used[i] = false; } } } int main() { scanf("%d%d",&n,&sum); for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++)used[j] = false; used[i] = true; dfs(1,i); used[i] = false; } return 0; }
怎么优化呢,生成排列是不能优化了。所以优化求和的过程,这个过程就是杨辉三角的求和:
#include <iostream> #include <string> #include <algorithm> using namespace std; int C(int n,int k) { int result = 1; for(int i = 0;i < k;++i) { result = result * (n - i) / (i + 1); } return result; } int main() { int N,sum; cin >> N >> sum; int line[16],i = 0; for(;i < N;++i) { line[i] = i + 1; } do { int result = 0; for(i = 0;i < N;++i) { result += C(N - 1,i) * line[i]; } if(result == sum) { break; } }while(next_permutation(line,line + N));//用到了next_permutation这个函数来生成排列 for(i = 0;i < N;++i) { cout << line[i] << " "; } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板