您的位置:首页 > 其它

poj 3187 Backward Digit Sums 【STL暴力】

2015-12-20 13:35 281 查看
Backward Digit Sums

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5413 Accepted: 3123
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

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

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.

题意:给定一个数N,表示杨辉三角的层数。现在让你找到一个字典序最小的1-N的排列使得以该排列为第一层的杨辉三角的最后一层是m。

思路:N最多为10,用next_permutation比较方便吧。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (1000000+1)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int Map[20][20];
int num[20];
bool judge(int n, int sum)
{
for(int i = 1; i <= n; i++)
Map
[i] = num[i];
for(int i = n-1; i >= 1; i--)
for(int j = 1; j <= i; j++)
Map[i][j] = Map[i+1][j] + Map[i+1][j+1];
return Map[1][1] == sum;
}
int main()
{
int N, M;
while(scanf("%d%d", &N, &M) != EOF)
{
for(int i = 1; i <= N; i++)
num[i] = i;
do
{
if(judge(N, M))
{
for(int i = 1; i <= N; i++)
{
if(i > 1) printf(" ");
printf("%d", num[i]);
}
printf("\n");
break;
}
}
while(next_permutation(num+1, num+N+1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: