[位运算]HDU4810 Wall Painting

1.题目:

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 <= 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.题意:

给出N个数字,求从中分别任意取i个数字(i=1,2,3,…,N)产生的所有亦或和之和。例如样例中,1,2,10,1,取1个数字时,能够产生的亦或和分别是1,2,10,1,总和是14;取2个数字时,能够取(1,2),(1,10),(1,1),(2,10),(2,1),(10,1)六个组合,他们的亦或和分别是3,11,0,8,3,11,总和是36;取3个和4个数字时,同理如上。最后输出14,36,30,8分别是取i个数字的亦或和之和

3.思路:

组合数和位运算的运用
第一步:我们可以将一个数字转化为30位的二进制,根据亦或和的性质,也就是根据每一位上的1的累计值来判断结果在该位上是否为1。
如果一些数字在第j位上1的累计数是奇数,则亦或计算之后该位为1;
反之,如果一些数字在第j位上1的累计数是偶数,则亦或计算之后该位为0

第二步:在计算亦或和总和时,由于累计数是偶数时会相互抵消,所以只需要计算累计数是奇数时的情况,这时需要运用组合数的方法:假设所有数字在第j位上的累计数是num[j],在取i个数字时,则亦或和总数中2^j的出现次数为这里写图片描述,及最后的总和为这里写图片描述

TIP:其中组合数可以利用打表的方式进行,利用这里写图片描述来进行递推

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define maxn 1010
#define ll long long
const ll MOD=1e6+3;
using namespace std;

ll X[maxn];
ll arr[maxn];
ll ans[maxn];
ll C[maxn][maxn];

int main()
{
    int n;
    C[0][0]=1;
    for(int i=1;i<=1000;i++){
        for(int j=0;j<=i;j++){
            if(j == 0) C[j][i]=1;
            else C[j][i] = (C[j][i-1]+C[j-1][i-1])%MOD;
        }
    }

    while(scanf("%d",&n)!=EOF){
        memset(arr,0,sizeof(arr));
        for(int i=1;i<=n;i++){
            scanf("%d",&X[i]);
            for(int j=0;j<=30;j++){
                if((X[i]>>j)&1)
                    arr[j] ++;
            }
        }

        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++){
            for(int j=0;j<=30;j++){
                for(int k=1;k<=min(arr[j],i);k+=2){
                    ans[i]=(ans[i]+(ll)(1<<j)%MOD*(ll)C[k][arr[j]]%MOD*(ll)C[i-k][n-arr[j]]%MOD)%MOD;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(i==n) printf("%d\n",ans[i]);
            else printf("%d ",ans[i]);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值