HDU 5446 lucas CRT
n中选m个模M,M为多个素数之积 $n, m, k (1 \leq m \leq n \leq 10^{18}, 1 \leq k \leq 10)$,$M = p_1 · p_2 · · · p_k ≤ 10^{18}$,$p_i \leq 10^5$
由于n,m很大组合数自然想到lucas,但是如果直接用M会因为M太大lucas就没什么用了,所以考虑以构成M的素因子为模数分别对组合数的lucas构建k个同余方程,这样就能得到模M下组合数了。了解题目意思后就很裸了
注意每个不同模数下的逆元、阶乘的模数也不同阿...
/** @Date : 2017-10-11 12:56:59
* @FileName: J.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; LL fac[N];
LL inv[N];
LL p[20];
LL r[20];
LL mod;
void init(int n, LL mod)
{
fac[0] = fac[1] = 1;
inv[0] = inv[1] = 1;
for(int i = 2; i < n; i++)
{
fac[i] = fac[i - 1] * i % mod;
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
for(int i = 2; i < n; i++)
(inv[i] *= inv[i - 1]) %= mod;
} LL C(LL n, LL m, LL mod)
{
if(m > n)
return 0;
LL ans = 0;
ans = ((fac[n] * inv[m] % mod)* inv[n - m]) % mod;
return ans;
} LL lucas(LL n, LL m, LL mod)
{
if(m == 0)
return 1;
return C(n % mod, m % mod, mod) * lucas(n / mod, m / mod, mod) % mod;
} LL exgcd(LL a, LL b, LL &x, LL &y)
{
LL d = a;
if(b == 0)
x = 1, y = 0;
else
{
d = exgcd(b, a % b, y, x);
y -= (a / b) * x;
}
return d;
} LL mul(LL a, LL b, LL mod)
{
while(a < 0)
a += mod;
while(b < 0)
b += mod;
LL ans = 0;
while(b)
{
if(b & 1)
ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans;
} LL CRT(LL n, LL rem[], LL mod[])
{
LL M = 1, x, y;
for(int i = 0; i < n; i++)
M *= mod[i];
LL res = 0;
for(int i = 0; i < n; i++)
{
LL t = M / mod[i];
exgcd(t, mod[i], x, y);
res = (res + mul(mul(t , rem[i], M), x, M)) % M;
} return (res % M + M) % M;
} int main()
{
int T;
cin >> T;
while(T--)
{
LL n, m, k;
scanf("%lld%lld%lld", &n, &m, &k);
mod = 1LL;
for(int i = 0; i < k; i++)
{
scanf("%lld", p + i);
init(p[i], p[i]);
r[i] = lucas(n, m, p[i]);
}
LL ans = CRT(k, r, p);
printf("%lld\n", ans);
}
return 0;
}
HDU 5446 lucas CRT的更多相关文章
- hdu 5446 lucas+crt+按位乘
http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意:题目意思很简单,要你求C(n,m)mod p的值 p=p1*p2*...pn; 题解:对于C(n,m ...
- 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.. ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
- hdu 5446 Unknown Treasure lucas和CRT
Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】
Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number o ...
- HDU 5446 CRT+Lucas+快速乘
Unknown Treasure Problem Description On the way to the next secret treasure hiding place, the mathem ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
随机推荐
- 我是一名IT小小鸟
我是一只it小小鸟 书中介绍了it界大牛们大学期间的学习方法和对未来的职业规划,相比他们,自我感觉相距甚远,对这学科的热情程度也远远比不上他们. 就拿目前数据结构这门高深的课程,应通过更多的课外扩展来 ...
- Sprint Boot入门(1):创建第一个Spring Boot应用
搭建工程 注:建议使用eclipse的STS插件创建Spring项目,而不是下面的Gradle项目,否则会导致有一些Spring文件不存在. new Gradle Project,如下 点next,如 ...
- angularJS1笔记-(19)-angular异步加载包的方式
我们平时写的导入包的方式都是同步方式,有时候会显得过于卡顿,这样我们就可以使用异步加载的方式. script.js方式: 执行结果为: 异步加载还可以加载多个即为script([,,,],functi ...
- ajax 数据请求(一)同域
参考:http://www.css88.com/jqapi-1.9/jQuery.ajax/ http://www.cnblogs.com/haitao-fan/p/3908973.html 1.常用 ...
- matlab for 运算的提速
[1]主要思想:matlab是按列存储,定义s(nums,1)比定义s(1,nums)要快哦 需要重复query的元素看看能不能再for之前就定义好 经典案 ...
- list 交换位置扩展
public static List<T> Swap<T>(this List<T> list, int index1,int index2) { if(index ...
- P3758 [TJOI2017]可乐
题目描述 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且放在了加里敦星球的1号城市上.这个可乐机器人有三种行为: 停在原地,去下一个相邻的城市,自爆.它每一秒都会随机 ...
- 【刷题】BZOJ 5415 [Noi2018]归程
www.lydsy.com/JudgeOnline/upload/noi2018day1.pdf Solution 考试的时候打的可持久化并查集,没调出来QAQ 后面知道了kruskal重构树这个东西 ...
- 美团codeM之美团代金券
前天做了下美团的一个codeM比赛的资格赛,遇到一个题目挺有意思的,所以现在做一下总结. 题目描述 美团的每一个用户都有一个用户代金券的消费记录日志,每位用户都能购买若干种代金券,但是每一种代金券最多 ...
- Android Studio & eclipse 调试技巧
如上图设置多个断点,开启调试.想跨断点移动到下一个断点,点击如下图1箭头,程序将运行一个断点到下一个断点之间需要执行的代码.如果后面代码没有断点,再次点击该按钮将会执行完程序.点击箭头2指向的按钮,可 ...