-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcc_GCDMOD.cpp
68 lines (55 loc) · 1.29 KB
/
cc_GCDMOD.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
/*
Author : Abhinav
Modified : 05-08-2018 01:22:19 AM
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
#define faster ios_base::sync_with_stdio(false);cin.tie(0);
const ll mod = 1e9+7;
#define rep(i,j,k) for(ll i=j; i<k; i++)
#define repv(i,j,k) for(ll i=j; i>k; i--)
ll power(ll x, ll y, ll m){
ll res = 1;
x = x % m;
while (y > 0){
if (y & 1){
//cout << x << " " << res << "\n";
res = (res%m*x%m) % m;
//cout << x << " " << res << "\n";
}
y = y>>1;
x = (x%m*x%m) % m;
}
return res;
}
ll gcd(ll a, ll b){
if (!a)
return b;
return gcd(b%a,a);
}
ll reduceB(ll a, char b[]){
ll md = 0;
for (int i=0; i<strlen(b); i++)
md = (md*10 + b[i] - '0')%a;
return md;
}
ll gcdLarge(ll a, char b[]){
ll num = reduceB(a, b);
return gcd(a, num);
}
int main(){
faster;
ll t,a,b,n;
cin >> t;
while(t--){
cin >> a >> b >> n;
//cout << ((ll)power(a,n) + (ll)power(b,n)) << " " << abs(a-b) << "\n";
if(a-b == 0)
cout << (power(a,n,mod)%mod + power(b,n,mod)%mod)%mod << "\n";
else
cout << (gcd((power(a,n,abs(a-b))%(abs(a-b)) + power(b,n,abs(a-b))%(abs(a-b)))%abs(a-b),abs(a-b)))%mod << "\n";
}
return 0;
}