1005 - Rooks(规律)

1005 - Rooks
Time Limit: 1 second(s) Memory Limit: 32 MB

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 vertically or horizontally from its current position and two rooks attack each other if one is on the path of the other. In the following figure, the dark squares represent the reachable locations for rook R1 from its current position. The figure also shows that the rook R1 and R2 are in attacking positions where R1 and R3 are not. R2 and R3 are also in non-attacking positions.

 

Now, given two numbers n and k, your job is to determine the number of ways one can put k rooks on an n x n chessboard so that no two of them are in attacking positions.

Input

Input starts with an integer T (≤ 350), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 30) and k (0 ≤ k ≤ n2).

Output

For each case, print the case number and 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. You may safely assume that this number will be less than 1017.

Sample Input

Output for Sample Input

8

1 1

2 1

3 1

4 1

4 2

4 3

4 4

4 5

Case 1: 1

Case 2: 4

Case 3: 9

Case 4: 16

Case 5: 72

Case 6: 96

Case 7: 24

Case 8: 0

 题解:这个题感触好深,自己也就是个傻叉,从开始就想着深搜,搜啊搜,搜了好久,搜出来了,超时了。。。傻叉到极致,30的数据量每列还是30,自己真敢想啊;这要搜下去到何年何月。。。其实就是个规律,从n行里面选m行放棋子,这个无序,从n列选m列放旗子,这个有序。。。就得出来规律了,C(n,m)*A(n,m);

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<map>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 using namespace std;
11 const int INF=0x3f3f3f3f;
12 int main(){
13     int n,m,T,flot=0;
14     scanf("%d",&T);
15     while(T--){
16         long long ans=1;
17         scanf("%d%d",&n,&m);
18         for(int i=n;i>(n-m);i--)ans*=i;
19         for(int i=1;i<=m;i++)ans/=i;
20         for(int i=n;i>(n-m);i--)ans*=i;
21         printf("Case %d: %lld\n",++flot,ans);
22     }
23     return 0;
24 }

见见我的逗比超时dfs:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<map>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 using namespace std;
11 const int INF=0x3f3f3f3f;
12 const int MAXN=1010;
13 int vis[MAXN];
14 int n,k;
15 long long ans;
16 void dfs(int x,int num){
17     if(num==k){
18         ans++;
19         return;
20     }
21     if(x>=n)return;
22         for(int i=0;i<n;i++){
23             if(vis[i])continue;
24             vis[i]=1;
25             dfs(x+1,num+1);
26             vis[i]=0;
27         }
28         if(x+1<n&& num<=k)dfs(x+1,num);
29 }
30 int main(){
31     int T,flot=0;
32     scanf("%d",&T);
33     while(T--){
34         memset(vis,0,sizeof(vis));
35         scanf("%d%d",&n,&k);
36         if(k>n){
37             puts("0");continue;
38         }
39         if(n==k){
40             long long t=1;
41             for(int i=1;i<=n;i++)t*=i;
42             printf("%I64d\n",t);
43             continue;
44         }
45         ans=0;
46         dfs(0,0);
47         printf("Case %d: %lld\n",++flot,ans);
48     }
49     return 0;
50 }

 

posted @ 2015-10-24 20:51  handsomecui  阅读(605)  评论(0编辑  收藏  举报