<|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++ Solution
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto& x: a) {
        out << x << ' ';
    }
    return out;
}

int n, k;
vector<pair<int, int>> black_cells, white_cells;
vector<bool> used_diag1_black, used_diag2_black;
vector<bool> used_diag1_white, used_diag2_white;
vector<int64_t> ways_black, ways_white;

void read() { cin >> n >> k; }

void backtrack_black(int idx, int placed) {
    if(idx == (int)black_cells.size()) {
        if(placed <= k) {
            ways_black[placed]++;
        }

        return;
    }

    backtrack_black(idx + 1, placed);

    auto [r, c] = black_cells[idx];
    int d1 = r + c;
    int d2 = r - c + (n - 1);
    if(!used_diag1_black[d1] && !used_diag2_black[d2]) {
        used_diag1_black[d1] = used_diag2_black[d2] = true;
        backtrack_black(idx + 1, placed + 1);
        used_diag1_black[d1] = used_diag2_black[d2] = false;
    }
}

void backtrack_white(int idx, int placed) {
    if(idx == (int)white_cells.size()) {
        if(placed <= k) {
            ways_white[placed]++;
        }

        return;
    }

    backtrack_white(idx + 1, placed);

    auto [r, c] = white_cells[idx];
    int d1 = r + c;
    int d2 = r - c + (n - 1);
    if(!used_diag1_white[d1] && !used_diag2_white[d2]) {
        used_diag1_white[d1] = used_diag2_white[d2] = true;
        backtrack_white(idx + 1, placed + 1);
        used_diag1_white[d1] = used_diag2_white[d2] = false;
    }
}

void solve() {
    // Bishops attack along diagonals. Rotating the board 45 degrees, the two
    // diagonal directions become rows and columns, and the board splits into
    // two independent sub-problems: cells of one colour (by (r+c) parity) never
    // share a diagonal with cells of the other colour. So we enumerate, for
    // each colour separately, how many ways there are to place exactly i
    // non-attacking bishops on that colour's cells, recording the counts in
    // ways_black[i] / ways_white[i] via backtracking that tracks which "/" and
    // "\" diagonals are already occupied. The total number of placements of k
    // bishops is then the convolution sum over i of
    // ways_black[i] * ways_white[k - i].

    black_cells.clear();
    white_cells.clear();
    for(int r = 0; r < n; r++) {
        for(int c = 0; c < n; c++) {
            if(((r + c) & 1) == 0) {
                black_cells.emplace_back(r, c);
            } else {
                white_cells.emplace_back(r, c);
            }
        }
    }

    used_diag1_black.assign(2 * n, false);
    used_diag2_black.assign(2 * n, false);
    used_diag1_white.assign(2 * n, false);
    used_diag2_white.assign(2 * n, false);

    ways_black.assign(k + 1, 0);
    ways_white.assign(k + 1, 0);

    backtrack_black(0, 0);
    backtrack_white(0, 0);

    int64_t ans = 0;
    for(int i = 0; i <= k; i++) {
        ans += ways_black[i] * ways_white[k - i];
    }

    cout << ans << "\n";
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    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.