您的位置:首页 > 其它

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,写了一份交上去,可惜超时。。。

超时的程序:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 题解