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 ...
随机推荐
- 转: jquery中ajax回调函数使用this
原文地址:jquery中ajax回调函数使用this 写ajax请求的时候success中代码老是不能正常执行,找了半天原因.代码如下 $.ajax({type: 'GET', url: " ...
- LeetCode 283 Move Zeroes 解题报告
题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...
- websocketd
https://www.cnblogs.com/tinywan/p/6826125.html https://www.jianshu.com/p/63afd0099565
- 浅谈KMP算法
一.介绍 烤馍片KMP算法是用来处理字符串匹配问题的.比如说给你两个字符串A,B,问B是不是A的子串? 比如,eg就是aeggx的子串 一般讲字符串A称为主串,用来匹配的B串称为模式串 定义n为字符串 ...
- 异常 java.net.ConnectException: Connection refused: no further information
java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImp ...
- MYSQL的价格
MYSQL的价格 来自:http://www.greatlinux.com/column/column.do?nodeid=2c90c6093416705c013416f283f40004&c ...
- what's the 回抽
出自 MBA智库百科(https://wiki.mbalib.com/) 什么是回抽 由于近年来证券投资领域中技术分析的不断普及,许多投资者都认识到了股价(汇价)一旦突破颈线压力,后市看好.但是还有一 ...
- Python3学习之路~6.7 经典类和新式类的继承顺序
在Python中,经典类(class Person:)和新式类(class Person(object):)的主要区别就是体现在多继承的顺序上. Python 2.x中默认都是经典类,只有显式继承了o ...
- shell文件查找和压缩命令
locate命令 1.locate filename 模糊匹配 只要文件名中包含关键字即可 非实时查找,速度比find快. 依靠索引数据库 /var/lib/mlocate/m ...
- shell脚本编写实例
实际案例 1.判断接收参数个数大于1 [ $# -lt 1 ] && echo "至少需要一个参数" && { echo "我要退出了.. ...