nefu628 組合數取模,模不是素數的情況

2021-08-04 21:02:17 字數 695 閱讀 9429

傳送門

題意:求c(m+n-2,m-1)%p,其中1<=m,n,p<=1e5,p不一定是素數

思路:資料範圍並不是很大,但關鍵是p不一定是素數,所以傳統求逆元的方法如費馬小定理或者擴充套件歐幾里得都不適用了,c(m+n-2,m-1)=(m+n-2)!/((n-1)!*(m-1)!),可以把這個數暴力分解素因子,計算其中包含的素因子個數,然後再用快速冪計算,累乘就得到了最後的結果。十分暴力。

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

typedef long long ll;

const int maxn=1e6+5;

bool prime[maxn];

int p[maxn];

int cnt;

int mod;

void isprime()

return ans;

}ll fun(ll n,ll m)///計算n!中m這個素因子的個數

return ans;

}ll solve(ll n,ll m,ll pp)

{ n=n+m-2;

m=m-1;

ll ans=1;

for(int i=0;i

組合數取模

複習了一下組合數取模,當然推薦檢視acdreamer的部落格啦,寫的確實好啦,自己把裡面的題目全a掉了。include include include include include include include using namespace std typedef long long ll l...

組合數取模

對於c n,m mod p。這裡的n,m,p p為素數 都很大的情況。就不能再用c n,m c n 1,m c n 1,m 1 的公式遞推了。這裡用到lusac定理 for non negative integers m and n and a prime p,the following congr...

組合數取模

組合數c m,n 表示在m個不同的元素中取出n個元素 不要求有序 產生的方案數。定義式 c m,n m n m n 並不會使用latex qaq 根據題目中對組合數的需要,有不同的計算方法。運用乙個數學上的組合恒等式 oi中稱之為楊輝三角 c m,n c m 1,n 1 c m 1,n 證明 1.直...