#異或# HDU - 4810 Wall Painting
Problem Description
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 <= 103).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 106 +3) from the first day to the n-th day.
Sample Input
4
1 2 10 1
Sample Output
14 36 30 8
Description:
給出 N 和 k,第 k(1 <= k <= N) 天從 n 個數箇中任意選擇 k 個數進行異或,再把所有可能的異或值進行求和,依次輸出。
Solution:
異或的性質:對於每一個二進位制位只有當1的個數為奇數時,異或後該位才能為1(1 ^ 1 = 0,1 ^ 0 = 1)。
(參考部落格:https://blog.csdn.net/Jasmineaha/article/details/81412711)
因此,我們可以列舉所有二進位制位,計算出每個第 j 位上1的個數 bit[j],列舉 1~k 之間所有的奇數(1,3,5,7……),利用組合數
例如對於一些二進位制數,第0位上一共有4個1,第1位上一共有4個1,第2位上一共有0個1,第3位上一共有3個1,如下所示:
3 2 1 0 (二進位制位)
3 0 4 4 (1的個數)
結果就是把最後的每一位上的數轉換出來 ans1 = 3pow(2,3) + 0pow(2,2)+ 4pow(2,1) + 4pow(2,0) = 36.
那麼這個過程轉換為公式就是:
ans[ i ] += (C[ bit[j] ][ k ] * C[ n - bit[j] ][ i - k ] ) * ( 1 << j )
ans[ i ] 代表選取 i 個數的最後結果;C[ bit[j] ][ k ] 為在 bit[j] 個1中選取k個1,其中 k 為奇數;C[ n - bit[j] ][ i - k ] 為在 n-bit[j] 個0中選取 i-k 個0;
他們的乘積就是每一位上可能的組合的數目,然後再乘這一位上的值(1 << j)就是這一位上的結果,然後把每一位上的加起來就行了。
Code:
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define fi first
#define se second
#define fopen freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int Mod = 1e6 + 3;
const int MaxN = 1e3 + 5;
int c[MaxN][MaxN];
int a[MaxN], bit[MaxN];
int n;
void get_C() {
mst(c, 0);
c[0][0] = 1;
for(int i = 1; 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;
}
}
void get_bit() {
mst(bit, 0);
for(int i = 1; i <= n; i++) {
for(int j = 0; j < 32; j++) {
if(a[i] & (1 << j)) bit[j]++;
}
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
get_C();
while(cin >> n) {
for(int i = 1; i <= n; i++) cin >> a[i];
get_bit();
for(int i = 1; i <= n; i++) {
LL ans = 0LL;
for(int j = 0; j < 32; j++) {
for(int k = 1; k <= bit[j] && k <= i; k += 2) {
ans += ((LL)c[bit[j]][k] * c[n-bit[j]][i-k]) % Mod * ((1 << j) % Mod);
ans %= Mod;
}
}
i == n ? cout << ans % Mod << endl : cout << ans % Mod << " ";
}
}
return 0;
}