-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfermetstheorem.c
54 lines (49 loc) · 955 Bytes
/
fermetstheorem.c
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
#include<stdio.h>
#include<math.h>
#include<string.h>
long long power(long long a,long long r,long long p)
{
long long t=2,re=1;
printf("%lld %lld \n",re,r);
while(r>t)
{
if((long long)pow(a,t*2)%p>=0)
{ re=re*( (long long)(pow(a,t))%p ) %p;
r=r-t;
t=t*2;
}
else
{
if(r>(2*t)){
re=(re*re)%p;
r=r-(2*t);
t=t+t;}
else
{
re=re*( (long long)(pow(a,t))%p ) %p;
r=r-t;
}
}
printf("%lld %lld %lld\n",re,r,t);
}
re=(re*(long long)(pow(a,r)))%p;
return re;
}
void main()
{
long long int a,m,r=0;
int i;
char b[100];
scanf("%lld",&a);
scanf("%s",b);
scanf("%lld",&m);
for(i=0;i<strlen(b);i++)
{
r=r*10 + b[i]-'0';
r=r%(m-1);
}
//printf("%lld\n",r);
//r=(long long)pow(a,r);
printf("%lld",power(a,r,m));
getch();
}