1. 程式人生 > >[HDU 4810] Wall Painting 組合數 按位處理

[HDU 4810] Wall Painting 組合數 按位處理

Wall Painting

Time Limit: 5000ms Memory Limit: 32MB

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 C(N,K) 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個數,對所有1<=i<=N,求從N個數中選擇i個數異或起來的所有方案異或值累和。

解題報告

很少做數學題,寒假回來神犇就送了五道數論要求一次AC的考試大禮包,果斷爆0。
感覺數學太差了,所以寫點數學的部落格。。。。

考慮選i個的情況,首先發現如果要異或的話,二進位制下單個數位位之間不影響,可以對二進位制每一位分別考慮,問題就簡單多了。
考慮第K位,加入N個數中第K位為1的數有cnt[K]個,因為第K位為零的數不產生任何影響,第K位為一的數選中個數為奇數個才不為0,所以列舉選取第K位為一的數j個,j=1,3,5….(j<=cnt[K] && j<=i)。
這種情況下
選擇第K位為零的數的方案數為C(N-j, i-j)
選擇第K位為一的數的方案數為C(cnt[K], j)
那麼總方案數就是他們的乘積,對sum的貢獻為

C(Nj,ij)C(cnt[K],j)2k1
然後就發現時間複雜度為O(N2)加上一個32的常數還有多組測試資料,5秒有點懸啊
別管那麼多啦反正大力出奇跡就對啦0.0

程式碼

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <climits>

typedef long long LL;
const int MAXN = 1100;
const int MOD = 1000003;

LL C[MAXN][MAXN];

int nums[MAXN];
int sum[40];

int main () {

    C[0][0] = 1;
    for(int i = 1; i<1100; i++)
        for(int j = 1; j<1100; j++)
            C[i][j] = (C[i-1][j-1]+C[i-1][j]) % MOD;
    C[0][0] = 0;
    int n;

    while(~scanf("%d", &n)) {
        memset(sum, 0, sizeof sum);
        for(int i = 1; i<=n; i++)
            scanf("%d", &nums[i]);
        for(int i = 1; i<=n; i++)
            for(int j = 1; j<=31; j++)
                sum[j] += ((nums[i] & (1<<(j-1)))!=0);
        LL ans = 0;
        for(int i = 1; i<=n; i++) {
            ans = 0;
            for(int k = 1; k <= 31; k++)
                for(int j = 1; j<=sum[k] && j <= i; j+=2)
                    ans += (LL)(1LL<<((LL)k-1LL))%MOD*(LL)C[sum[k]+1][j+1]%MOD
                    * C[n-sum[k]+1][i-j+1] % MOD, ans %= MOD;
            printf("%I64d%c", ans, i == n ? '\n' : ' ');
        }
    }
}