SGU - 222 Little Rooks

来源:互联网 发布:数控电火花编程实例 编辑:程序博客网 时间:2024/06/05 11:02
Little Rooks
Time Limit: 250MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

Description

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 ≤ n 2). 

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 
[submit]
[forum]

Author:Andrew StankevichResource:Little Chess Pieces Series, SPb IFMO 2003-2004 Authumn Training SessionsDate:2003-10-01











组合数和排列数,数据不大,结果就是C(n,k)*A(n,k),还要注意k=0时结果为1,k=1时结果为n*n,k>n时为0.

#include<iostream>using namespace std;int main(){long long int n, k;while (cin >> n >> k){long long int sum = 1;long long int ans = 1;if (k > n){cout << 0 << endl; continue;}if (k == 1){cout << n*n << endl; continue;}for (long long int i = n; i > n - k; i--)sum = sum*i;ans = sum;for (long long i = 1; i <= k; i++)ans = ans / i;ans = ans*sum;cout << ans << endl;}}


0 0