HDU - 4349 Lucas定理的应用
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4349
题意: 给定n,统计 C(n,i)(0<=i<=n) 奇数的个数
思路:
卢卡斯定理介绍:https://brilliant.org/wiki/lucas-theorem/
用到的结论:C(m,n) is divisible by p if and only if at least one of the base-p digits of n is greater than the corresponding base-p digit of m.
C(m,n) 可以被p整除当且仅当p进制下n有至少一位大于m。
令 p=2 ,则要求出二进制表示下所有位都小于n的数字的个数。
将n转换成2进制,统计其中1出现的个数i,求 C(i,j)(0<=j<=i) 的和即可,由牛顿二项式可知,答案为2的 i 次方。
参考图片:

总结:
初次使用 Lucas 定理,还有很多不懂的地方。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #define ll long long using namespace std; int main() { ll n; while (~scanf("%lld", &n)) { ll cnt = 0; cnt = 0; while (n) { cnt += n & 1; n >>= 1; } printf("%lld\n", 1 << cnt); } }

浙公网安备 33010602011771号