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.
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
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.
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
走到这的我想就不用说题意了。
这个题看上去无从下手,毕竟是要求异或的值,所以我们把数换成2进制之后就会发现特点了。
举个栗子:
如果我们要选择两个数异或,那么最终的值其实就是:
(a^b)+(a^c)+.....。
而对于这个a^b,我们可以分解成(末尾异或*2^0+末尾-1位上异或*2^1.....)+(末尾异或*2^0+末尾-1位上异或*2^1.....)
所以实际上就是(所有末尾异或能出来1的情况总个数*2^0+末尾-1位上能异或出来1的总情况*2^1+........)。
这样我们利用组合数学。
在选择n个数异或的时候,便可以枚举在每一个位上选择奇数个1异或,然后乘上相应的权值,最后加和就是所要求的值。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1000+10;
const int mod=1e6+3;
int sum2[40];
long long c[MAXN][MAXN];
void get_2(long long a)
{
for(int i=0;a;i++)
{
if(a&1)sum2[i]++;
a>>=1;
}
}
void get_c()
{
int i,j;
for(i=0;i<=1000;++i)
for(j=0;j<=i;++j)
{
if(!j||j==i)c[i][j]=1;
else c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
}
int main()
{
int i,j,k;
int n;
get_c();
while(~scanf("%d",&n))
{
int x;
memset(sum2,0,sizeof(sum2));
for(i=1;i<=n;++i)
{
scanf("%d",&x);
get_2(x);
}
for(i=1;i<=n;++i)
{
long long sum=0;
for(j=0;j<=31;++j)
{
if(sum2[j])
{
for(k=1;k<=i;k+=2)
{
if(k>sum2[j])break;
sum+=(c[sum2[j]][k]*c[n-sum2[j]][i-k]%mod*(1ll<<j))%mod;
sum%=mod;
}
}
}
if(i==1)printf("%I64d",sum);
else printf("% I64d", sum);
}
puts("");
}
return 0;
}