日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

hdu3037 Saving Beans

發(fā)布時(shí)間:2025/4/16 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu3037 Saving Beans 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題意: ?給了n課不同的樹,要求將 0,1,2,3,4,5,...m個(gè)松果,分別放在n棵樹上的方案數(shù)有多少
? ? 總結(jié):當(dāng)選取得數(shù)含有0時(shí),要轉(zhuǎn)化,然而開始并不會(huì)組合數(shù)學(xué)太爛了

#include <iostream> #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;} 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=fac[n];ans*=inv_exgcd((fac[m]*fac[n-m])%mod,mod);return ans%mod; } 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(1+b,a,mod);printf("%lld\n",lu);}return 0; } //waw: ///https://blog.csdn.net/sdfzchy/article/details/76099080 #include <iostream> #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=1e4+10; using namespace std; int primer[maxn]; ll fac[maxn][maxn]; ll inv[maxn][maxn]; ll a[maxn]; ll n,m,mod,cnt=0; void isprime() {for(int i=2;i<maxn;++i){if(!a[i]){ primer[cnt++]=i;for(int j=2*i;j<=maxn;j+=i)a[j]=1;}} } void init()//預(yù)處理階乘和逆元 {for(int i=0;i<cnt;++i){fac[primer[i]][0]=inv[primer[i]][0]=1;for(int j=1;j<primer[i];++j){fac[primer[i]][j] = (fac[primer[i]][j-1] * j) % primer[i];inv[primer[i]][j] = inv_exgcd(fac[primer[i]][j],primer[i]);}} } ll C(ll n,ll m,ll mod) {if(m>n)return 0;if(n==m)return 1;return fac[mod][n]*(inv[mod][m]*inv[mod][n-m]%mod)%mod;//ll ans=fac[n]*inv_exgcd((fac[m]*fac[n-m])%mod,mod); return ans%mod; } 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() {isprime();init();ll t;scanf("%lld",&t);while(t--){scanf("%lld%lld%lld",&n,&m,&mod);ll lu=Lucas(1+m,n,mod);printf("%lld\n",lu);}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的hdu3037 Saving Beans的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。