<|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.

222. Little Rooks
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard



Inspired by a "Little Bishops" problem, Petya now wants to solve problem for rooks.

A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move horizontally and vertically from its current position and two rooks 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 rooks 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 rooks on a chessboard of the given size so that no two of them are in attacking positions.

Sample test(s)

Input
4 4

Output
24
Author:	Andrew Stankevich
Resource:	Little Chess Pieces Series, SPb IFMO 2003-2004 Authumn Training Sessions
Date:	2003-10-01

<|response|>
1. Abridged Problem Statement  
Given integers n and k, determine the number of ways to place k rooks on an n×n chessboard so that no two rooks attack each other (i.e., no two share the same row or column). If k>n, the answer is 0.

2. Key Observations  
- Two rooks attack each other if and only if they share a row or a column.  
- To avoid attacks, each rook must occupy a unique row and a unique column.  
- We need to choose k distinct rows out of n, choose k distinct columns out of n, then assign one rook per chosen row–column pair in a one-to-one fashion.  
- The number of ways to choose k rows is C(n,k). Similarly for columns. The number of ways to match k chosen rows to k chosen columns is k! (a permutation of k elements).  
- Therefore the total number of valid placements is  
  C(n,k) × C(n,k) × k! = [C(n,k)]² × k!  
- If k>n, it is impossible to place k non-attacking rooks, so the answer is 0.

3. Full Solution Approach  
Step 1. Read n and k.  
Step 2. If k>n, print 0 and terminate.  
Step 3. Precompute factorials f[i] = i! for i=0…n.  
Step 4. Compute the binomial coefficient C(n,k) = f[n] / (f[k] * f[n−k]).  
Step 5. Compute the final answer as C(n,k) × C(n,k) × f[k].  
Step 6. Print the answer.  

Since n≤10, all intermediate values (factorials, binomial coefficients) fit comfortably into 64-bit integers.

4. C++ Implementation with Detailed Comments  
#include <bits/stdc++.h>
using namespace std;

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

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

    // If more rooks than rows/columns, no valid placement
    if (k > n) {
        cout << 0 << "\n";
        return 0;
    }

    // Precompute factorials up to n: f[i] = i!
    vector<long long> f(n + 1, 1LL);
    for (int i = 1; i <= n; ++i) {
        f[i] = f[i - 1] * i;
    }

    // Compute binomial coefficient C(n,k) = n! / (k! * (n-k)!)
    long long cnk = f[n] / (f[k] * f[n - k]);

    // Number of ways = C(n,k)^2 * k!
    long long result = cnk * cnk * f[k];

    cout << result << "\n";
    return 0;
}

5. Python Implementation with Detailed Comments  
import sys
import math

def main():
    data = sys.stdin.read().split()
    n, k = map(int, data)

    # If k > n, impossible to place k non-attacking rooks
    if k > n:
        print(0)
        return

    # Compute binomial coefficient C(n, k)
    cnk = math.comb(n, k)

    # Compute the total ways: C(n,k) * C(n,k) * k!
    result = cnk * cnk * math.factorial(k)

    print(result)

if __name__ == "__main__":
    main()