J. Ceizenpok’s formula
time limit per test

2.0 s

memory limit per test

256 MB

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.

Examples
Input
2 1 3
Output
2
Input
4 2 5
Output
1
 
 
 

【题解】

裸的扩展lucas + CRT,推荐博文http://blog.csdn.net/clove_unique/article/details/54571216
因为特殊情况k = 0的时候,我以为是0,加了个特判,结果应该是1,不用特判。。。。。。。。。。卡了我半个晚上
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <sstream>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
template<class T>
inline void swap(T &a, T &b)
{
T tmp = a;a = b;b = tmp;
}
inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f; long long pow(long long a, long long b, long long mod)
{
long long r = , base = a % mod;
for(;b;b >>= )
{
if(b & ) r *= base, r %= mod;
base *= base, base %= mod;
}
return r;
}
void exgcd(long long a, long long b, long long &x, long long &y)
{
if(!b)x = , y = ;
else exgcd(b, a%b, y, x), y -= (a / b) * x;
}
long long ni(long long x, long long mod)
{
long long inv, y; exgcd(x, mod, inv, y);
inv = (inv % mod + mod) % mod;
if(!inv) inv = mod;
return inv;
}
int tiaoshi;
long long calc(long long n, long long p, long long pt)
{
if(n == ) return ;
long long ans = ;
for(long long i = ;i <= pt;++ i) if(i % p) ans *= i, ans %= pt;
ans = pow(ans, n/pt, pt);
for(long long i = ;i <= n%pt;++ i)
if(i % p)
ans *= i, ans %= pt;
return ans * calc(n/p, p, pt) % pt;
}
long long C(long long n, long long m, long long p, long long pt)
{
if(n < m || n < || m < ) return ;
long long cnt = ;
for(long long i = n;i;i /= p) cnt += i/p;
for(long long i = m;i;i /= p) cnt -= i/p;
for(long long i = n - m;i;i /= p) cnt -= i/p;
return pow(p, cnt, pt) * calc(n, p, pt) % pt * ni(calc(m, p, pt), pt) % pt * ni(calc(n - m, p, pt), pt) % pt;
}
long long exlucas(long long n, long long m, long long mod)
{
long long tmp2 = mod, ans = ;
for(long long i = ;i <= mod;++ i)
if(tmp2 % i == )
{
long long pt = ;
while(tmp2 % i == ) tmp2 /= i, pt *= i;
long long tmp3 = C(n, m, i, pt);
ans += tmp3 * (mod/pt) % mod * ni(mod/pt, pt) % mod;
ans %= mod;
}
return ans;
}
long long n,m,p;
int main()
{
read(n), read(m), read(p);
printf("%lld", exlucas(n, m, p));
return ;
}

GYM100633J

GYM100633J. Ceizenpok’s formula 扩展lucas模板的更多相关文章

  1. 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 ...

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

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

  3. bzoj 2142 礼物——扩展lucas模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 没给P的范围,但说 pi ^ ci<=1e5,一看就是扩展lucas. 学习材料 ...

  4. 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 stand ...

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

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

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

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

  7. 扩展CRT +扩展LUCAS

    再次感谢zyf2000超强的讲解. 扩展CRT其实就是爆推式子,然后一路合并,只是最后一个式子上我有点小疑惑,但整体还算好理解. #include<iostream> #include&l ...

  8. [学习笔记]扩展LUCAS定理

    可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...

  9. BZOJ - 2142 礼物 (扩展Lucas定理)

    扩展Lucas定理模板题(貌似这玩意也只能出模板题了吧~~本菜鸡见识鄙薄,有待指正) 原理: https://blog.csdn.net/hqddm1253679098/article/details ...

随机推荐

  1. Luogu P4180 【模板】严格次小生成树[BJWC2010]

    P4180 [模板]严格次小生成树[BJWC2010] 题意 题目描述 小\(C\)最近学了很多最小生成树的算法,\(Prim\)算法.\(Kurskal\)算法.消圈算法等等.正当小\(C\)洋洋得 ...

  2. C++ 系列:typedef 和 #define 的区别

    总结一下typedef和#define的区别 1.概念 #define 它在编译预处理时进行简单的替换,不作正确性检查.它是预处理指令. typedef 它在自己的作用域内给一个已经存在的类型一个别名 ...

  3. js获取json的健与值

    let myObj = { name: '张三', age: 18,sex:'女' } let tempArr = Object.keys(myObj) console.log(tempArr) fo ...

  4. python+selenium中webdriver相关资源

    Chrome chrome的webdriver :  http://chromedriver.storage.googleapis.com/index.html chrome的webdriver需要对 ...

  5. thinkphp 参数绑定

    参数绑定是指绑定一个参数到预处理的SQL语句中的对应命名占位符或问号占位符指定的变量,并且可以提高SQL处理的效率,需要数据库驱动类的支持,目前只有PDO和Sqlsrv驱动支持参数绑定功能. 富瑞华大 ...

  6. 完全卸载之前8.0的Mysql,安装5.5mysql

    完全卸载: https://blog.csdn.net/sxingming/article/details/52601250 安装mysql5.5: https://blog.csdn.net/fly ...

  7. 容斥原理——hdu3208

    和hdu2204有点像 这题要特别注意精度问题,如pow的精度需要自己搞一下,然后最大的longlong可以设为1<<31 /* 只要求[1,n]范围内的sum即可 那么先枚举幂次k[1, ...

  8. iOS之CAShapeLayer属性简介

    1.CAShapeLayer需要和贝塞尔曲线一块使用! #import <QuartzCore/CALayer.h> NS_ASSUME_NONNULL_BEGIN CA_CLASS_AV ...

  9. solusvm 主控端迁移

    难点在于solusvm被控端已经开了小鸡的情况. 备份数据库: #!/bin/sh ## Vars CONF=/usr/local/solusvm/includes/solusvm.conf FILE ...

  10. JSON高亮格式化页面显示

    高亮CSS定义: <style type="text/css"> pre {outline: 1px solid #ccc; padding: 5px; margin: ...