Lucas定理模板
用于大组合数对p取模的计算。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 100010
typedef long long LL; LL m,n,p;
LL Pow(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&)
{
b--;
ans=(ans*a)%mod;
}
b>>=;
a=(a*a)%mod;
}
return ans;
}
LL C(LL n,LL m)
{
if(n<m) return ;
if(m == ) return ;
if(m < ) return ;
LL ans=;
for(int i=;i<=m;i++)
{
ans=ans*(((n-m+i)%p)*Pow(i,p-,p)%p)%p; //除以一个数的话是乘以其逆元,用到费马小定理
}
return ans;
}
LL Lucas(LL n,LL m)
{
if(m==)
return ;
return (Lucas(n/p,m/p)*C(n%p,m%p))%p;
}
int main()
{
while(cin>>n>>m){
p=;
LL l=n+m-,r=n-;
printf("%I64d\n",Lucas(l,r)); //计算C(l, r)
}
return ;
}
Lucas定理模板的更多相关文章
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数
typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...
- Lucas定理模板【bzoj2982】【combination】
(上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...
- 【组合数+Lucas定理模板】HDU 3037 Saving
acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...
- BZOJ 4403 2982 Lucas定理模板
思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...
- lucas定理 模板
lucas定理 (nm) mod p=(⌊np⌋⌊mp⌋)(n mod&VeryTh ...
- HDU 3037 Saving Beans(Lucas定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- xdoj-1057(Lucas定理的证明及其模板)
Lucas定理的证明: 转自百度百科(感觉写的还不错) 首先你需要这个算式: ,其中f > 0&& f < p,然后 (1 + x) nΞ(1 + x) sp+q Ξ ...
- 组合数取模&&Lucas定理题集
题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020 输出组合数C(n, m) mod p (1 ...
随机推荐
- appcon 图标打包
ERROR ITMS-90022: "Missing required icon file. The bundle does not contain an app icon for iPho ...
- Java基础之创建窗口——使窗口在屏幕居中(TryWindow2/TryWindow3)
控制台程序. 1.使用ToolKit对象在屏幕的中心显示窗口,将窗口的宽度和高度设置为屏幕的一半: import javax.swing.JFrame; import javax.swing.Swin ...
- Synchronized及其实现原理
并发编程中synchronized一直是元老级角色,我们称之为重量级锁.主要用在三个地方: 1.修饰普通方法,锁是当前实例对象. 2.修饰类方法,锁是当前类的Class对象. 3.修饰代码块,锁是sy ...
- PostgreSQL9.2.4内核源码结构介绍
PostgreSQL的源代码可以随意获得,其开源协议也允许研究者任意修改,这里介绍一下PostgreSQL的源码结构以及部分实现机制.下载PostgreSQL源代码并减压后,其一级目录结构如下图: P ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- 设置searchBar上右边取消按钮的颜色
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:RGBA(0, 161, 176 ...
- 查看真机的系统中sdk的版本
1.adb devices 确保连接上了真机 2.adb shell 进入android系统 3.进入system目录下 4.查看build.prop文件 cat build.prop
- android命令安装apk时报错:INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
点击下载Genymotion-ARM-Translation.zip 将你的虚拟器运行起来,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行.然后重启你的虚拟机.
- CCF真题之网络延时
201503-4 问题描述 给定一个公司的网络,由n台交换机和m台终端电脑组成,交换机与交换机.交换机与电脑之间使用网络连接.交换机按层级设置,编号为1的交换机为根交换机,层级为1.其他的交换机都连接 ...
- kafka迁移数据目录
问题 先前存储kafka日志的磁盘空间太小,zabbix警报不断,于是加了磁盘,将日志存到新磁盘上. 解决方案 依次在每台机器上操作,保证有机器能响应producer和consumer的操作. 加磁盘 ...