Title Description:
Post an official tip:
The topic idea, uses the exponential power-down formula to do, notice opens long long.
Code Reference @w4k3up @ Xuhang
thanks to @qq_36704031, the previous code does have a problem, thank the brother for pointing out my mistake.
AC Code: (Corrected)
/* Urozero Autumn Training Camp Day 1:ncpc-2016, Friday, November, Problem E. exponial September 3, 2017 14:21:27 201 7 October 5 13:18 Correct the power-down formula for AC index */#include <iostream> #include <map> #include <set> #include <string>
; #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue
> #include <vector> using namespace std;
typedef long Long LL;
ll N,m,ans;
ll Euler (ll N) {//return Euler (n) ll Res=n,a=n; for (ll i=2;i*i<=a;i++) {if (a%i==0) {res=res/i* (i-1);//Division first to prevent overflow of intermediate data while (a%i==0
) A/=i;
}} if (a>1) res=res/a* (A-1);
return res;
} ll Fast_mod (ll x,ll n,ll Max) {ll res=1;
while (n>0) {if (n & 1) res= (res*x)%max;
x= (x*x)%max;
n >>= 1;
} return res;
} ll func (ll n,ll m) {if (m==1) return 0;
if (n==1) return 1;
else if (n==2) return 2%m; else IF (n==3) return 9%m;
else if (n==4) return Fast_mod (4,9,M);
/* if (n<=4) {ll ans=1;
for (int i=1;i<=n;i++) {ans=fast_mod (i,ans,m);
} return ans;
} */else{ll Phi=euler (m);
ll Z=func (N-1,phi);
Ans=fast_mod (N,PHI+Z,M);
} return ans;
} void Solve () {scanf ("%lld%lld", &n,&m);
printf ("%lld\n", Func (n,m));
} int main () {solve ();
return 0;
}