用于大组合数对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. javascript设计模式学习之十六——状态模式

    一.状态模式的定义 状态模式的关键是区分事务内部和外部的状态,事务内部状态改变往往会带来事务的行为改变. 状态模式中有意思的一点是,一般我们谈到封装,都是优先封装对象的行为,而非对象的状态.但在状态模 ...

  2. 使用mysql 的docker

    sudo docker run --name phpmyAdmin3 --link some-mysql:mysql -d phpmyadmin/phpmyadmin:latest -p 8080:8 ...

  3. mysql -prompt选项

    使用-pormpt修改提示符.可以在登录时或者在登录后使用prompt选项来修改提示符 (1)使用mysql命令行参数修改提示符 # mysql -u root -p Enter password: ...

  4. 使用已有PDB克隆PDB

    使用已有PDB克隆PDB $ sqlplus '/as sysdba' SQL*Plus: Release Production on Tue Jun :: Copyright (c) , , Ora ...

  5. Mybatis的批量CRUD

    CRUD与批量CRUD 分页前后缀(方言 Mysql与Oracle情况下不一样) 批量插入数据  http://chenzhou123520.iteye.com/blog/1583407/ 亟待完善 ...

  6. php:mysqli扩展

    mysqli功能概述:http://php.net/manual/zh/mysqli.summary.php 代码(基本的用法):貌似可以防止sql注入 $root = "root" ...

  7. 示sudo: cd: command not found

    执行sudo cd 时出现 sudo: cd: command not found 原因shell shell是一个命令解析器 所谓shell是一个交互式的应用程序. shell执行外部命令的 时候, ...

  8. Leetcode: Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  9. Google 推出的 Java 编码规范(转)

    原文地址:http://www.dahuatu.com/1225/988516.html 原文地址:http://www.dahuatu.com/1225/988516.html 原文地址:http: ...

  10. 去掉字符串中的空格 JS JQ 正则三种不同写法

    <script> function trim(str) { return str.replace(/(^\s*|\s*$)/g, "") } console.log(t ...