题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4810
Wall Painting
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1267 Accepted Submission(s): 362
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.
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.
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
Source
思路:这题刚开始的思路 用dp[i][j]表示第i个数到第j个数异或和,预先处理,然后爆搜,==!写了好久发现,dfs没法写;然后。。。。挂机
看了题解:位操作+组合取数
每个数的位数不超过32为,每当输入一个数x,我们就把这个数转化为二进制,在转化为二进制的过程中,用一个a[]数组记录32位中每位含1的个数;那么可想而知每位含0的个数就是n-a[];
接下来:在第k天,从n个数里面取k个数求异或之和;
这就可以转化为: 从第一位遍历到第32位,然后每位求 奇数个1的取法数即可,最后累加;
详细看代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
typedef long long ll;
const int mod=1e6+3;
using namespace std;
int a[35];
ll c[1100][1100];
ll ans[1100];
void Init() //预处理组合数
{
c[0][0]=1;
for(int i=1;i<=1001;i++)
{
c[i][0]=1;
for(int j=1;j<i;j++)
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
c[i][i]=1;
}
}
void change(int x)
{
int k=0;
while(x)
{
if(x&1) a[k]++;
x/=2;
k++;
}
}
int main()
{
int n;
Init();
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
int temp;
scanf("%d",&temp);
change(temp);
}
for(int i=1;i<=n;i++)
for(int j=0;j<32;j++)
for(int k=1;k<=i;k+=2)
{
ans[i]+=((c[a[j]][k]*c[n-a[j]][i-k])%mod*(1<<j))%mod;
ans[i]%=mod;
}
for(int i=1;i<=n;i++)
if(i==1)printf("%I64d",ans[i]);
else printf(" %I64d",ans[i]);
printf("\n");
}
return 0;
}