zoukankan      html  css  js  c++  java
  • 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 }

  • 相关阅读:
    【NOI2008】志愿者招募
    【2010国家集训队】人员雇佣
    html5手机移动端三级联动城市选择器
    WebSocket实现简单的在线聊天
    游戏开发完整学习路线(各个版本都有)
    vs下开发windows服务程序
    解决Firefox下,页面元素不刷新问题
    C# JObject和JArray 的分享
    jQuery如何改变css伪元素样式
    safari 浏览器window.history.go(-1)运行无效解决办法
  • 原文地址:https://www.cnblogs.com/zzqc/p/8795946.html
Copyright © 2011-2022 走看看