Pabon_SEC

MODEX

Jun 3rd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int Big_Mod(int base,int power,int mod)
  6. {
  7.     int a,b;
  8.  
  9.     if(power==0)
  10.     {
  11.         return 1;
  12.     }
  13.     else if(power%2==1)
  14.     {
  15.         a = base%mod;
  16.  
  17.         b = (Big_Mod(base,power-1,mod))%mod;
  18.  
  19.         return (a*b)%mod;
  20.     }
  21.     else if(power%2==0)
  22.     {
  23.         a=(Big_Mod(base,power/2,mod))%mod;
  24.  
  25.         return (a*a)%mod;
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.     int i,a,base,power,mod,test;
  32.  
  33.     while(scanf("%d",&test)&&test)
  34.     {
  35.         for(i=1; i<=test; i++)
  36.         {
  37.             scanf("%d%d%d",&base,&power,&mod);
  38.  
  39.             a = Big_Mod(base,power,mod);
  40.  
  41.             printf("%d\n",a);
  42.         }
  43.     }
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment