您的位置:首页 > 其它

hdu 4349 求C(n,0),C(n,1),C(n,2)...C(n,n).当中有多少个奇数 (Lucas定理推广)

2015-09-20 21:48 288 查看

Lucas定理:把n写成p进制a
a[n-1]a[n-2]...a[0],把m写成p进制b
b[n-1]b[n-2]...b[0],则C(n,m)与C(a
,b
)*C(a[n-1],b[n-1])*C(a[n-2],b[-2])*....*C(a[0],b[0])模p同余。

即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)

这题是求C(n,0),C(n,1),C(n,2)...C(n,n).当中有多少个奇数
也是就是说 求C(n,m)%2==1 的个数 m的范围是0-n

C(n,m)%2,那么由lucas定理,我们可以写成二进制的形式观察

比如n=010 m为000 001 010
最终结果为 1+0+1=2
因为 C(0,1)=0
所以C(0,0)* C(1,0)*C(0,1)= 0
所以n = 010 中的0 不能对应m中的1 否则就会为了
n = 010 中的1 可以对应m中的0 或 1
也就变成了求n的二进制中有多少个1 求1的个数
最后结果为 2^(n中1的个数)

Sample Input
1 //n
2
11

Sample Output
2
2
8

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <string>
# include <cmath>
# include <queue>
# include <list>
# define LL long long
using namespace std ;

int main()
{
//freopen("in.txt","r",stdin) ;
int n ;
while(scanf("%d",&n)!=EOF)
{
int cnt=0;
while(n)
{
if(n&1)
cnt++;
n>>=1;
}
printf("%d\n",1<<cnt);
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: