日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【BZOJ】2982: combination(lucas定理+乘法逆

系統(tǒng) 2253 0

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

然后這是網(wǎng)上搜到的關于lucas的一些內(nèi)容

首先給出這個Lucas定理:

?

A、B是非負整數(shù),p是質(zhì)數(shù)。AB寫成p進制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。則組合數(shù)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個一段,每一段求的結(jié)果是一樣的。但是需要單獨處理每一段的末尾p, 2p, ...,把p提取出來,會發(fā)現(xiàn)剩下的數(shù)正好又是(n / p)!,相當于劃歸成了一個子問題,這樣遞歸求解即可。

這個是單獨處理n!的情況,當然C(n,m)就是n!/(m!*(n-m)!),每一個階乘都用上面的方法處理的話,就是Lucas定理了,注意這兒的p是素數(shù)是有必要的。

Lucas最大的數(shù)據(jù)處理能力是p在10^5左右,不能再大了,hdu 3037就是10^5級別的!

?

對于大組合數(shù)取模,n,m不大于10^5的話,用逆元的方法,可以解決。對于n,m大于10^5的話,那么要求p<10^5,這樣就是Lucas定理了,將n,m轉(zhuǎn)化到10^5以內(nèi)解。

?

然后左邊暴力加逆元就行了,右邊就是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

LMZ有n個不同的基友,他每天晚上要選m個進行[河蟹],而且要求每天晚上的選擇都不一樣。那么LMZ能夠持續(xù)多少個這樣的夜晚呢?當然,LMZ的一年有10007天,所以他想知道答案mod 10007的值。(1<=m<=n<=200,000,000)

Input

? 第一行一個整數(shù)t,表示有t組數(shù)據(jù)。(t<=200)
? 接下來t行每行兩個整數(shù)n, m,如題意。

Output

T行,每行一個數(shù),為C(n, m) mod 10007的答案。

Sample Input

4
5 1
5 2
7 3
4 2

Sample Output

5
10
35
6

HINT

Source

【BZOJ】2982: combination(lucas定理+乘法逆元)


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 姜堰市| 汝州市| 晋江市| 隆昌县| 玉门市| 临安市| 定远县| 成武县| 政和县| 汤原县| 中方县| 齐河县| 芦山县| 和平县| 永吉县| 石渠县| 密云县| 双桥区| 湘潭县| 阿拉善右旗| 页游| 徐水县| 腾冲县| 新密市| 若尔盖县| 凤翔县| 蓬莱市| 吉隆县| 长汀县| 万全县| 嘉荫县| 句容市| 宁陵县| 新晃| 治多县| 府谷县| 武安市| 拉萨市| 永定县| 明星| 宝山区|