[HDU 4349] Xiao Ming's Hope (lucas定理)

HDU - 4349

给定n求满足 C(n,m)为奇数的 m的个数


首先朴素的想法是把每一个组合数都求出来
但是组合数比较大,所以自然的就想到 mod 2
所以很关键的一点是要转化为 C(n,m)%2
即 C(n,m)%2 = 1 的m的个数
利用lucas定理展开,审视乘积中的每一个组合数
由于 C(0,0) = C(1,0) = C(1,1) = 1,C(0,1) = 0
所以要想结果为 1,当 n的二进制某一位为 0时,m此位只能为 0
当 n的二进制某位为 1时,m此位可为 0,可为 1
根据乘法原理,n的二进制位有 cnt个 1 ,答案即为 2^cnt

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))

int N;

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    #endif

    while(~scanf("%d", &N))
    {
        int cnt=__builtin_popcount(N);
        printf("%d\n", 1<<cnt);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值