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
题意:给两个整数n,k和n个整数。分别求出从n个数里挑选所有的k(k分别取值1到n)个进行异或得到的总和,输出n个整数答案。
题目一开始要求明确但是一直想不到能正确快速的求出选数求值的方法,看过题解后是用二进制按位考虑对答案的贡献计算。将给出的N个数转换成二进制,统计所有的数在每一位上1的个数和0的个数。假设当前要求任意取k个数的所有异或和,要对答案有贡献则对应的位上应该为1,由异或的性质可知只有奇数个1时结果才会有1。所以求出对应每一位上的所有可能取法。例如样例的1 2 10 1,二进制为0001,0010,1010,0001。第1位有2个1,第二位有2个1,第三位有0个1,第四位有1个1。当k=2时,第四位要为1的取法有C(3,1)*C(1,1)*(1<<3),第二位的取法有C(2,1)*C(2,1)*(1<<1)。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define M 1000003
const int x=1010;
ll fac[x]={1,1},inv[x]={1,1},f[x]={1,1};
ll C(ll a,ll b)
{
if(b>a) return 0;
return fac[a]*inv[b]%M*inv[a-b]%M;
}
void init()
{
for(int i=2;i<x;i++){
fac[i]=fac[i-1]*i%M;
f[i]=(M-M/i)*f[M%i]%M;
inv[i]=inv[i-1]*f[i]%M;
}
}
int n;
int b[100];
int main()
{
init();
while(cin>>n){
int ai;
memset(b,0,sizeof b);
for(int i=1;i<=n;i++){
cin>>ai;
for(int j=1;j<=32&&ai;j++,ai>>=1){
if(ai&1){
b[j]++;
}
}
}
ll sum=0;
for(int i=1;i<=n;i++){
sum=0;
for(int j=1;j<=32;j++){
for(int k=1;k<=i&&k<=b[j];k+=2){
sum=(((C(b[j],k)*C(n-b[j],i-k)%M)*(1<<(j-1))%M)+sum)%M;
}
}
if(i==n){
cout<<sum%M;
}
else cout<<sum%M<<" ";
}
cout<<endl;
}
return 0;
}