<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

220. Little Bishops
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard




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.

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 contains two integers n (1 ≤ n ≤ 10) and k (0 ≤ k ≤ n2).

Output

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.

Sample test(s)

Input
Test #1

4 4

Test #2

8 6

Output
Test #1

260

Test #2

5599888
Author:	Folklore, Andrew Stankevich
Resource:	Little Chess Pieces Series, SPb IFMO 2003-2004 Authumn Training Sessions
Date:	2003-09-27

<|response|>
1. Abridged problem statement  
Given an n×n chessboard (1≤n≤10) and an integer k (0≤k≤n²), count the number of ways to place k bishops so that no two attack each other. Bishops attack along both diagonals; no two bishops may share a diagonal.

2. Key observations  
- On a standard chessboard coloring, bishops on black squares never attack those on white squares. We can split the problem: count ways to place i bishops on black squares and k−i on white squares, then sum over i.  
- On one color, label the “main” diagonals by d = r+c (0≤d≤2n−2). Each diagonal d contains L[d] squares of that color.  
- We must place at most one bishop per main diagonal (otherwise they attack along that diagonal).  
- Besides main-diagonal conflicts, bishops on earlier diagonals attack exactly one square on each later main diagonal via the “anti-diagonals” (r−c constant). Since no two bishops share a main diagonal, their attacked squares on any future main diagonal are all distinct.  
- Therefore, if we have already placed p bishops on previous main diagonals, they block p distinct squares on the current diagonal, leaving L[d]−p free squares. To place one new bishop on diagonal d, we have (L[d]−p) choices.  

3. Full solution approach  
a. Precompute, for each color (0=black, 1=white), an array L_color[d] = number of squares of that color on main diagonal d, for d=0…2n−2.  
b. For each color, run a DP over diagonals to compute ways_color[b] = number of ways to place b bishops on that color’s squares without mutual attacks.  
   - Let DP be an array of length k+1, DP[j] = ways to have placed j bishops so far. Initialize DP[0]=1, DP[j>0]=0.  
   - Process diagonals d=0…2n−2 in order. For each diagonal with cnt = L_color[d]:  
     • Make newDP = DP (accounts for placing 0 bishops on this diagonal).  
     • For j from 0…k−1, if DP[j]>0 and cnt−j>0, then we can place one more bishop:  
         newDP[j+1] += DP[j] * (cnt−j)  
     • Replace DP = newDP.  
   - After all diagonals, DP[b] = ways_color[b].  
c. Finally answer = Σ_{i=0..k} ways_black[i] * ways_white[k−i].  

This runs in O(n·k) per color (n≤10,k≤100), which is instant.

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;

    // There are 2n-1 main diagonals (r+c = 0..2n-2).
    int D = 2*n - 1;

    // L[0] for black color, L[1] for white color.
    // L[col][d] = number of squares of color 'col' on main diagonal d.
    vector< vector<int> > L(2, vector<int>(D, 0));

    // Count squares by color and main diagonal index.
    for(int r = 0; r < n; r++){
        for(int c = 0; c < n; c++){
            int col = (r + c) & 1;    // 0=black, 1=white
            int d = r + c;            // main diagonal index
            L[col][d]++;
        }
    }

    // Function to compute ways[col][b] via DP over main diagonals.
    auto computeWays = [&](int col){
        // DP[j] = number of ways to place j bishops so far.
        vector<ll> dp(k+1, 0), newdp(k+1, 0);
        dp[0] = 1;  // zero bishops placed at start

        // Process each main diagonal d in increasing order
        for(int d = 0; d < D; d++){
            int cnt = L[col][d];     // squares available on this diagonal
            // Start newdp with the case of placing 0 bishops on diag d
            for(int j = 0; j <= k; j++){
                newdp[j] = dp[j];
            }
            // Try placing exactly one bishop on this diagonal
            // if we have placed j so far, we move to j+1
            for(int j = 0; j < k; j++){
                ll ways_j = dp[j];
                if(ways_j == 0) continue;
                // p=j bishops already placed ⇒ they block j squares on this diag
                // free spots = cnt - j
                int free_spots = cnt - j;
                if(free_spots > 0){
                    newdp[j+1] += ways_j * free_spots;
                }
            }
            // swap dp and newdp for next iteration
            dp.swap(newdp);
        }
        return dp;  // dp[b] = ways to place b bishops on color 'col'
    };

    // Compute separately for black (col=0) and white (col=1)
    vector<ll> waysBlack = computeWays(0);
    vector<ll> waysWhite = computeWays(1);

    // Combine the two color counts by convolution
    ll answer = 0;
    for(int i = 0; i <= k; i++){
        answer += waysBlack[i] * waysWhite[k - i];
    }

    cout << answer << "\n";
    return 0;
}
```

5. Python implementation with detailed comments  
```python
import sys
data = sys.stdin.read().split()
n, k = map(int, data)

# Number of main diagonals
D = 2 * n - 1

# L[0] for black, L[1] for white
L = [[0] * D for _ in range(2)]

# Count how many squares of each color appear on each main diagonal
for r in range(n):
    for c in range(n):
        color = (r + c) & 1   # 0 = black, 1 = white
        d = r + c             # main-diagonal index
        L[color][d] += 1

def compute_ways(color):
    """
    Returns a list dp where dp[b] is the number of ways to place b bishops
    on all squares of the given color without attacking each other.
    """
    # dp[j] = ways to have placed j bishops so far
    dp = [0] * (k + 1)
    dp[0] = 1  # base case: zero bishops

    # Process diagonals in order
    for d in range(D):
        cnt = L[color][d]   # total squares of this color on diag d
        newdp = dp[:]       # case of placing 0 bishops on diag d

        # Try placing exactly one bishop on diag d
        # If j bishops are already placed, they block j squares here
        for j in range(k):
            if dp[j] == 0:
                continue
            free_spots = cnt - j
            if free_spots > 0:
                newdp[j+1] += dp[j] * free_spots

        dp = newdp

    return dp

# Compute ways for each color
ways_black = compute_ways(0)
ways_white = compute_ways(1)

# Combine: choose i bishops on black, k-i on white
answer = 0
for i in range(k+1):
    answer += ways_black[i] * ways_white[k - i]

print(answer)
```

Explanation summary:  
- We exploit the fact that bishops on different colors cannot interact.  
- On each color, we do a DP across main diagonals. Each new bishop blocks exactly one square on every future main diagonal (via an anti-diagonal), so if we have placed j bishops already, a diagonal with cnt squares contributes (cnt−j) choices for placing one more bishop.  
- Finally we multiply (convolve) the counts from the two colors to get the total number of k-bishop placements.