[原创]HDU 5446 Unknown Treasure [lucas+CRT]【数论】

2016-08-11 20:13:29 Tabris_ 阅读数:224


博客爬取于2020-06-14 22:43:52
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/52186122


题目连接:传送门
------------------------------.
Unknown Treasure

Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2196 Accepted Submission(s): 814

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
------------------------------.
题目大意:
就是求C(n,m)%M ,M= p1p2p3p4…*pn;

题目解释:
大组合数就是用lucas定理求解
而lucas要求是对质数取模的时候才成立
所以分别对p[i] 进行求解 然后构成了一个同余方程组
用中国剩余定理求解就行了

注意大数乘法的时候可能会爆longlong 所以要用快速乘

附本题代码
------------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# include <stdio.h>
# include <vector>
# include <iostream>
# include <stdlib.h>
using namespace std;
# define LL long long int
# define pb push_back



LL qmul(LL a,LL b,LL c)
{
LL res=0;
while(b)
{
if(b&1) res=(res+a)%c;
a=(a+a)%c;
b>>=1;
}
return res;
}
LL qmod(LL a,LL b,LL c)
{
LL res=1;
while(b)
{
if(b&1) res=qmul(res,a,c)%c;
b>>=1;
a=qmul(a,a,c)%c;
}
return res;
}
LL exgcd(LL a,LL b,LL &x,LL &y) //ax+by=d
{

if(!b)
{
x=1;
y=0;
return a;
}
else
{
LL r=exgcd(b,a%b,x,y);
LL t = x;
x = y;
y=t-a/b*x;
return r;
}
}

LL CRT(LL a[],LL m[],LL len) //x%m[i]=a[i]
{
LL i,x,y,M,n=1,ret=0;
for(i=0; i<len; ++i) n*=m[i];
for(i=0; i<len; ++i)
{
M=n/m[i];
exgcd(M,m[i],x,y);
ret=(ret+qmul(qmul(x,M,n),a[i],n))%n;
}
return (ret+n)%n;
}



LL C(LL n,LL m,LL p)//组合数模素数P
{
if(m>n||m<0) return 0;
if(n-m<m) m=n-m;
LL a=1,b=1;
for(int i=0; i<m; ++i)
{
a=a*(n-i)%p;
b=b*(m-i)%p;
}
return a*qmod(b,p-2,p)%p;
}

LL Lucas(LL n,LL m,LL p)
{
LL ans=1;
while(n&&m&&ans)
{
ans=ans*C(n%p,m%p,p)%p;
n/=p,m/=p;
}
return ans;
}

LL a[11],p[11];

int main()
{
int _;
scanf("%d",&_);
while(_--)
{
LL n,m;
int k;
scanf("%I64d%I64d%d",&n,&m,&k);

LL ans = 0;
for(int i=0; i<k; i++)
{
scanf("%I64d",&p[i]);
a[i] = Lucas(n, m, p[i]);
// printf("%I64d ",a[i]);
}
// puts("");
LL sum = CRT(a,p,k);
printf("%I64d\n",sum);
}
return 0;
}