uva 10237 bishops

Problem B

Bishops
Input: standard input
Output: standard output
Time Limit: 4 seconds
Memory Limit: 32 MB

 

A bishop is a piece used in the game of chess which is played on a board of square grids. A bishop can only move diagonally from its current position and two bishops attack each other if one is on the path of the other. In the following figure, the dark squares represent the reachable locations for bishop B1 form its current position.  The figure also shows that the bishops B1 and B2 are in attacking positions whereas B1 and B3 are not. B2 and B3 are also in non-attacking positions.

 

 

Now, given two numbers n and k, your job is to determine the number of ways one can put k bishops on an n × n chessboard so that no two of them are in attacking positions.

 

Input

 The input file may contain multiple test cases. Each test case occupies a single line in the input file and contains two integers n (1 ≤ n ≤ 30) and k (0 ≤ k ≤ n2).

 

A test case containing two zeros for n and k terminates the input and you won’t need to process this particular input.

 

Output

For each test case in the input print a line containing the total number of ways one can put the given number of bishops on a chessboard of the given size so that no two of them are in attacking positions. You may safely assume that this number will be less than 1015.

 

Sample Input
8 6
4 4
20 40
30 5
0 0

 

Sample Output
5599888
260
0
3127859642656

(World Finals Warm-up Contest, Problem setter: Rezaul Alam Chowdhury)

 

 

"I think Garry Kasparov will like this problem very much!!!"



黑书上的题目,最后看了人家的解法。。。


http://fqq11679.blog.hexun.com/24991343_d.html#


F[i,j] = (Len[i]-j+1)*F[i-1,j-1] + F[i-1,j]


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>

#define N_MAX 35
#define K_MAX 1000

long long lenR [N_MAX];
long long lenB [N_MAX];

long long dpR [N_MAX][K_MAX];
long long dpB [N_MAX][K_MAX];

int n, k;
int szR, szB;

void MakeR () {
    szR = n;
    lenR[0] = lenR[1] = 1;
    for (int i=2; i<szR; i+=2) {
        lenR[i] = lenR[i + 1] = lenR[i - 1] + 2;
    }
}

void MakeB () {
    szB = n - 1;
    lenB[0] = lenB[1] = 2;
    for (int i=2; i<szB; i+=2) {
        lenB[i] = lenB[i + 1] = lenB[i - 1] + 2;
    }
}

void DP (long long dp[N_MAX][K_MAX], long long len[N_MAX], int sz) {
    memset (dp, 0, sizeof (**dp) * N_MAX * K_MAX);
    for (int i=0; i<=sz; ++i) {
        dp[i][0] = 1;
    }
    for (int i=1; i<=sz; ++i) {
        for (int k=1; k<=::k; ++k) {
            dp[i][k] = (len[i - 1] - k + 1) * dp[i - 1][k - 1] + dp[i - 1][k];
        }
    }
#ifdef _DEBUG
     printf ("\n");
     for (int i=0; i<=sz; ++i) {
        for (int k=0; k<=::k; ++k) {
            printf ("%10lld%s", dp[i][k], k==::k ? "\n" : "");
        }
     }
     printf ("\n");
#endif
}

void Solve () {
    if (scanf ("%d%d", &::n, &::k) == EOF || ::n == 0 && ::k == 0) {
        exit (0);
    }
    MakeR ();
    MakeB ();
    DP (dpR, lenR, szR);
    DP (dpB, lenB, szB);
    long long ans = 0;
    for (int i=0; i<=::k; ++i) {
        ans += dpR[szR][i] * dpB[szB][::k - i];
    }
    printf ("%lld\n", ans);
}

int main () {
    while (true) {
        Solve ();
    }
    return 0;
}


阅读更多
个人分类: ACM
想对作者说点什么? 我来说一句

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

关闭
关闭
关闭