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
相关文章推荐
- Mysql笔记:体系结构
- ios-新浪微博开发-21-获取用户昵称
- andriod开发点滴(未安装该应用程序)
- 显示与隐藏子窗口
- 堆跟栈的区别
- SQL
- 简易版网页计算器html页面
- 360今年笔试题之一:整数分解为素因子,并用用九字段数字表示出来的代码
- C++ CScopeGuard
- 九大内置对象
- JS可复用的多条件筛选插件
- 不可不知的网站代码交易注意事项
- unity3d实现场景截图
- 机房收费总结
- hdu2094(拓扑排序)
- 谁说转载的文章用户就不喜欢了?
- springAOP
- HandList类(Leap:: HandList)
- 类修饰符的含义 成员变量修饰符 成员方法修饰符的含义
- Java过滤器与SpringMVC拦截器之间的关系与区别