HDU - 4810 Wall Painting【组合数+二进制拆分】

版权声明:转载什么的好说,附上友链就ojek了,同为咸鱼,一起学习。 https://blog.csdn.net/sodacoco/article/details/86497577

参看资料:

https://blog.csdn.net/xiaoyizhan139/article/details/82831533


题目:

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

 题目大意:

       给定n包颜料,第 i 天选取 i 种颜料,使这 i 种颜料相异或,求这一天所有可能的组合 异或后 的 和;

       输出从1到n天的结果;

解题思路:

       一开始没有看明白题意,后来明白题意了,一直想着分组暴力求解,想想实现不出来,,看了题解之后,了解了一种神奇的解题姿势,二进制拆分:以样例为例:

       二进制表示形式:

              1:  0 0 0 1

              2:  0 0 1 0

              10:1 0 1 0

              1:     0 0 0 1

       在第一位(就是二进制表示1的那一位)记录下来num[1]=2;

           第二位  num[2]=2;

           第三位  num[3]=0;

           第四位  num[4]=1;

       我们可以看出这四个是最大的是10,二进制位为4位。

       我们计算 1个数异或 和 就等于这四个数的和,我们也可以这样想:

       在第1位上是1的我们可以挑一个,在第1位是0的不挑这样就是四个数第1位1个数的异或和:

                                                 c(2,1)*c(2,0)*(1<<(1-1));

       第二位同样我们可以这样计算 :c(2,1)*c(2,0)*(1<<(2-1));

       第三位同样我们可以这样计算 :c(0,1)*c(4,0)*(1<<(3-1));、

       第四位同样我们可以这样计算 :c(1,1)*c(3,0)*(1<<(4-1));

       这四个结果加和即为一个数异或和的答案。

       而两个数异或和的答案就略微推广一点。很容易得知:因为我们要挑两个数,两个1或两个0异或和都是0;所以我们只有选择1个1和1个0;

       在第一位上这样计算:c(2,1)*c(2,1)*(1<<(1-1));

       第二位同样我们可以这样计算 :c(2,1)*c(2,1)*(1<<(2-1));

       第三位同样我们可以这样计算 :c(0,1)*c(4,1)*(1<<(3-1));、

       第四位同样我们可以这样计算 :c(1,1)*c(3,1)*(1<<(4-1));

       这四个结果和即为两个数异或和的答案。

       由此推广到n个数的异或和。

       因为数是int型,最大也就32位。所以时间复杂度为O(n*32*n/2);很快就算出答案。

代码实现:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define inf 0x3f3f3f3f
#define maxn 1100
#define MOD 1000003
using namespace std;
int N;
long long rem[32],c[maxn][maxn];
//求1000以内的组合数,预处理
void init() {
    c[0][0] = 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]) % MOD;
    }
    return ;
}
int main(){
    init();
    while(~scanf("%d",&N)){
        memset(rem,0,sizeof(rem));
        for(int i=0;i<N;i++){
            int k;
            scanf("%d",&k);
            for(int j=0;j<32;j++)//判断k的每一位是否为1
                if( (1<<j)& k ) rem[j]++;
        }

        for(int i=1;i<=N;i++){//n天累加
            long long ans=0;
            for(int j=0;j<32;j++){  //对于每一位上二进制累加
                for(int k=1;k<=rem[j] && k<=i;k+=2)//k+=2,找奇数个1,1,3,5....
                ans=(ans+ ( c[N-rem[j]][i-k]* (c[rem[j]][k]* ( (1<<j)%MOD) )%MOD)%MOD)%MOD;
            }
            if(i==N) printf("%I64d\n",ans);
            else     printf("%I64d ",ans);
        }
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sodacoco/article/details/86497577