链接:戳这里
Wall Painting
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
4
1 2 10 1
Sample Output
14 36 30 8
题意:
n种颜色,选出k种然后抑或和,求所有选出k种颜色的情况的总和
思路:
每种颜色的权值映射在二进制,n个数抑或和对应在二进制上出现偶数次就为0,奇数次为(1LL<<i)
那么我们每次选取k个数,也就是在每个二进制num不为0的位置上,选出奇数个数量来组成抑或和
假设当前是n个数,我要选2^3,并且(1LL<<3)位置上对应有num个数ai
所以贡献就是ans+=(1LL<<3)*C(num,2*j+1)*C(n-2*j+1,k-j) (j>=0,奇数),
当前num中取出j(奇数)个(1LL<<3)来,另外的k-j个数任意选,这就是8对于总值的贡献
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
const ll mod=1e6+3;
int n;
ll a[1010];
int num[65];
ll C[1010][1010];
int main(){
for(int i=0;i<=1000;i++) C[i][0]=1LL;
for(int i=1;i<=1000;i++){
for(int j=1;j<=i;j++){
C[i][j]=(C[i-1][j]%mod+C[i-1][j-1]%mod)%mod;
}
}
while(scanf("%d",&n)!=EOF){
mst(num,0);
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
for(int i=1;i<=n;i++){
for(int j=0;j<64;j++){
if((a[i]&(1LL<<j))) num[j]++;
}
}
for(int k=1;k<=n;k++){
ll ans=0;
for(int i=0;i<64;i++){
for(int j=1;j<=min(k,num[i]);j+=2){
ans=(ans+(1LL<<i)*C[num[i]][j]%mod*C[n-num[i]][k-j]%mod+mod)%mod;
}
}
if(k>1) printf(" ");
printf("%I64d",(ans%mod+mod)%mod);
}
printf("\n");
}
return 0;
}