uva 11806 容斥原理+二进制

很容易想到要用容斥原理,这里有一个小技巧就是用二进制数来表示集合的交。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 const int MOD = 1000007;
 8 const int N = 501;
 9 int cn[N][N];
10 
11 void init()
12 {
13     memset( cn, 0, sizeof(cn) );
14     cn[0][0] = 1;
15     for ( int i = 1; i < N; i++ )
16     {
17         cn[i][0] = cn[i][i] = 1;
18         for ( int j = 1; j < i; j++ )
19         {
20             cn[i][j] = ( cn[i - 1][j] + cn[i - 1][j - 1] ) % MOD;
21         }
22     }
23 }
24 
25 int main ()
26 {
27     init();
28     int t;
29     cin >> t;
30     for ( int _case = 1; _case <= t; _case++ )
31     {
32         int n, m, k;
33         cin >> n >> m >> k;
34         int ans = cn[n * m][k];
35         for ( int s = 1; s < 16; s++ )
36         {
37             int cnt = 0, r = n, c = m;
38             for ( int i = 0; i < 4; i++ )
39             {
40                 if ( s & ( 1 << i ) )
41                 {
42                     cnt++;
43                     if ( i & 1 )
44                     {
45                         r--;
46                     }
47                     else
48                     {
49                         c--;
50                     }
51                 }
52             }
53             if ( cnt & 1 )
54             {
55                 ans = ( ans - cn[r * c][k] + MOD ) % MOD;
56             }
57             else
58             {
59                 ans = ( ans + cn[r * c][k] ) % MOD;
60             }
61         }
62         cout << "Case " << _case << ": " << ans << endl;
63     }
64     return 0;
65 }

 

转载于:https://www.cnblogs.com/huoxiayu/p/4733687.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值