Codeforces Round #257 (Div. 1) (Codeforces 449D)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   

思路:定義f(x)為 Ai & x==x  的個數,g(x)為x表示為二進位時1的個數,最後答案為    。為什麼會等於這個呢:運用容斥的思想,如果 我們假設 ai&x==x 有f(x)個,那麼 這f(x)個 組成集合的子集 & 出來是 >=x那麼我們要扣掉>x的 。。。  因為這裡我們要求的是 & 之後等於0 一開始1個數為0那麼就是 1個數為偶數時加上去,  為奇數時減掉了。

 那麼就剩下求f(x)    。我們把A[i]和x的二進位 分成  前 (20-k)位和  後 k 位時  X1表示A[i]前20-k位 ,X2表示A[i]後k位, Y1表示x前20-k 位,Y2表示A[i]後k位。

 d[k][x] 表示X1==Y1  &&  (X2&Y2==Y2) 那麼 d[k][x]轉移就能O(1) 轉移  ,如下:

當(x&(1<<(k-1)))==1時 d[k][x]=d[k-1][x],   當(x&(1<<(k-1)))==0時d[k][x]=d[k-1][x]+d[k-1][x+(1<<(k-1)];

 

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include <iostream>#define N 1050000#define LL __int64#define MOD 1000000007using namespace std;int f[22][N];int a[N];LL qmod(LL a, LL b) {    LL res = 1;    while (b) {        if (b & 1)            res = (res * a) % MOD;        a = (a * a) % MOD;        b >>= 1;    }    return res;}int cal(int i) {    int flag = 0;    int res = (int) qmod(2, f[20][i]);    while (i) {        if (i & 1)            flag++;        i >>= 1;    }    if (flag & 1)        return -res;    else        return res;}int main() {    int n;    scanf("%d", &n);    int w = (1 << 20);    for (int i = 1; i <= n; ++i) {        scanf("%d", &a[i]);        f[0][a[i]]++;    }    for (int k = 1; k <= 20; ++k) {        for (int i = 0; i < w; ++i) {            if (i & (1 << (k - 1))) {                f[k][i] = f[k - 1][i];            } else {                if (i + (1 << (k - 1)) < w)                    f[k][i] = (f[k - 1][i] + f[k - 1][i + (1 << (k - 1))])                            % (MOD - 1);                else                    f[k][i] = f[k - 1][i];            }        }    }    int ans = 0;    for (int i = 0; i < w; ++i) {        ans = (ans + cal(i)) % MOD;    }    printf("%d\n", (ans+MOD)%MOD);    return 0;}
View Code

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.