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 ...
随机推荐
- 转:HashMap实现原理分析(面试问题:两个hashcode相同 的对象怎么存入hashmap的)
原文地址:https://www.cnblogs.com/faunjoe88/p/7992319.html 主要内容: 1)put 疑问:如果两个key通过hash%Entry[].length得 ...
- winform窗体启动过程
窗体启动执行顺序:FormShowFormPaintFormActivateFormResize 关闭窗体过程FormCloseFormDestroy 最小化再最大化:FormPaintFormRes ...
- Innodb semi-consistent 简介
A type of read operation used for UPDATE statements, that is a combination of read committed and con ...
- MapStruct
一.Object mapping 的技术分类: 运行期 反射调用set/get 或者是直接对成员变量赋值 . 该方式通过invoke执行赋值,实现时一般会采用beanutil, Javassist等开 ...
- 5分钟简述Spring中的DI与AOP
Spring的两个核心特性: 依赖注入(dependency injection,DI) 面向切面编程(aspect oriented programming,AOP) 依赖注入(dependency ...
- MySQL忘记root密码--不重启mysqd重置root密码
先提个问题:如何不重启mysqld,且没有权限修改用户账号和权限的情况下,如何重新设置root密码?不知道没关系,在此之前我也是不知道如何操作的,先看看下面的几种重置root密码的方法. 1.skip ...
- perfmon——使用windows系统自带的性能监视器监控进程信息
第一次使用perfmon监控应用进程的信息,步骤总结如下: 第一部分 性能监视器 1.快捷键Win+R打开运行界面,输入“perfmon”命令后回车即可打开windows的性能监视器 2.点击“性能监 ...
- Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'.
VS 里面的Interop.Shell32.dll(1.0) 这个版本太低了,需要高版本的(我使用的是1.2.107.0) 具体下载地址 自己找吧
- EditText的一些使用技巧
1.让EditText不自动获取焦点 将EditText的某个父级控件设置成 android:focusable="true" android:focusableInTouchMo ...
- 异常Exception分类
1:编译时被检测异常:只要有是Exception和其子类都是,除了特殊子类RuntimeException体系. 这种问题已但出现,希望在编译时进行检测,让这种问题有对应处理方式 ...