Allowance
As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different 阅读全文 »
Allowance
As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different 阅读全文 »
Backward Digit Sums
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.
Source
USACO 2006 February Gold & Silver
题目类型:序列枚举+组合计数
算法分析:从小到大按照字典序生成序列,然后对于每个序列计算相邻和,简单观察会发现最后的和是Sigma(C(n - 1, i - 1) * a[i]),不需要模拟计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
/************************************************* Author :supermaker Created Time :2016/4/12 12:38:57 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int aa[16], com[16][16]; void getCom () { memset (com, 0, sizeof (com)); for (int i = 0; i <= 10; i++) for (int j = 0; j <= i; j++) { if (!j || i == j) com[i][j] = 1; else com[i][j] = com[i-1][j-1] + com[i-1][j]; } com[0][0] = 0; } int main() { //CFF; //CPPFF; int n, val; getCom (); while (scanf ("%d%d", &n, &val) != EOF) { for (int i = 1; i <= n; i++) aa[i] = i; int tt; do { tt = aa[1] + aa[n]; for (int i = 2; i <= n - 1; i++) tt += com[n-1][i-1] * aa[i]; if (tt == val) break; } while (next_permutation (aa + 1, aa + 1 + n)); for (int i = 1; i <= n; i++) { if (i == 1) printf ("%d", aa[i]); else printf (" %d", aa[i]); } puts (""); } return 0; } |