zoj3557 插板法卢卡斯求组合数取模
生活随笔
收集整理的這篇文章主要介紹了
zoj3557 插板法卢卡斯求组合数取模
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:給一個集合,一共n個元素,從中選取m個元素,選出的元素中沒有相鄰的元素的選法一共有多少種?
?插板法的經典應用?
?0.首先我們拿出m個小球,還剩下n-m個小球。這n-m個小球一共有n-m+1個空(左右兩邊也可以),把這m個小球插入到這n-m+1個空里就是答案,即?
? ?這m個小球的編號取決于它插入的位置,所以和選哪個小球沒關系.
?1.1<= p <=1e9不能預處理
?
#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; 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;}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() {while(scanf("%lld%lld%lld",&a,&b,&mod)!=EOF){a=a-b+1;ll lu=Lucas(a,b,mod);printf("%lld\n",lu);}return 0; }?
總結
以上是生活随笔為你收集整理的zoj3557 插板法卢卡斯求组合数取模的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gym/100633/J Ceizen
- 下一篇: HYSBZ-1951 古代猪文 【好题】