比赛 20121107 评测结果 AAAAA
题目名称 三只小猪 最终得分 100
用户昵称 王者自由 运行时间 0.013 s
代码语言 C++ 内存使用 2.48 MiB
提交时间 2012-11-07 11:21:59
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int N = 50 + 1;
int t, n, m;
const int M = 100;
struct bign {
    int l, s[N];
    bign() {
        fill_n(s, N, 0);
        l = 1;
    }
    bign operator = (int v) {
        char t[M];
        sprintf(t, "%d", v);
        *this = t;
        return *this;
    }
    bign operator = (const char* v) {
        l = strlen(v);
        for(int i=0; i<l; i++)
            s[i] = v[l-i-1] - '0';
        return *this;
    }
    void print() const {
        for(int i=l-1; i>=0; i--)
            printf("%d", s[i]);
        printf("\n");
    }
    bign operator + (const bign& b) const {
        bign c; c.l = 0;
        for(int i=0, g=0; g || i < max(l, b.l); i++) {
            int x = g;
            if(i < l) x += s[i];
            if(i < b.l) x += b.s[i];
            c.s[c.l++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    void clean() {
        while(l > 1 && !s[l-1]) l--;
    }
    bign operator * (const bign& b) const {
        bign c; c.l = l + b.l;
        for(int i=0; i<l; i++)
            for(int j=0; j<b.l; j++)
                c.s[i+j] += s[i] * b.s[j];
        for(int i=0; i<c.l-1; i++){
            c.s[i+1] += c.s[i] / 10;
            c.s[i] %= 10;
        } c.clean();
        return c;
    }
} f[N][N], u[N];
int main() {
    freopen("piggy.in", "r", stdin);
    freopen("piggy.out", "w", stdout);
    for(int i=0; i<N; i++)
        f[i][0] = f[0][i] = 0, u[i] = i;
    f[0][0] = 1;
    for(int i=1; i<N; i++)
        for(int j=1; j<N; j++)
            f[i][j] = u[j] * f[i-1][j] + f[i-1][j-1];
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n, &m);
        f[n][m].print();
    }
    return 0;
}