hdu 4349

Xiao Ming's Hope

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2109    Accepted Submission(s): 1428


Problem Description
Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2011.11.11 comes. Seeing classmates walking with their girl friends, he coundn't help running into his classroom, and then opened his maths book preparing to count odd numbers. He looked at his book, then he found a question "C (n,0)+C (n,1)+C (n,2)+...+C (n,n)=?". Of course, Xiao Ming knew the answer, but he didn't care about that , What he wanted to know was that how many odd numbers there were? Then he began to count odd numbers. When n is equal to 1, C (1,0)=C (1,1)=1, there are 2 odd numbers. When n is equal to 2, C (2,0)=C (2,2)=1, there are 2 odd numbers...... Suddenly, he found a girl was watching him counting odd numbers. In order to show his gifts on maths, he wrote several big numbers what n would be equal to, but he found it was impossible to finished his tasks, then he sent a piece of information to you, and wanted you a excellent programmer to help him, he really didn't want to let her down. Can you help him?
 

Input
Each line contains a integer n(1<=n<=10 8)
 

Output
A single line with the number of odd numbers of C (n,0),C (n,1),C (n,2)...C (n,n).
 

Sample Input
  
  
1 2 11
 

Sample Output
  
  
2 2 8
 

首先给出这个Lucas定理:

A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。
则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  modp同余

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



For non-negative integers m and n and a prime p, the following congruence relationholds:

\binom{m}{n}\equiv\prod_{i=0}^k\binom{m_i}{n_i}\pmod p, 表示的是同余,表示两个数对p取余,余数相同。

where

m=m_kp^k+m_{k-1}p^{k-1}+\cdots +m_1p+m_0,

and

n=n_kp^k+n_{k-1}p^{k-1}+\cdots +n_1p+n_0

are the base p expansions of m and n respectively.


 



证明:


http://en.wikipedia.org/wiki/Lucas%27_theorem


维基的意思是说在m中里面取n个%p来说,这样考虑:

将m划分成m0个1,m1个p,m2个p^2,……

将n划分成n0个1,n1个p,n2个p^2……


那是不是C(m,n)的总共的取法就是( 在m0个里面取n0个的方案数 * m1个里面取n1个的方案数 * m2个里面取n2个的方案数 ……)


证明就是这样。


之所以看lucas,是因为12年多校有Lucas定理的题。


咳咳,我们先来看看hdu3037.


意思是说将不少于m个球放在n个盒子里面。


做ACM做多了,把高中知识都忘了-------其实就是将m + n个球放在n个盒子当中有多少放法,就用Lucas来做呗。


 其中,由于涉及到还有取模,需要运用求乘法逆元,求乘法逆元不一定要用欧几里德,还可以用欧拉定理。


欧拉定理求逆元简介: ---维基


数论中,欧拉定理(也称费马-欧拉定理欧拉{\varphi}函数定理)是一个关于同余的性质。欧拉定理表明,若n,a为正整数,且n,a互素\gcd(a,n)=1,则

a^{\varphi(n)} \equiv 1 \pmod n

其中\varphi(n)欧拉函数\mod n同余关系。欧拉定理得名于瑞士数学家莱昂哈德·欧拉

欧拉定理实际上是费马小定理的推广。



以下是 http://blog.csdn.net/wukonwukon/article/details/7341270 的代码,加上我的一点注释:

[cpp]  view plain  copy
 print ?
  1. /* 
  2. Pro: hdu-3037 
  3.  
  4.  
  5. Sol:Lucas 
  6.     还用到乘法逆元,乘法逆元可以不用欧几里得 
  7.     定义: 
  8.     满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元。 
  9.     (PS:p一定是个素数才能对a有乘法逆元(除1),特别注意:当p是1时,对于任意a,k都为1) 
  10.   
  11.     为什么要有乘法逆元呢? 
  12. 当我们要求(a/b) mod p的值(a/b一定是个整数),且a很大,无法直接求得a/b的值时,我们就要用到乘法逆元。 
  13. 我们可以通过求b关于p的乘法逆元k,将a乘上k再模p,即(a*k) mod p。 
  14. 其结果与(a/b) mod p等价。 
  15.  
  16.  
  17. date:2012/08/07 
  18. */  
  19. #include <iostream>  
  20. #include <string.h>  
  21. #include <stdio.h>  
  22. using namespace std;  
  23.   
  24.   
  25. #define N 100010  
  26.   
  27.   
  28. long long mod_pow(int a,int n,int p)  
  29. {  
  30.     long long ret=1;  
  31.     long long A=a;  
  32.     while(n)  
  33.     {  
  34.         if (n & 1)  
  35.             ret=(ret*A)%p;  
  36.         A=(A*A)%p;  
  37.         n>>=1;  
  38.     }  
  39.     return ret;  
  40. }  
  41.   
  42.   
  43. long long factorial[N];  
  44.   
  45.   
  46. void init(long long p)  
  47. {  
  48.     factorial[0] = 1;  
  49.     for(int i = 1;i <= p;i++)  
  50.         factorial[i] = factorial[i-1]*i%p;  
  51. }  
  52.   
  53.   
  54. long long Lucas(long long a,long long k,long long p)  
  55. //求C(n,m)%p p最大为10^5。a,b可以很大!  
  56. {  
  57.     long long re = 1;  
  58.     while(a && k)  
  59.     {  
  60.         long long aa = a%p;long long bb = k%p;  
  61.         if(aa < bb) return 0; //C(aa,bb) 表示 在aa里面取bb个,取法为0;  
  62. //由于p是素数,所以 a / b % p ,b 对于 mod a 肯定存在逆元  
  63.         re = re*factorial[aa]*mod_pow(factorial[bb]*factorial[aa-bb]%p,p-2,p)%p;//这儿的求逆不可先处理  
  64.         a /= p;  
  65.         k /= p;  
  66.     }  
  67.     return re;  
  68. }  
  69. int main(){  
  70.     int a,b,c;  
  71.     while(cin >> a >> b >> c){  
  72.         cout << mod_pow(a,b,c)<< endl;  
  73.     }  
  74.     return 0;  
  75. }  
  76.   
  77.   
  78. int main()  
  79. {  
  80.     int t;  
  81.     cin >> t;  
  82.     while(t--)  
  83.     {  
  84.         long long n,m,p;  
  85.         cin >> n >> m >> p;  
  86.         init(p);  
  87.         cout << Lucas(n+m,m,p) << "\n";  
  88.     }  
  89.     return 0;  
  90. }  
接着,再来看 Multi-University 05-1010hdu-4349

http://acm.hdu.edu.cn/showproblem.php?pid=4349

官方解题报告,懂了Lucas就非常非常非常好写代码了。


本题为Lucas定理推导题,我们分析一下 C(n,m)%2,那么由lucas定理,我们可以写
* 成二进制的形式观察,比如 n=1001101,m是从000000到1001101的枚举,我们知道在该定理中
* C(0,1)=0,因此如果n=1001101的0对应位置的m二进制位为1那么C(n,m) % 2==0,因此m对应n为0的
* 位置只能填0,而1的位置填0,填1都是1(C(1,0)=C(1,1)=1),不影响结果为奇数,并且保证不会
* 出n的范围,因此所有的情况即是n中1位置对应m位置0,1的枚举,那么结果很明显就是:2^(n中1的个数)


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;


int main()
{
    int n;
    while(scanf("%d", &n)!=EOF)
    {
        int sum=1, num=0;
        while(n!=0)
        {
            if(n&1)
            {
                num++;
            }
            n>>=1;
        }
        for(int i=0;i<num;i++)
        {
            sum*=2;
        }
        printf("%d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值