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

Bishops

05-18

Problem DescriptionnYesterday was Sam's birthday. The most interesting gift was definitely the chessboard. Sam quickly learned the rules of chess and defeated his father, all his friends, his little sister, and now no one wants to play with him any more. nnSo he decided to play with another birthday gift – a Book of Math Problems for Young Mathematicians. He opened the book somewhere in the middle and read the following problem: "How many knights can be placed on a chessboard without threatening each other?" After a while he realized that this was trivial and moved on to the next problem: "How many bishops can be placed on a chessboard without threatening each other?". Sam is in trouble here. He is not able to solve this problem and needs your help. nnTask SpecificationnnSam's chessboard has size N×N. A bishop can move to any distance in any of the four diagonal directions. A bishop threatens another bishop if it can move to the other bishop's position. Your task is to compute the maximum number of bishops that can be placed on a chessboard in such a way that no two bishops threaten each other. nn nnInputnThe input file consists of several lines. The line number i contains a single number representing the size( <10^100) of the i-th chessboard.n nnOutputnThe output file should contain the same number of lines as the input file. The i-th line should contain one number – the maximum number of bishops that can be placed on i-th chessboard without threatening each other. n nnSample Inputn2n3n nnSample Outputn2n4

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭