Lucas定理:
A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。
则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0]) modp同余
即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)
For non-negative integers m and n and a prime p, the following congruence relationholds:
-
表示的是同余,表示两个数对p取余,余数相同。
where
and
are the base p expansions of m and n respectively.
题解:本题为Lucas定理推导题,我们分析一下 C(n,m)%2,那么由lucas定理,我们可以写成二进制的形式观察,
比如 n=1001101,m是从000000到1001101的枚举,我们知道在该定理中C(0,1)=0,
因此如果n=1001101的0对应位置的m二进制位为1那么C(n,m) % 2==0,
因此m对应n为0的位置只能填0,而1的位置填0,填1都是1(C(1,0)=C(1,1)=1),不影响结果为奇数,并且保证不会出n的范围,
因此所有的情况即是n中1位置对应m位置0,1的枚举,那么结果很明显就是:2^(n中1的个数)
#include <stdio.h>
#include <iostream>
using namespace std ;
int main(int argc, char const *argv[])
{
int n ;
long long ans ;
while(cin>>n)
{
int k = 0;
ans =1 ;
while(n)
{
if(n % 2)
{
k ++ ;
}
n /= 2 ;
}
for(int i = 1 ; i <=k ; i ++) ans *= 2 ;
cout << ans << endl ;
}
return 0;
}