题目链接

求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数。

先求出C(n, m)%pi的值, 然后这就是一个同余的式子。 用中国剩余定理求解。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
ll a[], b[];
void extend_Euclid(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
extend_Euclid(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll mul(ll a, ll n, ll mod)
{
a = (a%mod+mod)%mod;
n = (n%mod+mod)%mod;
ll ret = ;
while(n) {
if(n&)
ret = (ret+a)%mod;
a = (a+a)%mod;
n >>= ;
}
return ret;
}
ll CRT(ll a[],ll m[],int n)
{
ll M = ;
ll ans = ;
for(int i=; i<=n; i++)
M *= m[i];
for(int i=; i<=n; i++)
{
ll x, y;
ll Mi = M / m[i];
extend_Euclid(Mi, m[i], x, y);
ans = (ans + mul(mul(Mi, x, M), a[i], M))%M;
}
return (ans+M)%M;
}
ll pow(ll a, ll b, ll mod)
{
ll ret = ;
while(b) {
if(b&) ret = ret*a%mod;
a = a*a%mod;
b /= ;
}
return ret;
}
ll C(ll n, ll m, ll mod)
{
ll a = , b = ;
for(int i = ; i <= m; i++) {
b = b*i%mod;
a = a*(n-i+)%mod;
}
return a*pow(b, mod-, mod)%mod;
}
ll lucas(ll n, ll m, ll mod)
{
if(m == )
return ;
return lucas(n/mod, m/mod, mod)*C(n%mod, m%mod, mod)%mod;
}
int main()
{
ll t, n, m, k;
cin>>t;
while(t--) {
cin>>n>>m>>k;
for(int i = ; i <= k; i++) {
scanf("%lld", &a[i]);
}
for(int i = ; i <= k; i++) {
b[i] = lucas(n, m, a[i]);
}
ll ans = CRT(b, a, k);
cout<<ans<<endl;
}
return ;
}

hdu 5446 Unknown Treasure 中国剩余定理+lucas的更多相关文章

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

  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. HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

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

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

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

  5. HDU 5446 Unknown Treasure

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  6. HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...

  7. hdu 5446 Unknown Treasure lucas和CRT

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

  8. hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

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

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

随机推荐

  1. spring 配置和实例

    Spring 是一个开源框架.Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring 是一个 IOC(DI) ...

  2. 基于excel9.h的excel处理

    基于excel9.h的excel处理; #include "excel9.h" #include <iostream> using namespace std; cla ...

  3. eclipse tomcat内存设置

    -Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPermSize=512m

  4. python运维开发(七)----面向对象(上)

    内容目录: 面向对象应用场景 类和对象的创建 类中的__init__构造方法 self理解 面向对象的三大特性:封装.继承.多态 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装 ...

  5. NDEF-NFC数据交换格式

    为实现NFC标签.NFC设备以及NFC设备之间的交互通信,NFC论坛(NFC FROUM)定义了称为NFC数据交换格式(NDEF)的通用数据格式.NDEF是轻量级的紧凑的二进制格式,可带有URL,vC ...

  6. Bayesian Formulation on Cooperative Tracking

    Suppose a joint state representing a set of \(N_{n}\) nodes moving in a field\[    \textbf{X}=    \b ...

  7. index seek与index scan

    原文地址:http://blog.csdn.net/pumaadamsjack/article/details/6597357 低效Index Scan(索引扫描):就全扫描索引(包括根页,中间页和叶 ...

  8. Nutch环境搭建

    1. 环境准备 HOST:Ubuntu12.04LTS JDK: jdk-7u45-linux-i586.rpm Nutch:apache-nutch-1.7-bin.tar.gz Solr:solr ...

  9. ios中strong, weak, assign, copy

    copy 和 strong(retain) 区别 1. http://blog.csdn.net/itianyi/article/details/9018567 大部分的时候NSString的属性都是 ...

  10. 让app在ios6上具有ios7的扁平效果

    使用cocoapods在工程中加入UI7Kit,关于UI7Kit请自行google. 加入到工程 如果没安装cocoapods,则安装.(http://www.cocoapods.org) 安装方法: ...