【BZOJ】1653:[Usaco 2006 Feb]Backward Digit Sums(暴力)

2915 ワード

http://www.lydsy.com/JudgeOnline/problem.php?id=1653
問題解を見てこそできます。Tシャツ。T
私達は直接一つ一つの状況を列挙します。
そして状況に合うかどうかを判断する(積算判断)
#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <iostream>

#include <algorithm>

#include <queue>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); ++i)

#define for1(i,a,n) for(int i=(a);i<=(n);++i)

#define for2(i,a,n) for(int i=(a);i<(n);++i)

#define for3(i,a,n) for(int i=(a);i>=(n);--i)

#define for4(i,a,n) for(int i=(a);i>(n);--i)

#define CC(i,a) memset(i,a,sizeof(i))

#define read(a) a=getint()

#define print(a) printf("%d", a)

#define dbg(x) cout << #x << " = " << x << endl

#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }

inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

inline const int max(const int &a, const int &b) { return a>b?a:b; }

inline const int min(const int &a, const int &b) { return a<b?a:b; }



int n, m;

int a[15], f[15];

int main() {

	read(n); read(m);

	for1(i, 1, n) a[i]=i;

	do {

		memcpy(f, a, sizeof(a));

		for1(i, 1, n-1) for3(j, i, 1) f[j]+=f[j+1];

		if(f[1]==m) {

			printf("%d", a[1]);

			for1(i, 2, n) printf(" %d", a[i]);

			puts("");

			break;

		}

	}while(next_permutation(a+1, a+1+n));

	return 0;

}

 
 
 
 
Description
FJ and his cows enjoy playing a mental game.The y write down the numbers from 1 to N(1==N==10)in a certain order and then sum adjacenumbers to produce a new list with one fewers number.Thexypeinfont。might go like this:3 1 2 4 6 9 Behind FJ's back、the cows have started playing a more difficult game、in which the y to determine the starting sequence from onlythe final total and the number.Unight forelturythe game is a bit above FJ's mental arthmetic capabilitys.Write a program to help FJプレイthe game and keep up with the cows.
Input
*Line 1:Two space-separated integers:N and the final sum.
Output
*Line 1:An ordeng of the integers 1.N that leads to the given sum.If there re multiple soutions,chose the one that is lexicographically least,i.e.that put smarr numberst.
Sample Input
4 16
Sample Output
3 1 2 4
OUT DETAILS:
The re are other possible sequences、such as 3 2 1 4、but 3 1 2 2 4
is the lexicographicly smalest.
HINT
ソurce
シルバーカバー