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. python 可变参数函数定义* args和**kwargs的用法

    python函数可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法.其中,*args是可变的positional arguments列表,**kwargs是 ...

  2. html5中form表单新增属性以及改良的input标签元素的种类

    在HTML5中,表单新增了一些属性,input标签也有了更多的type类型,有些实现了js才能实现的特效,但目前有些浏览器不能全部支持.下面是一些h5在表单和input标签上的一些改动. <!D ...

  3. android菜鸟学习笔记22----ContentProvider(二)ContentObserver的简单使用

    现在有这样一个应用A通过ContentProvider提供自己的数据给其他应用,应用B通过ContentResolver获取应用A中提供的数据,并将其展示在ListView中,而应用C通过Conten ...

  4. 合理的布局,绚丽的样式,谈谈Winform程序的界面设计

    转载,不错的学习文章 阅读后,起初不太明白,试验了几次后明白了dev的强大.从事Winform开发很多年了,由于项目的需要,设计过各种各样的界面效果.一般来说,运用传统的界面控件元素,合理设计布局,能 ...

  5. iOS main函数讲解

    int main(int argc, char * argv[]) { @autoreleasepool { //四个参数 主要讲解后面两个参数 /* 第三个参数:UIApplication或者其子类 ...

  6. android 多语言(在APP里面内切换语言)

    创建SharedPreferences的管理类 public class PreferenceUtil { private static SharedPreferences mSharedPrefer ...

  7. 关于树莓派Pi2通过UART连接攀藤G5传感器的python

    1.准备工作:树莓派Pi2板子,攀藤G5传感器 关于树莓派40pin口网上很多,我们只了解与攀藤G5连接的问题 (1)攀藤G5pin1(VCC5v)要注意是5V,有很多板子接的是3V,而树莓派的pin ...

  8. 让你快速上手Runtime(转)

    前言 本篇主要介绍Runtime在开发中的一些使用场景,顺便讲解了下MJExtension的底层实现.如果喜欢我的文章,可以关注我微博:袁峥Seemygo,也可以来小码哥,了解下我们的iOS培训课程. ...

  9. [原创]Scala学习:函数的定义

    方式一:标准的定义函数 def 函数名(参数1: 参数类型,参数2: 参数类型): 返回值类型 = { 函数体 } 例子 def max(x: Int,y: Int): Int ={ if(x > ...

  10. python有哪些关键字?让他自己“吐”出来!

    通过调用库来输出!for循环控制! 源代码: import keyword c = 0 for i in keyword.kwlist: print(i) c += 1 代码截图: 哈哈,关键字: F ...