sgu220:Little Bishops(DP)

题意及题解请走
http://www.cnblogs.com/jianglangcaijin/archive/2013/01/22/2872059.html
就是转换一下模型后dp.

AC code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long LL;
#define ONLINE_JUDGE

const int MAXN = 59;

int n, m;
int cur[2][MAXN], tot[2];

struct bignum
{
    int size;
    int a[109];
    bignum()
    {
        size = 1;
        memset(a, 0, sizeof(a));
    }
    void print()
    {
        for(int i = size; i >= 1; --i)
            putchar('0'+a[i]);
    }
}ini;

bignum operator + (const bignum &a, const bignum &b)
{
    bignum ret;
    int sz = std::max(a.size, b.size);
    for(int i = 1; i <= sz; ++i)
    {
        ret.a[i] += a.a[i]+b.a[i];
        ret.a[i+1] += ret.a[i]/10;
        ret.a[i] %= 10;
    }
    if(ret.a[sz+1]) ret.size = sz+1;
    else ret.size = sz;
    return ret;
}

bignum operator * (const bignum &a, const bignum &b)
{
    bignum ret;
    for(int i = 1; i <= a.size; ++i)
        for(int j = 1; j <= b.size; ++j)
            ret.a[i+j-1] += a.a[i]*b.a[j];
    for(int i = 1; i <= a.size+b.size-1; ++i)
    {
        ret.a[i+1] += ret.a[i]/10;
        ret.a[i] %= 10;
    }
    ret.size = a.size+b.size;
    while(!ret.a[ret.size]) ret.size--;
    if(!ret.size) ret.size = 1;
    return ret;
}

bignum change(int p)
{
    bignum ret;
    ret.size = 0;
    if(p == 0) ++ret.size;
    else
    {
        while(p)
        {
            ret.a[++ret.size] = p%10;
            p /= 10;
        }
    }
    return ret;
}

bignum operator * (const bignum &a, int p)
{
    return a*change(p); 
}

bignum f[2][MAXN][MAXN];
bignum ans;

void work(int x)
{
    tot[x-1] = 0;
    for(int i = x; i <= n; i += 2)
    {
        cur[x-1][++tot[x-1]] = i;
        if(i != n) cur[x-1][++tot[x-1]] = i;
    }
    f[x-1][0][0].a[1] = 1;
    for(int i = 1; i <= tot[x-1]; ++i)
    {
        f[x-1][i][0].a[1] = 1;
        for(int j = 1; j <= m && j <= cur[x-1][i]; ++j)
            f[x-1][i][j] = f[x-1][i-1][j]+f[x-1][i-1][j-1]*(cur[x-1][i]-j+1);
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("sgu220.in", "r", stdin);
    freopen("sgu220.out", "w", stdout);
    #endif
    std::ios::sync_with_stdio(0);
    std::cin >> n >> m;
    if(m > 2*n-1) puts("0");
    else
    {
        work(1);
        work(2);
        for(int i = 0; i <= m; ++i)
            ans = ans+f[0][tot[0]][i]*f[1][tot[1]][m-i];
        ans.print();
        puts("");
    }

    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
阅读更多
个人分类: SGU
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭