組合數學
阿新 • • 發佈:2020-07-18
盧卡斯定理
求組合數:
模板題:P3807 【模板】盧卡斯定理:https://www.luogu.com.cn/problem/P3807
#include<bits/stdc++.h> #define INF 0x3f3f3f3f #define DOF 0x7f7f7f7f #define endl '\n' #define mem(a,b) memset(a,b,sizeof(a)) #define debug(case,x); cout<<case<<" : "<<x<<endl; #define open freopen("ii.txt","r",stdin) #define close freopen("oo.txt","w",stdout) #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define pb push_back using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<long long, long long> PII; const int maxn = 1e6 + 10; const ll mod=1e9+7; ll j[maxn]; ll n,m,p; ll quick_pow(ll a,ll b ,ll p){ ll ans = 1; while(b){ if(b & 1) ans = (ans * a)%p; a = a * a % p; b>>=1; } return ans; } ll C(ll n,ll m){ if(m > n)return 0; return ((j[n] * quick_pow(j[m],p-2,p))%p * quick_pow(j[n-m],p-2,p)%p); } ll Lucas(ll n,ll m){ if(!m)return 1; return C(n%p,m%p)*Lucas(n/p,m/p)%p; } int main(){ int T; j[0] = 1; cin>>T; while(T--){ cin >> n >> m >> p; for(int i=1;i<=p;i++) j[i] = j[i-1] * i % p; cout<<Lucas(n+m,n)<<endl; } return 0; }
連結:https://ac.nowcoder.com/acm/problem/16596
來源:牛客網
題目描述
給定一個多項式(ax+by)k,請求出多項式展開後xnym項的係數。
輸入描述:
共一行,包含5個整數,分別為a,b,k,n,m,每兩個整數之間用一個空格隔開。
輸出描述:
輸出共1行,包含一個整數,表示所求的係數,這個係數可能很大,輸出對10007取模後的結果。
思路
\[T_{r+1}=C_{k}^{n}*(ax)^n*(by)^m \]
對公式直接求解
#include<bits/stdc++.h> #define INF 0x3f3f3f3f #define DOF 0x7f7f7f7f #define endl '\n' #define mem(a,b) memset(a,b,sizeof(a)) #define debug(case,x); cout<<case<<" : "<<x<<endl; #define open freopen("ii.txt","r",stdin) #define close freopen("oo.txt","w",stdout) #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define pb push_back using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<long long, long long> PII; const int maxn = 1e6 + 10; const ll mod=1e9+7; ll j[maxn]; ll n,m,p; ll quick_pow(ll a,ll b ,ll p){ ll ans = 1; while(b){ if(b & 1) ans = (ans * a)%p; a = a * a % p; b>>=1; } return ans; } ll C(ll n,ll m){ if(m > n)return 0; return ((j[n] * quick_pow(j[m],p-2,p))%p * quick_pow(j[n-m],p-2,p)%p); } ll Lucas(ll n,ll m){ if(!m)return 1; return C(n%p,m%p)*Lucas(n/p,m/p)%p; } int main(){ j[0]=1; ll a,b,k,n1,m1;cin>>a>>b>>k>>n1>>m1; n=k,m=n1; p=10007; for(int i=1;i<=p;++i){ j[i]=j[i-1]*i%p; } cout<<(((quick_pow(a,n1,p)%p*(quick_pow(b,m1,p)%p))%p)*(Lucas(n,m)%p))%p<<endl; }