GYM100633J. Ceizenpok’s formula 扩展lucas模板
2.0 s
256 MB
standard input
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.
Single line contains three integers n, k, m, separated with spaces (1 ≤ n ≤ 1018, 0 ≤ k ≤ n, 2 ≤ m ≤ 1 000 000).
Write the formula value for given arguments n, k, m.
2 1 3
2
4 2 5
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模板的更多相关文章
- 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 ...
- codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT
默默敲了一个下午,终于过了, 题目传送门 扩展Lucas是什么,就是对于模数p,p不是质数,但是不大,如果是1e9这种大数,可能没办法, 对于1000000之内的数是可以轻松解决的. 题解传送门 代码 ...
- bzoj 2142 礼物——扩展lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 没给P的范围,但说 pi ^ ci<=1e5,一看就是扩展lucas. 学习材料 ...
- 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 ...
- codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula【扩展lucas】
传送门 [题意]: 求C(n,k)%m,n<=108,k<=n,m<=106 [思路]: 扩展lucas定理+中国剩余定理 #include<cstdio> usi ...
- Ceizenpok’s formula Gym - 100633J 扩展Lucas定理 + 中国剩余定理
http://codeforces.com/gym/100633/problem/J 其实这个解法不难学的,不需要太多的数学.但是证明的话,我可能给不了严格的证明.可以看看这篇文章 http://ww ...
- 扩展CRT +扩展LUCAS
再次感谢zyf2000超强的讲解. 扩展CRT其实就是爆推式子,然后一路合并,只是最后一个式子上我有点小疑惑,但整体还算好理解. #include<iostream> #include&l ...
- [学习笔记]扩展LUCAS定理
可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...
- BZOJ - 2142 礼物 (扩展Lucas定理)
扩展Lucas定理模板题(貌似这玩意也只能出模板题了吧~~本菜鸡见识鄙薄,有待指正) 原理: https://blog.csdn.net/hqddm1253679098/article/details ...
随机推荐
- 6_4.springboot2.x数据整合springData介绍
介绍 Spring Data 项目的目的是为了简化构建基于Spring 框架应用的数据访问技术,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关系数据库的访问支持. spr ...
- 谈谈HINT /*+parallel(t,4)*/在SQL调优中的重要作用
/*+parallel(t,4)*/在大表查询等操作中能够起到良好的效果,基于并行查询要启动并行进程.分配任务与系统资源.合并结果集,这些都是比较消耗资源,但我们为能够减少执行事务的时间使用paral ...
- (十一)Json文件配置
接上一节,新建一个项目:JsonConfigSample 依然添加Microsoft.AspNetCore.All 在项目下新建一个Class.json配置文件 { ", "Cla ...
- html--双飞翼布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CodeForces-510D
https://vjudge.net/problem/CodeForces-510D题目可以转化为花最小代价选一些数,然后这些数可以经过加减运算得到1或-1,不然1你就凑不出来,一旦凑出来1,其他的都 ...
- Mac配置maven环境命令
1.安装:解压下载好的maven的文件,解压到你想要的文件夹底下. 2.配置 1)打开终端输入命令 vim ~/.bash_profile (编辑环境变量配置文件) 2)按下i,进入编辑模式 3)在环 ...
- window 下mongodb 配置
1.下载mongodb-win32-x86_64-2008plus-ssl-v3.6-latest 解压到 D:\mongodb 2.cmd => path是否有环境变量 如果没有请配置 3.创 ...
- LUOGU P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)
传送门 解题思路 学习了一下玄学算法--模拟退火,首先要求平衡处,也就是求势能最小的地方,就是求这个点到所有点的距离*重量最小.剩下的几乎是模拟退火的板子了. #include<iostream ...
- win10系统,vbox下安装centos6/7,挂载实现目录共享
用下载好的iso文件,新建虚拟机(所有步骤默认下一步即可). 我用的centos版本:CentOS-7-x86_64-Minimal-1908.iso ISO下载地址 设置虚拟机(指定好镜像后,先不要 ...
- 利用refind实现UEFI多系统引导
使用DiskGenius Pro给ESP分区指定盘符,目的是为了让ESP分区在硬盘上可见 使用BOOTICE工具中的UEFI选项卡中的功能调整引导顺序 修改启动序列-->EFI NetWork- ...