HDU4810_Wall Painting

题目很简单。

给你n个数,输出n个答案,第i个答案表示从n个数里取遍i个数的异或值的和。

其实每一个数最多也就32位,把所有的数分解,保存每一位总共有多少个1,最后要是这一位的异或结果为1,那么在所有的异或数中,这一位为1的数必须是有奇数个,在求解的时候就是求组合数的情况就可以了。

直接水过。

#include <iostream>
#include <cstdio>
#include <cstring>
#define M 1000003
#define maxn 1005
typedef long long ll;
using namespace std;

int a[53];
int ans,n,m,k,c[maxn][maxn],u[53];
ll tep;

void insert(int x)
{
    for (int cur=1; x; cur++,x>>=1) a[cur]+=x&1;
}

void init_c()
{
    u[1]=1;
    for (int i=2; i<=52; i++) u[i]=(u[i-1]+u[i-1])%M;
    c[0][0]=1;
    c[1][0]=c[1][1]=1;
    for (int i=2; i<maxn; i++)
    {
        c[i][0]=1;
        for (int j=1; j<=i; j++)
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%M;
    }
}

int main()
{
    init_c();
    while (scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof a);
        for (int i=1; i<=n; i++) scanf("%d",&k),insert(k);
        for (int i=1; i<=n; i++)
        {
            ans=0;
            for (int j=1; j<=32; j++)//每一位为1的情况
            {
                for (k=1; k<=a[j] && k<=i; k+=2)
                {
                    if (i-k>n-a[j]) continue;
                    tep=(ll)c[a[j]][k]*c[n-a[j]][i-k];
                    tep%=M;
                    tep=(tep*u[j])%M;
                    ans+=tep;
                    if (ans>=M) ans-=M;
                }
            }
            if (i>1) printf(" ");
            printf("%d",ans);
        }
        printf("
");
    }
    return 0;
}
如有转载,请注明出处(http://www.cnblogs.com/lochan)
原文地址:https://www.cnblogs.com/lochan/p/3451788.html