The exponential loop section, because a ^x = a ^ (x% m + phi (m)) (mod m) is only established when X >= Phi (m), it should be noted to determine
By:gavin http://www.cnblogs.com/IMGavin///Index Cyclic section recursive processing #include<cstdio> #include <iostream> #include < cstdlib> #include <cstring> #include <string> #include <algorithm> #include <map> #include <queue> #include <vector> #include <cmath> #include <utility>using namespace Std;typedef long Long Ll;const int N = 10008, INF = 0x3f3f3f3f; LL A[n], mod, n;int phi[n];void allphi (int N) {memset (phi,0,sizeof (PHI)); Phi[1]=1; for (int i=2;i<=n;i++) {if (!phi[i]) {//Then I is a prime number phi[i]=i; for (int j=i;j<=n;j+=i) {if (!phi[j]) {phi[j]=j; } phi[j]=phi[j]/i* (i-1); }}}}ll Powmod (ll a,ll b,ll MoD) {ll ret=1;while (b) {if (b&1) {ret = ret * a% mod;} A = A * a% mod;b>>=1;} return ret;} BOOL Check (ll A, ll N, ll m) {if (a = = 1) {return false; } LL ans = 1; for (int i = 0; i < n; i++) {ans *=A if (ans >= m) {return true; }} return false;} ll Dfs (ll D, ll M, bool &sym) {if (d = = N) {if (A[d] >= m) {sym = 1; }else{sym = 0; } return a[d]% m; } bool Flag; LL p = DFS (d + 1, phi[m], flag); if (flag) {p + = phi[m]; } sym = Check (A[d], p, m); Return Powmod (A[d], p, m);} int main () {Allphi (N-2); int t = 0; while (CIN >> MoD) {cin >> n; for (int i = 1; I <= n; i++) {cin >> a[i]; } bool Flag; printf ("Case #%d:%lld\n", ++t, DFS (1, MOD, flag)% mod); } return 0;}
UVA 10692 Huge Mods (exponential cycle section)