Topic Link: Click to open the link
Test instructions: Calculates a^b%c but where B is large and may reach 10^1000000, so there is a power-down formula a^b%c= a^ (B%phi (c) +phi (c))%c (B>=phi (c))
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 10100#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 1000000007#define pp pair<int,int> #define ull unsigned long longusing nam Espace std;int A,c;char B[1000010];ll Phi (ll N) {ll m= (ll) sqrt (n+0.5), ans=n;for (ll i=2;i<=m;i++) {if (n%i==0) {Ans=ans /i* (i-1); while (n%i==0) n/=i;}} if (n>1) ans=ans/n* (n-1); return ans;} ll Pow_mod (ll a,ll n,ll p) {if (n==0) return 1;ll ans=pow_mod (a,n/2,p); Ans=ans*ans%p;if (n&1) Ans=ans*a%p;return ans;} void Solve () {int Len=strlen (b); ll Tem=phi (c), Sb;if (len<=10) {sscanf (b, "%i64d", &SB); if (Sb>=tem) printf ("%I 64d\n ", Pow_mod (A,sb%tem+tem,c)); elseprintf ("%i64d\n ", Pow_mod (A,sb,c)); return;} ll ans=0;for (int i=0;i<len;i++) ans= (ans*10+ (b[i]-' 0 '))%tem;printf ("%i64d\n", Pow_mod (A,ans+tem,c));} int main () {while (~scanf ("%i64d%s%i64d", &a,b,&c)) solve (); return 0;}
Fzu 1759-super a^b MoD C (Fast power + large integer modulo + Euler function)