逆元, 組合數取模,費馬小定理, HDU 6114

2021-09-18 09:10:35 字數 1869 閱讀 8959

費馬小定理(fermat』s little theorem)

定理先放這, 現在目標是求

一般的可以做如下轉換

但是取模對除法不適用故

逆元:對於a和p(a和p互素),若a*b%p≡1,則稱b為a%p的逆元。

應用:t / a 對 p取模, 由於b是a%p的逆元, t / a 對p取模可以轉換為t * b %p 轉換為乘法了, 乘法對求模是適用的,接下來用這個來求解組合數。

求逆元:a^(p-1)≡1(mod p)即a^(p-2)* a≡1(mod p), 有a%p的逆元為a^(p-2)

對p取模, 即n! 與 m!% p的逆元,(n-m)!%p的逆元的乘積。用f(x) 表示x!,有表示式為f(n)* f(m)^(p-2) * f(n - m) ^ (p -2)對p取模。其中次方可以用快速冪求,而f(x)可以一開始用乙個陣列儲存小於max_num的階乘, 注意是對p取模的階乘,加上取模完整的表示式為**實現如下, solve(a,b)實現的是求得從a個中取b個的組合數對mod取模。

#include#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;

typedef long long ll;

const int maxn = 100000, mod = 1e9 + 7;

int fac[maxn * 2];

int pow_mod(int a, int b)

return res;

}int solve(int a, int b) //solve()函式求得從a個中取b個的組合數, 對mod取模

int main()

}

problem description

車是中國象棋中的一種棋子,它能攻擊同一行或同一列中沒有其他棋子阻隔的棋子。一天,小度在棋盤上擺起了許多車……他想知道,在一共n×m個點的矩形棋盤中擺最多個數的車使其互不攻擊的方案數。他經過思考,得出了答案。但他仍不滿足,想增加乙個條件:對於任何乙個車a,如果有其他乙個車b在它的上方(車b行號小於車a),那麼車a必須在車b的右邊(車a列號大於車b)。

現在要問問你,滿足要求的方案數是多少。

input

第一行乙個正整數t,表示資料組數。

對於每組資料:一行,兩個正整數n和m(n<=1000,m<=1000)。

output

對於每組資料輸出一行,代表方案數模1000000007(1e9+7)。

sample input

11 1

sample output

#include#include#include#include#include#include#include#include#includeusing namespace std;

typedef long long ll;

const int mod = 1e9 + 7;

int f[1005];

int n, m;

int pp(int a, int b)

return ans;

}int solve(int a, int b)

int main()

}

費馬小定理 組合數 hdu 4704

求解s k 將整數m拆分為n個數字的有序拆分方案數為c m 1,n 1 隔板法理解 把n分成乙份的分法數為c 0,n 1 把n分成兩份的分法數為c 1,n 1 把n分成三份的分法數為c 2,n 1 把n分成n份的分法數為c n 1,n 1 s 1 s 2 s n c 0,n 1 c 1,n 2 c ...

費馬小定理求逆元

求餘的概念 a b p a p b p p a b p a p b p p a b p a p b p p 為什麼要求逆元 對於一些題目,我們必須在中間過程中進行求餘,否則數字太大,電腦存不下,那如果這個算式中出現除法,我們是不是對這個算式就無法計算了呢?這時候就用到了逆元。費馬曾經說過 費馬小定理...

乘法逆元 費馬小定理

我實在是太.才明白這個qwq 一 前置知識 定義1 給定正整數m,若用m除兩個整數a和b所得的餘數相同,稱a和b對模m同餘,記作a b mod m 並稱該式子為同余式 否則稱a和b對模m不同餘 二 乘法逆元 若整數b,p互質,並且b a,則存在乙個整數x,使得 a b a x mod p 稱x為b的...