Sicily 1057. Rhyme Schemes

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 846749014511809332450147

10 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;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值