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;
}

  

2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)的更多相关文章

  1. 2015 ICL, Finals, Div. 2【ABFGJK】

    [题外话:我......不补了......] 2015 ICL, Finals, Div. 2:http://codeforces.com/gym/100637 G. #TheDress[水] (st ...

  2. CF 2015 ICL, Finals, Div. 1 J. Ceizenpok’s formula [Lucas定理]

    http://codeforces.com/gym/100633/problem/J Lucas定理P不是质数裸题 #include <iostream> #include <cst ...

  3. codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT

    默默敲了一个下午,终于过了, 题目传送门 扩展Lucas是什么,就是对于模数p,p不是质数,但是不大,如果是1e9这种大数,可能没办法, 对于1000000之内的数是可以轻松解决的. 题解传送门 代码 ...

  4. Ceizenpok’s formula Gym - 100633J 扩展Lucas定理 + 中国剩余定理

    http://codeforces.com/gym/100633/problem/J 其实这个解法不难学的,不需要太多的数学.但是证明的话,我可能给不了严格的证明.可以看看这篇文章 http://ww ...

  5. GYM100633J. Ceizenpok’s formula 扩展lucas模板

    J. Ceizenpok’s formula time limit per test 2.0 s memory limit per test 256 MB input standard input o ...

  6. codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula【扩展lucas】

    传送门 [题意]: 求C(n,k)%m,n<=108,k<=n,m<=106 [思路]: 扩展lucas定理+中国剩余定理    #include<cstdio> usi ...

  7. [Codeforces 100633J]Ceizenpok’s formula

    Description 题库链接 求 \[C_n^m \mod p\] \(1\leq m\leq n\leq 10^{18},2\leq p\leq 1000000\) Solution 一般的 \ ...

  8. codeforces Gym - 100633J Ceizenpok’s formula

    拓展Lucas #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...

  9. Codeforces.100633J.Ceizenpok's formula(扩展Lucas)

    题目链接 ->扩展Lucas //求C_n^k%m #include <cstdio> typedef long long LL; LL FP(LL x,LL k,LL p) { L ...

随机推荐

  1. pandas 使用

    ss = [['xx','m',22],['cc','w',33],['jj','w',44],['qq','m',11]] import pandas as pd df = pd.DataFrame ...

  2. Syabse数据库无法启动的解决方案

    在探讨本问题之前,首先要为大家解释一下Syabse数据库本身.Syabse数据库应用和本身的架构相对而言都相对比较复杂,多数技术人员及公司对Sybase数据库底层结构和运行机制也处于并非完全了解的阶段 ...

  3. [知识梳理]课本3&9.1

    函数:关键词:参数.返回值.函数返回类型.函数体. 函数按照返回类型,可以分为有参函数和无参函数. 函数根据是否有返回值,可以分为返回值函数和非返回值函数.     函数的定义:函数的定义可以放在任意 ...

  4. redis 持久化之 RDB

    redis的运维过程中,我们对数据持久化做一个基本的总结. 1什么是持久化: redis 所有数据保持在内存中,对数据的更新将异步地保存到磁盘上. RDB 文件创建的过程是直接从内存 写入到我们我磁盘 ...

  5. 使用Putty实现windows向阿里云的Linux云服务器上传文件

    1.首先获取PSCP工具 PuTTY小巧方便.但若需要向网络中的Linux系统上传文件,则可以使用PuTTY官方提供的PSCP工具来实现上传.PSCP是基于ssh协议实现. 可以点击这里下载 2.启动 ...

  6. HTTP协议扫盲(四)HTTP协议进阶 - MIME类型

    一.概念和原理 1.什么是MIME类型? MIME类型,即多用途互联网邮件扩展,它是一个互联网标准,在1992年最早应用于电子邮件系统,但后来也应用到浏览器. 服务器会将它们发送的多媒体数据的类型告诉 ...

  7. ABP CORE 框架入门视频教程《电话薄》基于 Asp.NET Core2.0 EF Core

    ABP框架简介 ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践和流行 ...

  8. Python系列-python文件操作

    原链接:https://blog.csdn.net/m0_37745438/article/details/79573414 python提供了一系列方法来对文件进行读取.写入等操作 一.打开文件的方 ...

  9. JavaScript 重点笔记

    JavaScript 重点笔记 ## 数组 // 必须掌握 - arr.length:获取数组元素的长度 - arr.splice(起始位置,长度):从数组中添加或删除元素. - arr.indexO ...

  10. Linux:nohub启动后台永久进程

    nohup 命令运行由 Command参数和任何相关的 Arg参数指定的命令,忽略所有挂断(SIGHUP)信号.在注销后使用 nohup 命令运行后台中的程序.要运行后台中的 nohup 命令,添加 ...