Lucas定理如下,是Édouard Lucas于1878年证明的定理,其数学表达式如下:
For non-negative integers m and n and a prime p, the following congruence relation holds:
where
and
are the base p expansions of m and n respectively.
其证明过程,我表示在一晚上没睡觉的情况下,实在是懒得去看懂........就默认该定理成立。
而在HDU 4349题中,是要求C(n,m)其中0<=m<=n中奇数的个数,由于n的值得范围是1<=n<=100000000,显然不可能将C(n,m)算出进行统计,那么就用到了Lucas定理。
每一个m都可以写成其中p=2;
每一个n都可以写成,其中p=2;
这样的话,每一个0<=ni<=1 0<=mi<=1。
由Lucas定理知,若C(n,m)是奇数,那么每一个C(ni,mi)都是奇数,这样那么ni==1 ( mi==1
或者 mi==0 ) , 而对于每组数据给定的n,其值ni是固定的。因此,只需将式子 中的换成2。那么这个式子就是n的二进制转化为10进制的式子,因此只需要统计出n转化为2进制时1的个数,就是题目的解。
附代码:
#include <stdio.h> #include <math.h> #define LL long long void solve() { int n;LL temp; while(scanf("%d",&n)==1) { int counter=0; while(n) { if(n%2==1) counter++; n=n/2; } temp=(LL)(pow(2.0,counter)); printf("%I64d\n",temp); } } int main() { solve(); return 0; }