BZOJ 1653: [Usaco2006 Feb]Backward Digit Sums(暴力)

1653: [Usaco2006 Feb]Backward Digit Sums

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 349  Solved: 258 [Submit][Status][Discuss]

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 4OUTPUT DETAILS:

There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.

思路:n很小。。一开始做得时候把公式推错了。。3+3算成了4.我的内心是崩溃的。。

其实直接暴力就好。可以用next_permutation来生成全排列,然后判断是否合法。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月12日 星期二 10时54分23秒
 4File Name :code/bzoj/1653.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int n;
34int sum;
35int a[15];
36int tmp[15];
37int main()
38{
39	#ifndef  ONLINE_JUDGE 
40	freopen("code/in.txt","r",stdin);
41  #endif
42
43	scanf("%d %d",&n,&sum);
44	for ( int i = 1 ; i <= n ; i++) a[i] = i;
45
46	do
47	{
48	    for ( int i = 1 ; i <= n ; i++) tmp[i] = a[i];
49	    for ( int i = 1 ; i < n ; i++)
50		for ( int j = 1 ; j <= n - i ; j ++)
51		    tmp[j]+=tmp[j+1];
52	    if (tmp[1]==sum)
53	    {
54		for ( int i = 1 ; i< n ; i++)
55		    printf("%d ",a[i]);
56		printf("%d\n",a[n]);
57		break;
58	    }
59	}while (next_permutation(a+1,a+n+1));
60
61
62  #ifndef ONLINE_JUDGE  
63  fclose(stdin);
64  #endif
65    return 0;
66}