Problem Description
On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pick m different apples among n of them and modulo it with M . M is the product of several different primes.
 
Input
On the first line there is an integer T(T≤20) representing the number of test cases.

Each test case starts with three integers n,m,k(1≤m≤n≤10^18,1≤k≤10) on a line where k is the number of primes. Following on the next line are k different primes p1,...,pk . It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤10^18 and pi≤10^5 for every i∈{1,...,k}.

 
Output
For each test case output the correct combination on a line.
 
Sample Input
1
9 5 2
3 5
 
Sample Output
6

题目要求一个大组合数模几个素数乘积的结果。

大组合那块能通过Lucas得到对每个素数模的结果。

然后再通过互质型的中国剩余定理可以得到最终结果。

不过我的模板中国剩余定理里面乘法部分会爆long long。

然后用大数优化了乘法部分,通过大数乘大数,模完后返回小数。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long
#define UNIT 10 using namespace std; const int maxK = ;
LL n, m, s[maxK], prime[maxK];
int k; struct Bignum
{
int val[];
int len; Bignum()
{
memset(val, , sizeof(val));
len = ;
} Bignum operator=(const LL &a)
{
LL t, p = a;
len = ;
while (p >= UNIT)
{
t = p - (p/UNIT)*UNIT;
p = p / UNIT;
val[len++] = t;
}
val[len++] = p;
return *this;
} Bignum operator*(const Bignum &a) const
{
Bignum x;
int i, j, up;
int x1, x2;
for (i = ; i < len; i++)
{
up = ;
for (j = ; j < a.len; j++)
{
x1 = val[i]*a.val[j] + x.val[i+j] + up;
if (x1 >= UNIT)
{
x2 = x1 - x1/UNIT*UNIT;
up = x1 / UNIT;
x.val[i+j] = x2;
}
else
{
up = ;
x.val[i+j] = x1;
}
}
if (up != )
x.val[i+j] = up;
}
x.len = i + j;
while (x.val[x.len-] == && x.len > )
x.len--;
return x;
} LL operator%(const LL &a) const
{
LL x = ;
for (int i = len-; i >= ; --i)
x = ((x*UNIT)%a+val[i]) % a;
return x;
}
}; LL mulMod(LL x, LL y, LL p)
{
LL ans = ;
Bignum xx, yy;
xx = x;
yy = y;
xx = xx*yy;
ans = xx%p;
return ans;
} LL quickMod(LL a, LL b, LL p)
{
LL ans = ;
a %= p;
while (b)
{
if (b&)
{
ans = ans*a%p;
b--;
}
b >>= ;
a = a*a%p;
}
return ans;
} LL C(LL n, LL m, LL p)
{
if (m > n)
return ;
LL ans = ;
for(int i = ; i <= m; i++)
{
LL a = (n+i-m)%p;
LL b = i%p;
ans = ans*(a*quickMod(b, p-, p)%p)%p;
}
return ans;
} LL Lucas(LL x, LL y, LL p)
{
if (y == )
return ;
return C(x%p, y%p, p)*Lucas(x/p, y/p, p)%p;
} //EXGCD
//求解方程ax+by=d,即ax=d mod(b)
//扩展可求逆元
//O(logn)
void exgcd(LL a, LL b, LL &x, LL &y, LL &d)
{
if (b == )
{
x = ;
y = ;
d = a;
}
else
{
exgcd(b, a%b, y, x, d);
y -= a/b*x;
}
} //中国剩余定理(互质)
//其中a为除数数组,n为模数数组
LL CRT(LL *a, LL *n, int len)
{
LL N = , ans = ;
for (int i = ; i < len; i++)
{
N *= n[i];
}
for (int i = ; i < len; i++)
{
LL m = N / n[i];
LL x, y, d;
exgcd(m, n[i], x, y, d);
x = (x%n[i] + n[i]) % n[i];
//ans = (ans + m*a[i]*x%N) % N;
ans = (ans + mulMod(mulMod(m, a[i], N), x, N)) % N;
}
return ans;
} void input()
{
scanf("%I64d%I64d%d", &n, &m, &k);
for (int i = ; i < k; ++i)
scanf("%I64d", &prime[i]);
} void work()
{
for (int i = ; i < k; ++i)
s[i] = Lucas(n, m, prime[i]);
LL ans = CRT(s, prime, k);
printf("%I64d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}

ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)的更多相关文章

  1. ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)

    hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB   描述 There is a strange storehouse in PKU. In this ...

  2. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

  3. ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)

    Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...

  4. ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

    Problem Description In Land waterless, water is a very limited resource. People always fight for the ...

  5. ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

    Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...

  6. hdu 5443 (2015长春网赛G题 求区间最值)

    求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...

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

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

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

  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. Root(hdu5777+扩展欧几里得+原根)

    Root                                                                          Time Limit: 30000/1500 ...

  2. 【BZOJ3744】Gty的妹子序列 分块+树状数组

    [BZOJ3744]Gty的妹子序列 Description 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzo ...

  3. 重载(overload)、覆盖(override)和隐藏(hide)

    写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖(override),隐藏(hide).在早期的C++书籍中,可能翻译的人不熟悉专业用语(也不能怪他们,他们不是搞计算机编程的, ...

  4. config相关操作(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/04/16/3025315.html,这是一个专题,感觉比较好,有空可以看与一下 System.Con ...

  5. 20179209《Linux内核原理与分析》第十一周作业

    Nmap配合Metasploit进行端口扫描 1.Nmap扫描器基本使用 1.1简介 Nmap(Network Mapper)最早是Linux下的网络扫描嗅探器.其基本功能有三个: 探测一组主机是否在 ...

  6. IM系统中如何保证消息的可靠投递(即QoS机制)(转)

    消息的可靠性,即消息的不丢失和不重复,是im系统中的一个难点.当初qq在技术上(当时叫oicq)因为以下两点原因才打败了icq:1)qq的消息投递可靠(消息不丢失,不重复)2)qq的垃圾消息少(它an ...

  7. 写出pythonic的python代码

    http://www.cnblogs.com/dadadechengzi/p/6226071.html 1,列表推导(没当要对序列中的内容进行循环处理时,就应该尝试使用列表推倒) 在python中编写 ...

  8. 【三】MongoDB文档的CURD操作

    一.插入文档 使用insert方法插入文档到一个集合中,如果集合不存在创建集合,有以下几种方法: db.collection.insertOne({}):(v3.2 new)  #插入一个文档到集合中 ...

  9. Referrer-Policy常见属性

    Referrer-Policy(来源协议)用来规定什么情况下显示Referer字段及refer字段内显示多少信息. 备注: referer实际上是对referrer的误写,因为写错的人多了也就正确了. ...

  10. 斯坦福机器学习视频笔记 Week3 逻辑回归与正则化 Logistic Regression and Regularization

    我们将讨论逻辑回归. 逻辑回归是一种将数据分类为离散结果的方法. 例如,我们可以使用逻辑回归将电子邮件分类为垃圾邮件或非垃圾邮件. 在本模块中,我们介绍分类的概念,逻辑回归的损失函数(cost fun ...