-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathD.cpp
65 lines (50 loc) · 1.23 KB
/
D.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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<int,pii> p3i;
typedef long double ld;
typedef vector<ld> vd;
#define FOR(i,a,b) for (int i(a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define UN(v) sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define CL(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define SORT(a) sort((a).begin(),(a).end())
int main(){
char s1[1111111];
char s2[1111111];
int c;
scanf(" %s %s %d",s1,s2,&c);
ll x = 0;
int q = strlen(s1);
REP(i,q)
x=10*x+s1[i]-'0',x%=c;
ll b = x;
ll b1 = (x-1+c)%c;
int w= strlen(s2);
s2[w-1]--;
int pos = w-1;
while(s2[pos]<'0'){
s2[pos]+=10;
pos--;
s2[pos]--;
}
ll res = 1;
ll t = b;
for(int i = w-1;i>=0;i--){
ll add = 1;
REP(j,s2[i]-'0') add *= t,add%=c;
res *= add,res%=c;
ll tmp = t;
t=1;
REP(j,10) t*=tmp,t%=c;
}
res*=b1;
res%=c;
if(res==0) cout<<c<<endl;
else cout<<res<<endl;
return 0;
}