Lucas定理 && HODJ  4349

来源:互联网 发布:苹果电脑设计软件下载 编辑:程序博客网 时间:2024/05/22 04:36

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349

首先给出这个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) 

Lucas更多:http://guanhuaing.iteye.com/blog/1502004

Fornon-negativeintegers m and n anda prime p, thefollowing congruencerelationholds:

\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

arethe base p expansionsof m and n respectively.

 

解体思路:
本题为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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int get_result(int n)
{
    int res =0;
    while(n)
 {
       res+=(n&1);
       n>>=1;
    }
    returnres;
}
int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
       printf("%d\n", 1 <<get_result(n));
    return0;
}

本文章源于:http://blog.csdn.net/julyana_lin/article/details/7840491

0 0