Unknown Treasure (卢卡斯 + 孙子定理, 模板题)
Unknown Treasure
参考链接 : https://www.cnblogs.com/linyujun/p/5199684.html
卢卡斯定理 : C(n, m) % p = C(n / p, m / p) * C(n%p, m%p) % p;
孙子定理 :https://blog.csdn.net/yskyskyer123/article/details/49032227
先用卢卡斯求出每个素数对大组合数的取模, 再用孙子定理将他们合并;
#include<cstdio>
typedef long long LL;
const int N = + ;
LL mul(LL a, LL b, LL p){//快速乘,计算a*b%p
LL ret = ;
while(b){
if(b & ) ret = (ret + a) % p;
a = (a + a) % p;
b >>= ;
}
return ret;
}
LL fact(int n, LL p){//n的阶乘求余p
LL ret = ;
for (int i = ; i <= n ; i ++) ret = ret * i % p ;
return ret ;
}
void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){
if (!b) {d = a, x = , y = ;}
else{
ex_gcd(b, a % b, y, x, d);
y -= x * (a / b);
}
}
LL inv(LL t, LL p){//如果不存在,返回-1
LL d, x, y;
ex_gcd(t, p, x, y, d);
return d == ? (x % p + p) % p : -;
}
LL comb(int n, int m, LL p){//C(n, m) % p
if (m < || m > n) return ;
return fact(n, p) * inv(fact(m, p), p) % p * inv(fact(n-m, p), p) % p;
}
LL Lucas(LL n, LL m, int p){
return m ? Lucas(n/p, m/p, p) * comb(n%p, m%p, p) % p : ;
}
LL china(int n, LL *a, LL *m){//中国剩余定理
LL M = , ret = ;
for(int i = ; i < n; i ++) M *= m[i];
for(int i = ; i < n; i ++){
LL w = M / m[i];
//ret = (ret + w * inv(w, m[i]) * a[i]) % M;//这句写了会WA,用下面那句
ret = (ret + mul(w * inv(w, m[i]), a[i], M)) % M;
}
return (ret + M) % M;
}
int main(){
int T, k;
LL n, m, p[], r[];
scanf("%d", &T);
while(T--){
scanf("%I64d%I64d%d", &n, &m, &k);
for(int i = ; i < k; i ++){
scanf("%I64d", &p[i]);
r[i] = Lucas(n, m, p[i]);
}
printf("%I64d\n", china(k, r, p));
}
}
Unknown Treasure (卢卡斯 + 孙子定理, 模板题)的更多相关文章
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
- HDU 3037 Saving Beans(Lucas定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- HDU 5446 Unknown Treasure (卢卡斯+CRT
代码: #include"bits/stdc++.h" #define db double #define ll long long #define vec vector<l ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
- 【数论】卢卡斯定理模板 洛谷P3807
[数论]卢卡斯定理模板 洛谷P3807 >>>>题目 [题目] https://www.luogu.org/problemnew/show/P3807 [输入格式] 第一行一个 ...
- POJ 2409 Let it Bead【Polya定理】(模板题)
<题目链接> 题目大意:用k种颜色对n个珠子构成的环上色,旋转.翻转后相同的只算一种,求不等价的着色方案数. 解题分析: 对于这种等价计数问题,可以用polay定理来解决,本题是一道pol ...
- Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)
题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...
- hdu 5446 Unknown Treasure Lucas定理+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
随机推荐
- Java之旅_高级教程_实例_数组
摘自:http://www.runoob.com/java/java-examples.html 1.数组排序及元素查找 以下实例演示了如何使用sort()方法对Java数组进行排序,及如何使用 bi ...
- java 网络编程(四)TCP通讯
客户端: package cn.sasa.TcpDemo; import java.io.IOException; import java.io.InputStream; import java.io ...
- swift 灯光的打开与关闭
func lightBtnAction(sender: UIButton){ let device = AVCaptureDevice.default(for:.video) if device == ...
- git的配置操作
git配置信息 一.配置git config git config user.name 'yourName' git config user.name 'yourEmail@gmail.com' 二. ...
- 转载:caffe中的Reshape层
http://blog.csdn.net/terrenceyuu/article/details/76228317 #作用:在不改变数据的情况下,改变输入的维度 layer { name: " ...
- springMVC(一): 整体请求过程概述
首先用户发送请求,DispatcherServlet实现了Servlet接口 获取url请求对应的处理方法,遍历handlerMappings列表,获取对象HandlerExecutionChain( ...
- c语言递归函数的调用
int fun(); int main() { int n,sum=0,i; scanf("%d",&n); for (i=1; i<=n; i++) { sum+= ...
- 前端 HTML body标签相关内容 常用标签 定义列表<dl>
定义列表<dl> 定义列表的作用非常大. <dl>英文单词:definition list,没有属性.dl的子元素只能是dt和dd. <dt>:definition ...
- 帝国cms搜索关键字调用标签(showsearch)怎么用
前面ytkah介绍了如何让帝国CMS7.2搜索模板支持动态标签调用,现在我们来说说怎么调用帝国cms搜索关键字调用标签(showsearch).在帝国cms后台那边的使用方法:[showsearch] ...
- OC分割输入验证码的视觉效果
效果图: 用到的类: UITextField+VerCodeTF.h #import <UIKit/UIKit.h> @protocol VerCodeTFDelegate <UIT ...