[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' : ' ');
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章