用于大组合数对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定理模板的更多相关文章

  1. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  2. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...

  3. Lucas定理模板【bzoj2982】【combination】

    (上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...

  4. 【组合数+Lucas定理模板】HDU 3037 Saving

    acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...

  5. BZOJ 4403 2982 Lucas定理模板

    思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...

  6. lucas定理 模板

    lucas定理 (nm)&VeryThinSpace;mod&VeryThinSpace;p=(⌊np⌋⌊mp⌋)(n&VeryThinSpace;mod&VeryTh ...

  7. HDU 3037 Saving Beans(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  8. xdoj-1057(Lucas定理的证明及其模板)

    Lucas定理的证明: 转自百度百科(感觉写的还不错) 首先你需要这个算式:    ,其中f > 0&& f < p,然后 (1 + x) nΞ(1 + x) sp+q Ξ ...

  9. 组合数取模&&Lucas定理题集

    题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020  输出组合数C(n, m) mod p (1 ...

随机推荐

  1. Maven 迁移local repository

    1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问,一般默认的地址:http://search.maven.org/ ...

  2. Java 收集的代码 transient

    public class Main { public static void main(String[] args) { ((NULL)null).haha(); } } class NULL { p ...

  3. yaffs2文件系统镜像分析

    概述 yaffs2文件系统镜像通过mkyaffs2img工具制作,由源码可编译出两个镜像工具mkyaffsimage和mkyaffs2image,其中mkyaffsimage是针对yaffs文件系统, ...

  4. RouterOS软路由常用命令

    修改用户密码 [admin@MikroTik]>/user                               #进入操作路径 [admin@MikroTik]/user>prin ...

  5. 导出iPhone中安装的APP的iPA文件

    1.让iPhone连接电脑,打开iTunes,选择本电脑,然后点立即备份. 2.完成上一步的操作之后,选择应用,在iTunes中就会列出你从App Store中下载的应用,自己连接真机调试的应用是没有 ...

  6. Lock file left by a different patch, OPatch will not try re-using the lock file.

    OPatch在打补丁的过程中被中断,重新执行后报以下错误: UtilSession failed: Lock file left by a different patch, OPatch will n ...

  7. Redis:安装

    此篇文章并不介绍linux下安装方法.围绕windows安装而介绍 两种安装方式: 1)下载压缩包,解压后(运行起来有个窗口,关闭掉就不在运行),没有windows服务被注册:可以只用命令在cmd将其 ...

  8. 2-sat 输出任意一组可行解&拓扑排序+缩点 poj3683

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8170   Accept ...

  9. 解决Xamarin 生成时出现 “aapt.exe”已退出,代码为 1。错误问题

    项目中添加的资源或项目文件的名称不能包含 空格 横线 特殊符号 或者 Android关键字 等

  10. [原创]安装Oracle 11gR2,以及如何在win8下使用plsql develper连接Oracle数据库 ,在这里和大家分享下

    一,关于win8下安装Oracle 11gR2 1.我下载的是Oracle_11gR2_win64.其中有两个包: 注意:在解压了之后将:win64_11gR2_database_2of2\datab ...