Wall Painting HDU - 4810(按位算贡献)

题目

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 <= 10 3).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 10 6 +3) from the first day to the n-th day.
Sample Input
4
1 2 10 1
Sample Output
14 36 30 8

解释

把每个数都按照二进制写出来,可以将问题转化为每个数对应二进制上的运算问题,求出这一位结果再转成十进制相加。
若能统计每个位数上的1、0个数,则对于C(n,i)的情况下,由于位运算,奇数个1方能对位数产生贡献,相当于是j为奇数,且小于等于i,然后剩下从0里面取i-j个0,双方组合数相乘就是这一位上的结果。
然后i++,循环这个过程累加得到答案。
组合数通过打标O(1)访问。

#include <cstdio>
#include <cstring>
#define ll long long
long long const mod =1e6+3;
ll C[1002][1002];
void cal(){
	C[0][0] = C[1][0] = C[1][1]= 1;
	C[2][0] = C[2][2] = 1;
	C[2][1] = 2;
	for(int i = 3; i <= 1000; i++){
		C[i][0] = C[i][i] = 1;
		for(int j = 1; j < i; j++)
			C[i][j] = (C[i-1][j]+C[i-1][j-1])%mod;
	}
}
int pos[35][2];
void get(int x){
	int k = 0;
	int i = 0;
	while(x){
		if(x%2 == 1)
			pos[k++][1]++;
		else
			pos[k++][0]++;
		x /=2;
		i++;
	}
	while(i <= 34)
		pos[i++][0]++; 
}
int main(){
	int n;
	int ch;
	cal();
	while(~scanf("%d", &n)){
		memset(pos, 0, sizeof(pos));
		for (int i = 0; i < n; i++){
			scanf("%d", &ch);
			get(ch);
		}	
		
		ll ans;
		int flag = 1;
		for (int i = 1; i <= n; i++){
			ans = 0;
			ll dig = 1;
			for(int k = 0; k <= 34; k++){		
				for (int j = 1; (j <= pos[k][1] && j <= i); j += 2){
					ans =((ll)(C[pos[k][1]][j]*C[pos[k][0]][i-j]%mod*dig%mod)%mod+ans)%mod;		
				}
				dig *= 2;
			}
			if(flag == 1){
				printf("%lld", ans);
				flag = 0;
			}
			else
				printf(" %lld", ans);
		}
		printf("\n");
	}
	return 0;
}
发布了52 篇原创文章 · 获赞 2 · 访问量 829

猜你喜欢

转载自blog.csdn.net/qq_44714572/article/details/103956533