hdu3929 容斥+扩展Lucas定理

分析:

根据扩展lucas定理, (1+x)n 含有的奇数项的个数是 n 的二进制表达中数字1的个数。
i & -i得到的是i的二进制表达最右边的 1 <script type="math/tex" id="MathJax-Element-114">1</script>的位置代表的十进制值。我们每次都减去它一个1,这样就有个函数可以算1的个数。
函数实现如下:

int get1(int x) {
    return x ? get(x - (x & -x)) + 1 : 0;
}

接下来容斥。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define pr(x) cout << #x << ": " << x << "  " 
#define pl(x) cout << #x << ": " << x << endl;

struct jibancanyang
{
    long long ans, n, A[22];

    int get(long long x) {
        return x ? get(x - (x & -x)) + 1 : 0;
    }


    void dfs(int ith, long long x, int f) {
        ans += ((long long)1 << get(x)) * f;
        for (int i = ith + 1; i <= n; ++i) 
            dfs(i, x & A[i], -2 * f);
    }

    void fun() {
        int T;
        scanf("%d", &T);
        for (int cas = 1; cas <= T; ++cas) {
            scanf("%lld", &n);
            for (int i = 1; i <= n; ++i) scanf("%lld", &A[i]);
            ans = 0;
            for (int i = 1; i <= n; ++i) 
                dfs(i, A[i], 1);
            printf("Case #%d: %lld\n", cas, ans);
        }
    }
}ac;

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    ac.fun();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值