hdoj4810Wall Painting【组合数学+位运算】

来源:互联网 发布:网络造字 编辑:程序博客网 时间:2024/05/21 23:34

Wall Painting

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2226    Accepted Submission(s): 698


Problem Description
Ms.Fang loves painting very much. She paints GFW(Great Funny Wall) every day. Every day before painting, she produces a wonderful color of pigments by mixing water and some bags of pigments. On the K-th day, she will select K specific bags of pigments and mix them to get a color of pigments which she will use that day. When she mixes a bag of pigments with color A and a bag of pigments with color B, she will get pigments with color A xor B.
When she mixes two bags of pigments with the same color, she will get color zero for some strange reasons. Now, her husband Mr.Fang has no idea about which K bags of pigments Ms.Fang will select on the K-th day. He wonders the sum of the colors Ms.Fang will get with different plans.

For example, assume n = 3, K = 2 and three bags of pigments with color 2, 1, 2. She can get color 3, 3, 0 with 3 different plans. In this instance, the answer Mr.Fang wants to get on the second day is 3 + 3 + 0 = 6.
Mr.Fang is so busy that he doesn’t want to spend too much time on it. Can you help him?
You should tell Mr.Fang the answer from the first day to the n-th day.
 

Input
There are several test cases, please process till EOF.
For each test case, the first line contains a single integer N(1 <= N <= 103).The second line contains N integers. The i-th integer represents the color of the pigments in the i-th bag.
 

Output
For each test case, output N integers in a line representing the answers(mod 106 +3) from the first day to the n-th day.
 

Sample Input
41 2 10 1
 

Sample Output
14 36 30 8
 

Source
2013ACM/ICPC亚洲区南京站现场赛——题目重现
 

题意:有n个数,从中选k个数共有C(n,k)种选择将每种选择出的k个数异或得到的数相加输出。

思路;将每个数分解为二进制。考虑这个二进制的为1的位置能否保留。k个数异或当这k个数的二进制同一位为1的个数为奇数是这个1可以保留。因此可以枚举每一位为1的的位置处1的个数的奇数。则此时所得的结果即为C[bit[j]][i]*C[n-bit[j]][k-i]*(2^j);即从二进制第j为1的个数为bit[j]中选出i个数再从剩下的不为1的个数中选k-i个数再乘以该该位的权值即可。

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<list>#include<vector>#define MOD 1000003using namespace std;int bit[33];long long C[1010][1010];void init(){for(int i=0;i<1010;++i){C[i][i]=C[i][0]=1;}for(int i=2;i<1010;++i){for(int j=1;j<i;++j){C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;}}}int main(){init();int n,i,j,k;while(scanf("%d",&n)!=EOF){memset(bit,0,sizeof(bit));int cnt,num=0;for(i=0;i<n;++i){scanf("%d",&k);cnt=0;while(k){bit[cnt++]+=k&1;k>>=1;}num=max(num,cnt);}for(i=1;i<=n;++i){long long ans=0;for(j=0;j<num;++j){if(bit[j]){for(k=1;k<=i&&k<=bit[j];k+=2){if(n-bit[j]>=i-k){ans=(ans+(((C[bit[j]][k]*C[n-bit[j]][i-k])%MOD)*(1<<j))%MOD)%MOD;}}}}printf(i==n?"%d\n":"%d ",ans);}}return 0;}


0 0
原创粉丝点击