1057. Rhyme Schemes
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
The rhyme scheme for a poem (or stanza of a longer poem) tells which lines of the poem rhyme with which other lines. For example, a limerick such as
If computers that you build are quantum
Then spies of all factions will want 'em
Our codes will all fail
And they'll read our email
'Til we've crypto that's quantum and daunt 'em
Jennifer and Peter Shor
Has a rhyme scheme of aabba, indicating that the first, second and fifth lines rhyme and the third and fourth lines rhyme.
For a poem or stanza of four lines, there are 15 possible rhyme schemes:
aaaa, aaab, aaba, aabb, aabc, abaa, abab, abac, abba, abbb, abbc, abca, a bcb, abcc, and abcd.
Write a program to compute the number of rhyme schemes for a poem or stanza of N lines where N is an input value.
Input
Input will consist of a sequence of integers N (<= 50), one per line, ending with a 0 (zero) to indicate the end of the data. N is the number of lines in a poem.
Output
For each input integer N, your program should output the value of N, followed by a space, followed by the number of rhyme schemes for a poem with N lines.
Sample Input
1 2 3 4 20 30 10 0
Sample Output
1 1 2 2 3 5 4 15 20 51724158235372 30 84674901451180933245014710 115975
// Problem#: 1057 // Submission#: 3240791 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <string.h> #include <vector> #include <iomanip> #include <map> #include <stack> #include <functional> #include <list> #include <cmath> #include <set> using namespace std; const int MAX_DIGIT = 1000; const int MAX_SIZE = 51; string add(const string & left, const string & right) { int temp[MAX_DIGIT], l[MAX_DIGIT], r[MAX_DIGIT], ans[MAX_DIGIT]; int lSize = left.size(), rSize = right.size(); for (int i = 0; i < MAX_DIGIT; i++) { temp[i] = l[i] = r[i] = ans[i] = 0; } for (int i = 0; i < lSize; i++) { l[i] = left[lSize - i - 1] - '0'; } for (int i = 0; i < rSize; i++) { r[i] = right[rSize - i - 1] - '0'; } int maxlength = lSize > rSize ? lSize : rSize; for (int i = 0; i < maxlength; i++) { temp[i] += l[i] + r[i]; temp[i + 1] += temp[i] / 10; temp[i] = temp[i] % 10; } int i = maxlength + 10; for (; temp[i] == 0 && i > 0; i--); for (int j = 0; j <= i; j++) { ans[j] = temp[i + 1 - j - 1]; } string anss; anss.resize(i + 1); for (int j = 0; j < i + 1; j++) { anss[j] = ans[j] + '0'; } return anss; } string multi(int n, const string & right) { string originalR = "0"; for (int counter = 0; counter < n; counter++) { originalR = add(originalR, right); } return originalR; } int main() { std::ios::sync_with_stdio(false); string dp[MAX_SIZE][MAX_SIZE]; dp[0][0] = "1"; for (int i = 1; i < MAX_SIZE; i++) { for (int j = 1; j <= i; j++) { dp[i][j] = add(multi(j, dp[i - 1][j]), dp[i - 1][j - 1]); } } while (1) { int n; cin >> n; if (n == 0) break; string ans; for (int i = 1; i <= n; i++) { ans = add(ans, dp[n][i]); } cout << n << " " << ans << endl; } //getchar(); //getchar(); return 0; }