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的更多相关文章

  1. 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 ...

  2. 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.. ...

  3. 中国剩余定理&Lucas定理&按位与——hdu 5446

    链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...

  4. HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

    HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018 思路:这题基本上算是模版题了 ...

  5. HDU 5446 Unknown Treasure(Lucas定理+CRT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...

  6. hdu 5446 Unknown Treasure lucas和CRT

    Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  7. 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 ...

  8. HDU 5446 CRT+Lucas+快速乘

    Unknown Treasure Problem Description On the way to the next secret treasure hiding place, the mathem ...

  9. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

随机推荐

  1. SDWebImage缓存图片的机制

    SDWebImage是一个很厉害的图片缓存的框架.既ASIHttp+AsyncImage之后,我一直使用AFNetworking集成的UIImageView+AFNetworking.h,但后者对于图 ...

  2. 学习jenv

    背景 生活不只是眼前的苟且, 还有诗和远方. 上个月工作需要启动了一个小项目, 按最初的计划会用JDK8. 但当计划报上去后, 运维部门出于后续升级维护的考虑, 不允许使用已经出来4年多的JDK8了, ...

  3. firewall和iptables

    防火墙有这三种方式,firewalld.iptables.ebtables,现在的centOS7使用的是firewalld. 下面是一些总结: 查看当前firewalld的状态 firewall-cm ...

  4. 数据库事务的四大特性以及4种事务的隔离级别-以及对应的5种JDBC事务隔离级别

    本篇讲诉数据库中事务的四大特性(ACID),并且将会详细地说明事务的隔离级别. 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ 原子性(Atomicity) 原子性是指事务 ...

  5. keydown和KeyPress事件有何不同

    KEYPRESSWhen a windowed control receives a key-press message (WM_CHAR) from Windows, its message han ...

  6. SQL利用Case When Then多条件

    CASE    WHEN 条件1 THEN 结果1    WHEN 条件2 THEN 结果2    WHEN 条件3 THEN 结果3    WHEN 条件4 THEN 结果4.........    ...

  7. python3 执行AES加密方法

    cmd执行命令:pip install pycryptodome # -*- coding: utf-8 -*- # __author__ = 'Carry' import base64 from C ...

  8. Opera官网打不开 下载Opera最新版本的实际地址

    目前Opera官网可以打开,但是点下载时就会出错,国内无法访问Opera的下载地址,无法通过官网直接下载Opera浏览器.下面提供下载的方式. 一.通过官方的ftp站点下载 FTP地址为 http:/ ...

  9. BZOJ 3173 最长上升子序列(树状数组+二分+线段树)

    给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...

  10. 基于c的简易计算器二

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...