首页 > 代码库 > HDU 4810 Wall Painting(组合数学)

HDU 4810 Wall Painting(组合数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4810

思路:先把每个数字按位分离出来,存放1的个数,那么每位0的个数为n - 1的个数,然后利用组合数学和异或的原理,枚举奇数个1的情况,然后利用乘法和加法计数原理累加出来的就是该位的答案,最后乘上改为对应的数值最后加起来就是答案

代码:

#include <stdio.h>
#include <string.h>

const __int64 MOD = 1000003;
const int N = 1005;
int n, num, sum[32];
__int64 C[N][N], mi[32];

void tra(int num) {
	int sn = 0;
	while (num) {
		sum[sn++] += num % 2;
		num /= 2;
 	}
}

__int64 cal(int day) {
	__int64 ans = 0;
	for (int i = 0; i < 32; i++) {
		for (int j = 1; j <= day && j <= sum[i]; j += 2) {
			if (sum[i] < j || n - sum[i] < day - j) continue;
			ans = (ans + C[sum[i]][j] * C[n - sum[i]][day - j] % MOD * mi[i] % MOD) % MOD;
  		}
 	}
 	return ans;
}

int main() {
	mi[0] = 1;
	for (int i = 1; i < 32; i++)
		mi[i] = mi[i - 1] * 2;
	for (int i = 0; i < N; i++) {
		C[i][0] = C[i][i] = 1;
		for (int j = 1; j < i; j++)
			C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
 	}
	while (~scanf("%d", &n)) {
		memset(sum, 0, sizeof(sum));
  		for (int i = 0; i < n; i++) {
			scanf("%d", &num);
			tra(num);
  		}
  		for (int i = 1; i < n; i++) {
  			printf("%I64d ", cal(i));
    	}
    	printf("%I64d\n", cal(n));
 	}
	return 0;
}