FZU - 2020 计算大组合数取模
生活随笔
收集整理的這篇文章主要介紹了
FZU - 2020 计算大组合数取模
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題意: 給出m,n. 計(jì)算C(n,m)%p,C(n,m)很大,p是素?cái)?shù)
?盧卡斯.不過不可以預(yù)處理,mod太大了
#include <iostream>//Lucas模板 #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <map> //#define IO ios::sync_with_stdio(false),cin.tie(0), cout.tie(0); //#pragma comment(linker, "/STACK:1024000000,1024000000") void ex_gcd(int a, int b, int &d, int &x, int &y) { if (!b) { x = 1; y = 0; d = a; } else { ex_gcd(b, a%b, d, y, x); y -= x * (a / b); }; } int gcd(int a, int b) { return b ? gcd(b, a%b) : a; } int lcm(int a,int b){return a/gcd(a,b)*b;}//?è3yoó3?·àò?3? int inv_exgcd(int a, int m) { int d, x, y;ex_gcd(a, m, d, x, y);return d == 1 ? (x + m) % m : -1; } typedef long long ll; const int maxn=1e6; using namespace std; ll fac[maxn]; ll a,b,mod; //void init() //{ // fac[0]=1; // for(int i=1;i<=mod;++i) // fac[i]=fac[i-1]*i%mod; //} ll C(ll n,ll m,ll mod) {if(m>n)return 0;ll ans=1;for(int i=1;i<=m;++i){ll a=(n+i-m)%mod,b=i%mod;ans=ans*(a*inv_exgcd(b,mod)%mod)%mod;} // ll ans=fac[n]; // ans*=inv_exgcd((fac[m]*fac[n-m])%mod,mod);return ans; } ll Lucas(ll n,ll m,ll mod) {if(m==0)return 1;return C(n%mod,m%mod,mod)*Lucas(n/mod,m/mod,mod)%mod; } int main() {ll t;scanf("%lld",&t);while(t--){scanf("%lld%lld%lld",&a,&b,&mod);// init();ll lu=Lucas(a,b,mod);printf("%lld\n",lu);}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的FZU - 2020 计算大组合数取模的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷【P2257】YY的GCD
- 下一篇: hdu-3944 DP?