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 抽象类 abstract
package cn.sasa.com; //抽象类 被abstract修饰的类 public abstract class Animal { //抽象类的成员变量 的定义 与 一般类是一样的 pri ...
- 关于linux特殊含义的转义符\033
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...
- 对比库表结构,生成SQL
网上找了一圈对比库的工具,能手工生成差别的SQL的工具没有,改造了一下网上的sql 1, 获取字段名的类型 create FUNCTION [dbo].[getColType](@tab varcha ...
- 010-java 表单方式或者base64方式上传图片,后端使用nutz的post转发图片到另一个请求
本地上传图片 方式一.使用表单方式上传-enctype <form enctype="multipart/form-data" method="post" ...
- xtrabackup全库还原+binlog日志还原
1.场景 mysql数据库误删某个库.误删表或者误删除数据 如下模拟图:备份策略定为每天凌晨进行全库备份,在B时间点进行了误操作以后,有两种恢复场景,一种是恢复到B时间点误操作前,一种是恢复到C时间点 ...
- fasttext学习笔记
When to use FastText? The main principle behind fastText is that the morphological structure of a wo ...
- huawei
线程堆栈(Thread Stack)和托管堆(Managed Heap) 每个正在运行的程序都对应着一个进程 (process),在一个进程内部,可以有一个或多个线程(thread),每个线程都拥有一 ...
- css3--单行、多行文本溢出
<style> .div1 { width: 200px; height: 200px; background: red url(img/user.png) no-repeat; text ...
- SQLite之C#连接SQLite
SQLite是一个开源.免费的小型的Embeddable RDBMS(关系型数据库),用C实现,内存占用较小,支持绝大数的SQL92标准,现在已变得越来越流行,它的体积很小,被广泛应用于各种不同类型的 ...
- RSA 加解密 秘钥对说明
rsa非对称加密, 加解密需要不同的秘钥,称作一对. rsa加解密分两种,第一:公钥加密私钥解密.第二:私钥加密公钥解密. 需要注意的是,公加私解得到的密文是变化的,而私加公解的得到的密文是固定的. ...