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. Java中关于注释、标识符、变量、常量、数据类型、类型转换、转移字符以及数值型的表现形式的详解

    Java文件的注意事项 在同一个Java文件中,可以定义多个类,但是被public修饰的类只能够有一个,并且此类名要与文件名一致. 在同一个类中,可以定义多个方法,但是名字叫做main的方法只能有一个 ...

  2. Python文件操作回顾

    with open("D:/Temp/a.txt", mode='w', encoding='utf-8') as f: f.write('hello') with open(&q ...

  3. marquee标签(跑马灯效果)

  4. C开发系列-数组

    C语言数组 数组:用来存储一组数据. 计算C语言的数组长度 int age1 = 12; int age2 = 15; int age3 = 10; int age4 = 13; int ages[] ...

  5. dataset datatable 转json

    class ToJosn { #region dataTable转换成Json格式 /// <summary> /// dataTable转换成Json格式 /// </summar ...

  6. 数论GCD——cf1055C

    被一道数论题卡了半天 网上的题解说只要匹配l或者r就行,想了下还真是.. 能让r1和r2对其就让他们对其,不能对其就讨论一下两种情况就可以了 #include <bits/stdc++.h> ...

  7. IO流读取和写入文件

    package com.xmlmysql.demo.config; import java.io.BufferedReader; import java.io.BufferedWriter; impo ...

  8. springboot导入excel到mysql

    @Controller @RequestMapping(path = "/excel") public class ImportController { @Autowired pr ...

  9. 07_Spring事务处理

    一.事务概述 数据库的事务: 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等.事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出 ...

  10. <每日一题>题目20:简单python练习题(11-20)

    #11.编写程序,输入一个自然数,输出它的二进制.八进制.十六进制表示形式 Num = input("请输入任性自然数:") Num = eval(Num) print(" ...