HDU - 4349 Xiao Ming‘s Hope(卢卡斯定理的理解)

传送门


题目大意:

题目很简单,给出一个 n n n,求 C n 0 , C n 1 , C n 2 , . . . , C n n C_n^0,C_n^1,C_n^2,...,C_n^n Cn0,Cn1,Cn2,...,Cnn,求其中奇数的个数

分析:

利用卢卡斯定理,求 C n m m o d    2 C_n^mmod~~2 Cnmmod  2,即相当于把 n n n m m m化为二进制,那么最后只需要知道 C n 0 , C n 1 , C n 2 , . . . , C n n C_n^0,C_n^1,C_n^2,...,C_n^n Cn0,Cn1,Cn2,...,Cnn每个模 2 2 2后是否为 1 1 1,但是数据范围太大无法 O ( n ) O(n) O(n)遍历,怎么办?

因为 A , B A,B A,B写成 p p p进制: A = a [ n ] a [ n − 1 ] … a [ 0 ] A=a[n]a[n-1]…a[0] A=a[n]a[n1]a[0] B = b [ n ] b [ n − 1 ] … b [ 0 ] B=b[n]b[n-1]…b[0] B=b[n]b[n1]b[0]后,组合数 C A B C_A^B CAB ( C a [ n ] b [ n ] ∗ C a [ n − 1 ] b [ n − 1 ] … ∗ C a [ 0 ] b [ 0 ] )   m o d    p (C_{a[n]}^{b[n]}*C_{a[n-1]}^{b[n-1]}…*C_{a[0]}^{b[0]})~mod~~p (Ca[n]b[n]Ca[n1]b[n1]Ca[0]b[0]) mod  p同余,我们又知道 C 0 1 = 0 , C 0 0 = 1 , C 1 0 = 1 , C 1 1 = 1 C_0^1=0,C_0^0=1,C_1^0=1,C_1^1=1 C01=0,C00=1,C10=1,C11=1,因为 m ≤ n m\leq n mn,那么在 n n n的二进制中某位为 0 0 0的话在 m m m中对应一定为 0 0 0,如果为 1 1 1最后乘积则为零而不是奇数,因此看 n n n中有多少个 1 1 1,设为 a n s ans ans,不难得到这些 1 1 1能组成的数的个数即为 2 a n s − 1 2^{ans}-1 2ans1,又因为 C n 0 C_n^0 Cn0一定为 1 1 1,那么答案就是 2 a n s 2^{ans} 2ans

//
// Created by Happig on 2020/8/20
//
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define lowbit(x) (x&(-x))
#define mkp(x, y) make_pair(x,y)
#define mem(a, x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double dinf = 1e300;
const ll INF = 1e18;
const int Mod = 1e9 + 7;
const int maxn = 1e5 + 10;


int main() {
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n;
    while(cin>>n && n){
        int ans=0;
        while(n){
            if(n&1) ans++;
            n>>=1;
        }
        cout<<(1<<ans)<<"\n";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值