1. 程式人生 > >HDU 3037 Saving Beans (Lucas定理求大數組合數)

HDU 3037 Saving Beans (Lucas定理求大數組合數)

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long  ll;
const int N = 150000;
ll n, m, p;
ll fac[N];
void init() {//預處理,很重要
	fac[0] = 1;
	for(int i = 1; i <= p; i++) {
		fac[i] = (fac[i-1]*i)%p;
	}
}
ll qpow(ll a, ll b) {
	ll res = 1;
	while(b) {
		if(b&1)
			res = res*a,res %= p;
		a *= a;
		a %= p;
		b >>= 1;
	}
	return res;
}
ll Cal(ll n, ll m) {
	if(m > n) return 0;
	return fac[n]*qpow(fac[m],p-2)%p*qpow(fac[n-m],p-2)%p;
}
ll Lucas(ll n, ll m) {
	if(m == 0) return 1;
	else return (Cal(n%p,m%p)*Lucas(n/p,m/p))%p;
}
int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		scanf("%lld %lld %lld", &n, &m, &p);
		init();
		printf("%lld\n", Lucas(n+m,m));
	}
}

相關推薦

HDU 3037 Saving Beans Lucas定理大數合數

#include <iostream> #include <cstdio> using namespace std; typedef long long ll; const

hdu 3037 Saving Beans合數取模

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3697    Accepted S

HDU 3037 Saving Beans 多重集合的結合 lucas定理

memset init pan 逆元 lucas定理 scan name style ide   題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=3037   題目描述: 要求求x1 + x2 + x3 + ...... +

HDU 3037 Saving Beans (隔板法 Lucas定理 費馬小定理 乘法逆元)

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7780&nbs

HDU 3037 Saving Beans (Lucas定理)

題目連結 Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days.

3037 Saving Beans 數論,合數取模,lucas定理

也是通過看別人的程式碼才知道這個題應該怎麼做:Lucas定理題目相當於求n個數的和不超過m的方案數。如果和恰好等於m,那麼就等價於方程x1+x2+...+xn = m的解的個數,利用插板法可以得到方案數為:(m+1)*(m+2)...(m+n-1)  = C(m+n-1,n-1) = C(m+n-1,m)現在

HDU 3037:Saving Beans

diff ble other hint while ber oid lines def Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth

HDU - 3037 Saving Beans

題目連結 題目大意:將不大於m個種子隨機放在n個樹上,有多少種可能的結果資料量可能有點大,結果模一個素數p /* 插板法:將N個物品分成M組,每一組至少有一件物品,這個問題就能轉換成N個物品擺在桌子上,然後中間有N-1個空, 在這N-1個空裡面找M個位置,把板子插進去 */ /*

hdu3037--Saving Beans --【lucas定理+逆元+插板法】

                                              Saving Beans                         Time Limit: 6000/3000 MS (Java/Others)    Memory Limit

Lucas定理與大合數的取模的求法總結

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

[演算法 18_001] Lucas 定理與大合數取餘

Lucas 定理 該定理是用來求當 (nm) ( n m

Codeforces 559C Gerald and Giant ChessDP+乘法逆元合數

先把黑塊按座標排序。 dp[i]表示到第i個黑塊且之前沒有經過黑塊的方案數,那麼每一個dp[i]中的方案都是完全不相同的。遞推的方法是dp[i]=C(xi+yi,xi)-sum(dp[j]*C(xi-xj+yi-yj,xi-xj))  (j<i) dp[j]*C(xi

HDU 3037Saving Beans合數取模

Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3706 Acc

HDU - 4349 - Xiao Ming's HopeLucas定理

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

Lucas定理合數模板

end code == turn tdi div rac bsp 模板 $Lucas(n,m,p)=C(n\%p,m\%p)*Lucas(n/p,m/p,p)$ $C^n_m=\frac{n!}{m!(n-m)!}$ $x^{p-1}\equiv 1(mod p)\Long

hdu 5919--Sequence II主席樹--區間不同數個數+區間第k大

positions minus -s ima date rst itl 主席樹 技術 題目鏈接 Problem Description Mr. Frog has an integer sequence of length n, which can be denot

bzoj2982: combinationLucas定理

Description LMZ 有 n 個不同的基友,他每天晚上要選 m 個進行 [ 河蟹 ] ,而且要求每天晚上的選擇都不一樣。那麼 LMZ 能夠持續多少個這樣的夜晚呢?當然, LMZ 的一年有 10007 天,所以他想知道答案

bzoj 2982: combinationlucas定理模板

lucas定理板子 對於可以轉化為這是一個遞迴的過程,時間為log。 #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespac

ZOJ 3557 - How Many Sets IILucas定理模板

題目連結 https://cn.vjudge.net/problem/ZOJ-3557 【題意】 從n個相同小球中取m個小球,不能取相鄰的小球的方案數 【思路】 首先拿出 m

51Nod 1120 - 機器人走方格 V3Lucas定理+Catalan數

題目連結 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1120 【題目描述】 N * N的方格,從左上到右下畫一條線。一個機器人從左上走到右下,只能向右或向下走。並要求只能在這條線的上面或下面走,不能穿越這條