-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLucas.txt
More file actions
47 lines (45 loc) · 861 Bytes
/
Lucas.txt
File metadata and controls
47 lines (45 loc) · 861 Bytes
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
#include<iostream>
#include<string>
using namespace std;
#define lld long long
lld Ext_gcd(lld a,lld b,lld &x,lld &y){
if(b==0) { x=1, y=0; return a; }
lld ret= Ext_gcd(b,a%b,y,x);
y-= a/b*x;
return ret;
}
lld Inv(lld a,int m){ ///求逆元
lld d,x,y,t= (lld)m;
d= Ext_gcd(a,t,x,y);
if(d==1) return (x%t+t)%t;
return -1;
}
lld Cm(lld n, lld m, lld p) ///组合数学
{
lld a=1, b=1;
if(m>n) return 0;
while(m)
{
a=(a*n)%p;
b=(b*m)%p;
m--;
n--;
}
return a*Inv(b,p)%p; ///(a/b)%p 等价于 a*(b,p)的逆元
}
lld Lucas(lld n, lld m, lld p) ///把n分段递归求解相乘
{
if(m==0) return 1;
return Cm(n%p,m%p,p)*(lld)Lucas(n/p,m/p,p)%p;
}
int main()
{
lld T,n,m,p;
cin>>T;
while(T--)
{
cin>>n>>m>>p;
cout<<Lucas(n+m,m,p)<<endl;
}
return 0;
}