Super a^b mod C fast Power + Euler function power down

Source: Internet
Author: User

uper a^b MoD CTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatus

Description

Given A,b,c, you should quickly calculate the result of A^b mod C. (1<=a,c<=1000000000,1<=b<=10^1000000).

Input

There is multiply testcases. Each testcase, there are one line contains three integers A, B and C, separated by A single space.

Output

For each testcase, output an integer, denotes the result of A^b mod C.

Sample Input

3 2 42) 10 1000

Sample Output

124 first to power down, from the power-down formula: (However, do not know how the formula came ...) Calculation of power after power-down
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 #definell __int647 #defineN 10001008 using namespacestd;9 CharB[n];Ten ll P[n]; One ll A, C; All Quick (ll A, ll b) {//Fast Power -ll k =1; -      while(b) { the         if(b%2==1){ -K = k*A; -K%=C; -         } +A = a*a%C; -b/=2; +     } A     returnK; at } - voidPriem () { -Memset (P,0,sizeof(P)); - ll I, J; -p[1] =1; -      for(i=2; I&LT;=SQRT (N); i++){ in          for(j=2; j<=n/i; J + +) -P[I*J] =1; to     } + } -ll Ola (ll N) {//Euler functions the ll I, J, R, AA; *R =N; $AA =N;Panax Notoginseng      for(i=2; I&LT;=SQRT (n); i++){ -         if(!P[i]) { the             if(aa%i==0){ +R = r/i* (i-1); A                  while(aa%i==0) theAA/=i; +             } -         } $     } $     if(aa>1) -R = r/aa* (aa-1); -     returnR; the } - intMain () {Wuyi ll D, I, J; the Priem (); -      while(~SCANF ("%i64d%s%i64d",&a,b,&c)) { Wull L =strlen (b); -ll b=0; Aboutll OC =Ola (c); $       //cout<< "OC =" <<oc<<endl; -          for(i=0; i<l; i++){ -B = b*Ten+b[i]-'0'; -             if(b>oc) A                  Break; +         } the         //cout<<i<<endl; -         if(i==l) $D =Quick (A, b); the         Else{ theb=0; the              for(i=0; i<l; i++) {//Power down theB = (b*Ten+b[i]-'0')%OC; -             } inD = Quick (a,b+oc); the         } the       //printf ("b=%i64d\n", B); Aboutprintf"%i64d\n", d); the     } the     return 0; the}

Super a^b mod C fast Power + Euler function power down

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.