【HDU 4810】组合数学 Wall painting

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.

    又是组合数学……这道题已经是作业里的最后一道了,自然会有些难度。

    我们要求这些数异或的结果,由于异或有一些非常特殊的性质,像是交换律、固定位上交换不影响结果之类的。我们正好可以利用这个性质。如果真的如题目所说去模拟,自然会有n*2^n的超高复杂度,我们便可以利用异或的性质来解决。记录每一个二进制位上1的个数,只要选择的数在这一位1的总和为奇数,结果就为1,不需考虑顺序。同样,偶数则为0。可是出现奇数可能会有多种情况,比如有7个1,那么可能选中了1个1、3个1、5个1、7个1,那么就加一层循环枚举。整理一下,设这一位是2^i,有m个1,今天为第j天,选中了k个1(k为奇数且小于等于j、m),则方案数为C(m,k)*C(n-m,j-k)。而算出这一位的数值还需要乘上2^i,算算时间复杂度O(n^2)(i层循环最多有32,可视为常数)。当然要先把组合数表打出来,而且模数为1000003,计算过程中最多会出现1000002^2=1000004000004超过了INT_MAX,必须用long long存储(我就没有用然后尴尬的WA掉了QAQ,看来组合数学大多都要用long long啊...)。然后是代码。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define N 1005
#define mod 1000003
#define M 70
#define ll long long
int n,num[M];
ll C[N][N];
int main()
{
	C[0][0]=1;
	for(int i=0;i<=1000;i++)
	{
		for(int j=0;j<=1000;j++)
		{
			C[i+1][j]=(C[i+1][j]+C[i][j])%mod;
			C[i+1][j+1]=(C[i+1][j+1]+C[i][j])%mod;
		}
	}
	while(scanf("%d",&n)==1)
	{
		memset(num,0,sizeof(num));
		for(int i=1;i<=n;i++)
		{
			ll a;
			scanf("%lld",&a);
			for(int j=1;a;j++)
			{
				num[j]+=a&1;
				a>>=1;
			}
		}
		for(int i=1;i<=n;i++)
		{
			ll ans=0;
			for(int j=1;j<=32;j++)
				for(int k=1;k<=num[j]&&k<=i;k+=2)
					ans=(C[num[j]][k]*C[n-num[j]][i-k]%mod*(1ll<<(j-1))%mod+ans)%mod;
			if(i!=1)
				printf(" ");
			printf("%lld",ans);
		}
		printf("\n");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值