UVa 861 Little Bishops (combinatorial mathematics)

Source: Internet
Author: User
Tags sort time limit

861-little bishops

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=36&page=show_problem &problem=802

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

Now, given two numbers n and K, your job are to determine the number of ways one can put K bishops on a NXN chessboard s o that no two of the them are in attacking positions.

Input

The input file may contain multiple test cases. Each test case is occupies a single line in the input file and contains two integers n (1≤n≤8) and K (0≤K≤N2).

A test case containing two zeros for N and K terminates the input and your won ' t need to process this particular input.

Output

For the, the input print a line containing the total number of ways one can put the given number of Bishops O n a chessboard of the given size so this no two of them are in attacking positions. You may safely assume the this number would be less than 1015.

Sample Input
8 6
4 4
0 0

Sample Output
5599888
260

Train of thought: (The following number of lines refers to the diagonal line number of lines.) )

1. Number of enumerated elephants on white and black lattices: sum{rb[n][i] * rw[n-1][k-i]} (oblique, rb[i][j) means to place a J-car on the black grid in the first I line without conflicting methods, rw[i][j] means to place a J-car on the white grid in the first I line and Non-conflict methods)

2. How do I calculate rb[i][j] and rw[i][j]?

Take RB as an example, also squint, because each row can only place one car, then J car either all in the front i-1 line, or the first line has one. J-Car all in front of the I-1 line placement method is rb[i-1][j]; If the line I placed a car, then the first i-1 line placed j-1 car, then the former i-1 line in the place of the j-1 car has occupied the first line of the j-1 lattice, the remaining number of squares for B[i]-(J-1) (B[i] is the number of black cells in line i)

Then get the recursive type: rb[i][j] = Rb[i-1][j] + rb[i-1][j-1] * (b[i)-(j-1))

Complete code:

/*0.015s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std;  
typedef long Long LL;  
const int M = 8 + 5;  
    
const int N = 8 * 8 + 5; ll Rb[m][n], Rw[m][n], b[m], w[m];  
    B=black, w=white void calc (int n, int k, LL R[][n], LL c[]) {int I, J;  
    memset (r, 0, sizeof (r));  
    for (i = 0; I <= N; i++) r[i][0] = 1;  for (i = 1; I <= n; i++) for (j = 1; J <= C[i]; j +) R[i][j] = R[i-1][j] + r[i-1][j-1]  
* (C[i]-j + 1);  
    int main () {int I, j, N, K;  
    ll ans;  
        while (scanf ("%d%d", &n, &k), N) {memset (b, 0, sizeof (b));  
        memset (w, 0, sizeof (w));  for (i = 1; I <= n; i++) for (j = 1; J <= N; j +) {if ((i + j) &  
                1) ++w[(i + j) >> 1]; else ++b[(i + j) >> 1]; Calculates the diagonal lattice number of black and white so that you do not have to consider the parity of n 
        Sort (b + 1, B + n + 1);  
        Sort (w + 1, W + N);  
        Calc (n, K, Rb, B);  
        Calc (n-1, K, Rw, W);///respectively calculates the number of solutions on white and black lattices ans = 0;  
        for (i = 0; I <= K; i++) ans + rb[n][i] * Rw[n-1][k-i];  
    printf ("%lld\n", ans);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.