codeforces 449D Jzzhu and Numbers (容斥+dp)

题意:

给出n个数,求a1&a2&a3...&an,任意子序列相&之后等于0的个数。

题解:

不是很懂之后再看看。dp[i][j]某个位i为0状态为j相&等于0的个数。然后用容斥原理得出答案,dp[n][j]即f(j)。

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
typedef long long lld;
const int oo=0x3f3f3f3f;
const lld OO=1LL<<61;
const lld MOD=1000000007;
#define eps 1e-6
const int ht=20;
const int full=1<<20;
lld dp[ht+1][full];

lld quickpow(lld a,lld b)
{
    lld res=1;
    while(b)
    {
        if(b&1)
            res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int t;
        memset(dp,0,sizeof dp);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&t);
            dp[0][t]++;
        }
        for(int i=1;i<=ht;i++)
        {
            for(int s=0;s<full;s++)
            {
                if(s&(1<<(i-1)))
                    dp[i][s]=dp[i-1][s];
                else
                    dp[i][s]=(dp[i-1][s]+dp[i-1][s|(1<<(i-1))]+MOD)%MOD;
            }
        }
        lld ans=0;
        for(int i=0;i<full;i++)
        {
            lld cnt=__builtin_popcount(i);
            lld res=quickpow(2,dp[ht][i]);
            ans=(ans+((cnt&1)?-res:res)+MOD)%MOD;
        }
        cout<<ans<<endl;
    }
    return 0;
}
/**
3
2 3 3
4
0 1 2 3
6
5 2 0 5 2 1

*/





你可能感兴趣的:(codeforces 449D Jzzhu and Numbers (容斥+dp))