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. 17.splash_case02

    # 抓取<我不是药神>的豆瓣评论 import csv import time import requests from lxml import etree fw = open('doub ...

  2. 第四周课堂笔记3th

    1.函数的嵌套 作用域,说的是变量 全局作用域:内置命名空间,全局命名空间  全局空间不可以引用局部空间 局部作用域: 局部命名空间  开辟的临时空间前提是调用了函数 全局作用域在整个文件中被使用 L ...

  3. 【LGP4714】「数学」约数个数和

    题目 众所周知,除数个数函数\(\sigma_0=I^2\),\(I\)就是狄利克雷卷积里的\(1\)函数 于是熟悉狄利克雷卷积的话很快就能看出我们要求的就是\(I\times I^{k}\),即\( ...

  4. docker 个人遇到问题日志记录

    system: openSUSE Leap 42.3 在openSUSE中可直接运行" sudo zypper in docker"进行安装docker-ce wakasann@l ...

  5. 关于公式文件.eqn

    建议默认打开该选项

  6. HTML - 表格标签相关

    <html> <head></head> <body> <!-- table (表格) border : 表格的边框 width : 表格的宽 h ...

  7. iOS7 断了统计和追踪用户的后路

    评论里大家都认可用identifierForVendor 然后用keychain和iCloud各保存一份. 看来这是接近最终结果的办法了. 官方文档又说了下面的话, 又有点费解. 我们只要把最后一组s ...

  8. Mac 下搭建vue开发环境

    tips:一定要有翻墙工具如lanter,另外要保证网速OK. 1. 首先需要安装homebrew liukingdeMBP:~ liuking$ /usr/bin/ruby -e "$(c ...

  9. C++系列作业

    1.编写一个完整的程序,实现功能:向用户提问“现在正在下雨吗?”,提示用户输入Y或N.若输入为Y,显示“现在正在下雨.”:若输入为N,显示“现在没有下雨”:否则继续提问“现在正在下雨吗?” #incl ...

  10. Linux虚拟机ip为127.0.0.1的处理

    Redhat系列(Cnetos)打配置文件在/etc/sysconfig/network-scripsts/ifcfg-eth0(在Centos6.5开始就有这种情况了) 打开配置文件找到ONBOOT ...