hdu 3929 Big Coefficients 解题报告 <容斥原理>


链接: http://acm.hdu.edu.cn/showproblem.php?pid=3929


Problem Description
F(x) is a polynomial in x with integer coefficients, here F(x) = (1+x)^a1 + (1+x)^a2 + ... + (1+x)^am. Given a1, a2, ... , am, find number of odd coefficients of F(x).
 


Input
The first line contains a single positive integer T( T <= 10000 ), indicates the number of test cases.
For each test case:
First line contains an integer N(1 <= N <= 15).
Second line contains N integers a1, a2, ..., am ( 0 <= ai <= 2^45 )
 


Output
For each test case: output the case number as shown and an the odd coefficients of F(x).
 


Sample Input
4 1 1 1 3 2 1 3 3 1 2 3
 


Sample Output
Case #1: 2 Case #2: 4 Case #3: 2 Case #4: 2
Hint
Case #3: (1+x) + (1+x)^3 = 2 + 4x + 3x^2 + x^3. it contains 2 odd coefficients.
Case #4: (1+x) + (1+x)^2 + (1+x)^3 = 3 + 6x + 4x^2 + x^3. it contains 2 odd coefficients.
 题意:求F( x ) 的奇系数个数
利用容斥定理 
 1 容斥原理有一般有简单的递归式
2
3 dfs(int beg,set S,int sym)
4
5 {
6
7 ans+=num(S)*sym;
8
9 for(int i=beg;i<=n;i++)
10
11 dfs(i,S∩A[i],sym*-1);
12
13 }
14
15 for(int i=1;i<=n;i++)
16
17 dfs(i,A[i],1);
代码
 1 #include <stdio.h>
2 typedef __int64 ll;
3 ll a[20], ans;
4 int N;
5 int get( ll x )
6 {
7 int t=0;
8 while( x )
9 {
10 x -= (x & -x);
11 t++ ;
12 }
13 return t;
14 }
15 void dfs( int b, ll n, ll s )
16 {
17 ans += s*( 1<<get( n ));
18 for( int i=b+1; i<N; ++ i )
19 {
20 dfs( i, a[i]&n, -2*s );
21 }
22 }
23 int main( )
24 {
25 int T, t=0;
26 scanf( "%d", &T );
27 while( T-- )
28 {
29 scanf( "%d", &N );
30 for( int i=0; i<N; ++ i )
31 {
32 scanf( "%I64d", a+i );
33 }
34 ans=0;
35 for( int i=0; i<N; ++ i )
36 dfs( i, a[i], 1 );
37 printf("Case #%d: %I64d\n", ++t, ans);
38 }
39 return 0;
40 }

你可能感兴趣的:(HDU)