-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverse mod prime fermat's theorem.cpp
72 lines (67 loc) · 1.82 KB
/
inverse mod prime fermat's theorem.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sd(x) scanf("%d", &(x))
#define pii pair<int, int>
#define F first
#define S second
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const int N = 1e6 + 3, mod = 1e6 + 3;
int fact[N], invfact[N];
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (((ll) x) * y) % mod;}
inline int powr(int a, ll b){
int x = 1 % mod;
while(b){
if(b & 1) x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
inline int inv(int a){ return powr(a, mod - 2);}
void pre(){
fact[0] = invfact[0] = 1;
for(int i = 1;i < N; i++) fact[i] = mul(i, fact[i - 1]);
invfact[N - 1] = inv(fact[N - 1]);
for(int i = N - 2; i >= 1; i--) invfact[i] = mul(invfact[i + 1], i + 1);
assert(invfact[1] == 1);
}
inline int C(int n, int k){
if(n < k || k < 0) return 0;
return mul(fact[n], mul(invfact[k], invfact[n - k]));
}
inline int getProd(int x, int y){
if(x == 0) return 0;
int num = y / mod - (x - 1) / mod;
if(num) return 0;
return mul(invfact[(x-1) % mod], fact[y % mod]);
}
int main(){
pre();
int q;
sd(q);
while(q--){
int n, x, d;
sd(x); sd(d); sd(n);
if(d == 0) printf("%d\n", powr(x, n));
else{
x = mul(x, inv(d));
printf("%d\n", mul(powr(d, n), getProd(x, x + n - 1)));
}
}
}