-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCSQUARE_re.cpp
More file actions
45 lines (31 loc) · 902 Bytes
/
CSQUARE_re.cpp
File metadata and controls
45 lines (31 loc) · 902 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
#include<stdio.h>
char b[300];
long int a;
long int m;
long long int answer(long int a,int c){
long int ans=1;
while(c-1>=0){
if((b[c-1]-'0')==1)
ans=(ans*(a%m))%m;
if((b[c-1]-'0')==2)
ans=(ans*((a*a)%m))%m;
a=(((a*a)%m)*a)%m;
c--;
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t>0){
scanf("%ld %s %ld",&a,&b,&m);
//printf("b:%s\n",b);
int cntr=0;
for(int i=0;b[i]!='\0';i++)
cntr++;
//printf("cntr:%d\n",cntr);
printf("%d\n",answer(a,cntr));
t--;
}
return 0;
}