博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)
阅读量:5992 次
发布时间:2019-06-20

本文共 2868 字,大约阅读时间需要 9 分钟。

 

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: nk 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 nkm, separated with spaces (1 ≤ n ≤ 10180 ≤ k ≤ n2 ≤ m ≤ 1 000 000).

Output

Write the formula value for given arguments nkm.

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/Jhhh-2016-04-16 13:07:05*/#include 
#include
#include
#include
#include
#include
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

你可能感兴趣的文章
怎样在Ubuntu中修改默认程序
查看>>
Python分布式爬虫原理
查看>>
PowerShell~发布你的mvc网站
查看>>
POJ3104 Drying [二分]
查看>>
jquery获取,赋值img的src值..
查看>>
Kali linux 2016.2(Rolling)里Metasploit的常用模块
查看>>
EntityFramework Core并发导致显式插入主键问题
查看>>
hrtimer的简单使用 + 原理和实现【转】
查看>>
关于IE8不支持document.getElementById().innerHTML的问题
查看>>
用rand()和srand()产生伪随机数的方法总结 【转】
查看>>
二分搜索及其扩展
查看>>
缓存篇(Cache)~大话开篇
查看>>
看SQL SERVER数据库当前连接数
查看>>
LintCode: Restore IP Address
查看>>
LintCode: Valid Parentheses
查看>>
事件引入和本质
查看>>
apiCloud事件发送与监听
查看>>
JasperReport,iReport 开发资源
查看>>
SOA面向服务架构简述
查看>>
FotoVision学习手记(2)
查看>>