spoj-ASSIGN-bitDP

ASSIGN - Assignments

 

Problem

Your task will be to calculate number of different assignments of n different topics to n students such that everybody gets exactly one topic he likes.

Input

First line of input contains number of test cases c (1<=c<=80). Each test case begins with number of students n (1<=n<=20). Each of the next n lines contains n integers describing preferences of one student. 1 at the ith position means that this student likes ith topic, 0 means that he definitely doesn't want to take it.

Output

For each test case output number of different assignments (it will fit in a signed 64-bit integer).

Example

Input:
3
3
1 1 1
1 1 1
1 1 1
11
1 0 0 1 0 0 0 0 0 1 1 
1 1 1 1 1 0 1 0 1 0 0 
1 0 0 1 0 0 1 1 0 1 0 
1 0 1 1 1 0 1 1 0 1 1 
0 1 1 1 0 1 0 0 1 1 1 
1 1 1 0 0 1 0 0 0 0 0 
0 0 0 0 1 0 1 0 0 0 1 
1 0 1 1 0 0 0 0 0 0 1 
0 0 1 0 1 1 0 0 0 1 1 
1 1 1 0 0 0 1 0 1 0 1 
1 0 0 0 1 1 1 1 0 0 0 
11
0 1 1 1 0 1 0 0 0 1 0 
0 0 1 1 1 1 1 1 1 1 1 
1 1 0 1 0 0 0 0 0 1 0 
0 1 0 1 0 1 0 1 0 1 1 
1 0 0 1 0 0 0 0 1 0 1 
0 0 1 0 1 1 0 0 0 0 1 
1 0 1 0 1 1 1 0 1 1 0 
1 0 1 1 0 1 1 0 0 1 0 
0 0 1 1 0 1 1 1 1 1 1 
0 1 0 0 0 0 0 0 0 1 1 
0 1 1 0 0 0 0 0 1 0 1 

Output:
6
7588
7426

 
                           利用二进制表示集合,f[S][i]表示i个人形成S状态的座位的方案个数,答案就是 f[all][n],  注意爆int。
     
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define inf 0x3f3f3f3f
 4 long long  f[2][(1<<20)+10];
 5 int e[21][21];
 6 int main()
 7 {
 8     int t,n,m,i,j,k;
 9     cin>>t;
10     while(t--){
11         cin>>n;
12         for(i=1;i<=n;++i) 
13         for(j=1;j<=n;++j) cin>>e[i][j];
14         memset(f,0,sizeof(f));
15         f[0][0]=1;
16         int cur=0;
17         for(i=1;i<=n;++i){
18             cur^=1;
19             memset(f[cur],0,sizeof(f[cur]));
20             for(j=0;j<(1<<n);++j){
21                 if(!f[cur^1][j]) continue;
22                 for(k=1;k<=n;++k){
23                 if(e[i][k]&& ((1<<(k-1))&j)==0) {
24                     f[cur][j|(1<<(k-1))]+=f[cur^1][j];
25                 }
26             }
27             }
28             /*for(j=0;j<(1<<n);++j){
29                 cout<<j<<' '<<f[cur][j]<<endl;
30             }*/
31         }
32         cout<<f[cur][(1<<n)-1]<<endl;
33     }
34     return 0;
35 }

 


转载于:https://www.cnblogs.com/zzqc/p/8795946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值