Wall Painting
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1265 Accepted Submission(s): 360
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.
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.
題解及代碼:
拿到題目的時候一直在想有什麼組合的方法,或者是DP之類的可以由兩個數推導到三個數等等,想了好一會也沒什麼思路。
之後想到異或的性質,因爲最後的答案是要相加的,所以異或之後的每一位想要有值的話,那麼我們必須保證進行異或的數字在當前位上的1個個數必須是奇數,想到這裏,思路就有了。我們只需要求出每個數二進制表示形勢下每一位1和0的個數,那麼然後在求組合數就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const long long mod=1e6+3;
long long C[1010][1010];
long long l[70],r[70];
long long num[70];
long long sum=0;
void init()
{
C[0][0]=1;
C[1][0]=C[1][1]=1;C[1][2]=0;
for(int i=2;i<=1000;i++)
{
C[i][0]=1;
for(int j=1;j<=i;j++)
C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
C[i][i+1]=0;
}
num[0]=1;num[1]=2;
for(int i=2;i<=62;i++)
{
num[i]=(num[i-1]*2)%mod;
}
}
int main()
{
init();
int n,color,k;
while(scanf("%d",&n)!=EOF)
{
sum=0;
memset(l,0,sizeof(l));
for(int i=1;i<=n;i++)
{
scanf("%d",&color);
sum+=color;
sum%=mod;
k=0;
while(color)
{
l[k++]+=(color&1);
color/=2;
}
}
printf("%I64d",sum);
for(int i=0;i<=62;i++)
{
r[i]=n-l[i];
}
for(int i=2;i<=n;i++)
{
sum=0;
for(int j=0;j<=62;j++)
{
if(l[j])
for(k=1;k<=i&&k<=l[j];k+=2)
{
//printf("%d %d %d,%d %d %d,%d \n",l[j],k,C[l[j]][k],r[j],n-k,C[r[j]][n-k],num[j]);
sum+=(C[l[j]][k]*C[r[j]][i-k]%mod*num[j])%mod;
sum%=mod;
}
}
printf(" %I64d",sum);
}
puts("");
}
return 0;
}