HDU 4349——Lucas定理的巧妙应用
2012-12-15 18:42
337 查看
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的个数,就是题目的解。
附代码:
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; }
相关文章推荐
- HDU 4349 Xiao Ming's Hope (Lucas定理的应用)
- hdu 4349 Xiao Ming's Hope (Lucas定理)
- 【HDU 4349】【组合数结论 LUCAS定理推广】 Xiao Ming's Hope【C[n][m]为奇数的个数】t
- HDU 4349 && POJ 3219 Lucas有趣应用c
- hdu 4349 Xiao Ming's Hope,lucas定理
- HDU 4349 Xiao Ming's Hope (组合数的奇偶性&&Lucas定理)
- HDU - 5446 Unknown Treasure (数论部分定理的综合应用,lucas+CRT)
- hdu 4349 lucas定理的推广
- hdu 4349 Xiao Ming's Hope(Lucas定理)
- HDU 4349——Xiao Ming's Hope——————【Lucas定理】
- HDU 4349 Xiao Ming's Hope(Lucas定理的推导)
- HDU 3037 Saving Beans(Lucas定理的直接应用)
- [HDU 4349] Xiao Ming's Hope (lucas定理)
- hdu 4349 lucas定理的推导
- HDU 4349 Xiao Ming's Hope(数学题,Lucas定理)
- HDU 3037 Saving Beans(Lucas定理的直接应用)
- hdu - 4349 - Xiao Ming's Hope - 大大的Lucas定理 && 小小的乘法逆元
- [Lucas定理推广] hdu 4349 and poj 3219
- hdu 4349(Lucas定理)
- Xiao Ming's Hope HDU - 4349(Lucas定理)