SGU[222] Little Rooks

Source: Internet
Author: User

DescriptionDescribe

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 are played on a board of square grids. A Rook can only move horizontally and vertically from the its current position and the rooks attack each other if one are on th E Path of the other.

Given-numbers n and K, your job is-determine the number of ways one can put K rooks on a nxn chessboard so that No. Them is in attacking positions.

Inspired by the "Little BIshops" problem, Petya now wants to solve the car problem.

The car is a piece on a square grid board. It can only move horizontally or vertically, and if there are other vehicles on its route, they may attack each other.

Given the two numbers n and K, your task is to ask for the number of scenarios where K-cars are placed on the NXN board, making them non-attacking each other.

InputInput

The input file contains the integers n (1≤n≤10) and K (0≤k≤n^2).

The input consists of two integers n (1 <= n <= 10) and K (0 <= k <= n^2).


OutputOutput

Print a line containing the total number of ways one can put the given number of rooks on a chessboard of the given size s o that no. them is in attacking positions.

The output line contains the number of scenarios where a given number of cars are placed on a chessboard of a given size, so that they do not attack each other.


Sample InputSample input

4 4


Sample OutputSample output

24

AnalysisAnalysis

First of all, the car will only be horizontal or vertical attack, then we just have to ensure that the K-car each exclusive row one column, then their relative position altogether has k! kind of situation.

Consider the checkerboard size below, if n < K, then cannot satisfy K cars can not each exclusive row of one column. Since we only need to place K-row K-columns, there are a total of C (n, k) * C (n, k) cases.

Then the answer is: C (n, k) * C (n, k) * k!, simplified: n! * n! /(k! * (N-K)! * (N-K)!), considering the possibility of overflow, let's change the order of operations.

That is: ans = n! /(k! * (N-K)!) * n! /(N-K)!.

For the factorial calculation, you can directly hit the table, the maximum needs only 10!.

SolutionSolution Solutions
#include <iostream> #include <algorithm>using namespace std;const int MAX = 16;unsigned long Long f[max];int ma In () {f[0] = 1;for (int i = 1; i < MAX; i++) {f[i] = f[i-1] * i;} int N, k;while (CIN >> n >> K) {if (K > N) {cout << 0 << Endl;} else {cout << f[n]/f[k]/f[n-k] * F[n]/f[n-k] << Endl;}} return 0;}

  

At first I thought it was like the eight queens, ready to write a check function, and later found just a simple permutation.

SGU[222] Little Rooks

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.