http://www.lydsy.com/JudgeOnline/problem.php?id=2982
少加了特判n<m return 0就wa了QAQ
lucas定理:C(n, m)%p=(C(n%p, m%p)*C(n/p, m/p))%p
等英語好一點去wiki看一下證明吧QAQ http://en.wikipedia.org/wiki/Lucas%27_theorem
然后這是網上搜到的關于lucas的一些內容
首先給出這個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])??mod p同余
即: Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)?
這個定理的證明不是很簡單,我一直想找個很好的張明,但是,沒找到,昨天看到了一個解題報告,基本上可以說明白這個Lucas定理是怎么回事了,具體的是說:
以求解n! % p為例,把n分段,每p個一段,每一段求的結果是一樣的。但是需要單獨處理每一段的末尾p, 2p, ...,把p提取出來,會發現剩下的數正好又是(n / p)!,相當于劃歸成了一個子問題,這樣遞歸求解即可。
這個是單獨處理n!的情況,當然C(n,m)就是n!/(m!*(n-m)!),每一個階乘都用上面的方法處理的話,就是Lucas定理了,注意這兒的p是素數是有必要的。
Lucas最大的數據處理能力是p在10^5左右,不能再大了,hdu 3037就是10^5級別的!
?
對于大組合數取模,n,m不大于10^5的話,用逆元的方法,可以解決。對于n,m大于10^5的話,那么要求p<10^5,這樣就是Lucas定理了,將n,m轉化到10^5以內解。
?
然后左邊暴力加逆元就行了,右邊就是lucas。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
const int MD=10007;
int mpow(int a, int b) {
int ret=1;
for(; b; b>>=1, a=(a*a)%MD) if(b&1) ret=(ret*a)%MD;
return ret;
}
int getc(int n, int m) {
if(n<m) return 0;
int up=1, down=1;
for1(i, m+1, n) up=(up*i)%MD;
for1(i, 1, n-m) down=(down*i)%MD;
return (up*mpow(down, MD-2))%MD;
}
int lucas(int n, int m) {
return m?(getc(n%MD, m%MD)*lucas(n/MD, m/MD))%MD:1;
}
int main() {
int t=getint();
while(t--) {
int n=getint(), m=getint();
printf("%d\n", lucas(n, m));
}
return 0;
}
?
?
?
?
Description
Input
Output
Sample Input
5 1
5 2
7 3
4 2
Sample Output
10
35
6
HINT
Source
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

