日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

發布時間:2025/4/16 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

J. Ceizenpok’s formula time limit per test ?2 seconds memory limit per test ?256 megabytes input ?standard input output ?standard output

Dr. Ceizenp'ok from planet i1c5l became famous across the whole Universe thanks to his recent discovery?— the Ceizenpok’s formula. This formula has only three arguments:?n,?k?and?m, and its value is a number of?k-combinations of a set of?n?modulo?m.

While the whole Universe is trying to guess what the formula is useful for, we need to automate its calculation.

Input

Single line contains three integers?n,?k,?m, separated with spaces (1?≤?n?≤?1018,?0?≤?k?≤?n,?2?≤?m?≤?1?000?000).

Output

Write the formula value for given arguments?n,?k,?m.

Sample test(s) input 2 1 3 output 2 input 4 2 5 output 1

?

?

/* 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(組合數取模,擴展lucas定理)求C(n,k)%m 如果m是素數的話直接就能套lucas模板. 對于m為合數,我們可以把它分解成素數在進行處理 m = p1*p2*p3..pk (pk = prime[i]^t) 然后利用擴展lucas定理可以求出 C(n,k) % pi的值,最后利用中國剩余定理漲姿勢:http://www.cnblogs.com/jianglangcaijin/p/3446839.html 題目鏈接:http://codeforces.com/gym/100633/problem/J hhh-2016-04-16 13:07:05 */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <functional> typedef long long ll; #define lson (i<<1) #define rson ((i<<1)|1) using namespace std;const int maxn = 1e6+10; ll fac[maxn]; int w[maxn],num[maxn],tw[maxn]; int tot; void get_factor(ll m) {ll mm = m;tot = 0;for(ll i = 2; i*i <= m; i++){if(mm % i == 0){num[tot] = 0;w[tot] = i;tw[tot] = 1;while(mm % i == 0){num[tot]++;mm /= i;tw[tot] *= i;}tot++;}}if(mm > 1){num[tot] = 1;w[tot] = mm;tw[tot] = mm;tot ++;} }ll ex_gcd(ll a,ll b,ll &x,ll &y) {if(a == 0 && b == 0)return -1;if(b == 0){x = 1,y = 0;return a;}ll d = ex_gcd(b,a%b,y,x);y -= a/b*x;return d; }ll pow_mod(ll a,ll b,ll mod) {ll ret = 1;while(b){if(b&1) ret = ret*a%mod;a = a*a%mod;b >>= 1;}return ret; }ll revers(ll a,ll b) {ll x,y;ll d = ex_gcd(a,b,x,y);if(d == 1) return (x%b+b)%b;else return 0; }ll c1(ll n,ll p,ll pk) {if(n==0)return 1;ll ans=1;for(ll i = 2; i <= pk; i++)if(i % p)ans = ans*i%pk;ans=pow_mod(ans,n/pk,pk);for(ll k=n%pk,i=2; i<=k; i++)if(i%p)ans=ans*i%pk;return ans*c1(n/p,p,pk)%pk; }ll cal(ll n,ll m,int cur,ll mod) {ll pi = w[cur],pk = tw[cur];ll k = 0,ans;ll a,b,c;a=c1(n,pi,pk),b=c1(m,pi,pk),c=c1(n-m,pi,pk);for(ll i=n; i; i/=pi)k+=i/pi;for(ll i=m; i; i/=pi)k-=i/pi;for(ll i=n-m; i; i/=pi)k-=i/pi;ans = a*revers(b,pk)%pk*revers(c,pk)%pk*pow_mod(pi,k,pk)%pk;return ans*(mod/pk)%mod*revers(mod/pk,pk)%mod; }ll lucas(ll n,ll m,ll mod) {ll ans = 0;for(int i = 0; i < tot; i++){ans = (ans+cal(n,m,i,mod))%mod;}return ans; }ll n,k; ll m; int main() {int T;while(scanf("%I64d%I64d%I64d",&n,&k,&m) != EOF){get_factor(m);printf("%I64d\n",lucas(n,k,m));}return 0; }

  



?

轉載于:https://www.cnblogs.com/Przz/p/5409567.html

總結

以上是生活随笔為你收集整理的2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。