Xiao Ming's Hope Binomial Coefficients 卢卡斯定理的推广。

                       Xiao Ming's Hope 

题目抽象:求C(n,0),C(n,1),……,C(n,n)中奇数的个数。

思路:考察C(n,m)%2.  

由卢卡斯定理    C(A,B)=C(a[n-1],b[n-1])*C(a[n-2],b[n-2])* ……*C(a[0],b[0])  ,其中a[i]为A的p进制位

要使C(n,m)%2==1,  那么对于n上的1的位,m上对应的位可以为0,1,(c(1,0)=c(1,1)=1).

对于n上为0的位,m上对应的位必须为0.(c(0,0)=1,c(0,1)=0).

那么题目的ans=2^cnt(cnt为n的二进制位中1的个数)。

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 int main()
10 {
11     int n;
12     while(scanf("%d",&n)!=EOF)
13     {
14         int cnt=0;
15         while(n)
16         {
17             if(n&1)
18                 cnt++;
19             n/=2;
20         }
21         printf("%d\n",1<<cnt);
22     }
23     return 0;
24 }

                             Binomial Coefficients

题目抽象:判断C(n,m)%2的值。

思路:卢卡斯定理。以m为研究对象,对于m的二进制上的0位,对奇性没有影响。   考察m的二进制上的1位。要使结果为1,那么n上对应的位必须为1.

ans=(n&m)==m?1:0;

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 int main()
10 {
11     int n,m;
12     while(scanf("%d%d",&n,&m)!=EOF)
13     {
14         if((n&m)!=m)
15             printf("0\n");
16         else
17             printf("1\n");
18     }
19     return 0;
20 }

 

转载于:https://www.cnblogs.com/hutaishi/p/4490973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值