B.super_log(The Preliminary Contest for ICPC Asia Nanjing 2019)
同:https://www.cnblogs.com/--HPY-7m/p/11444923.html
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 #include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<ll, vector<ll>, greater<ll> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 #define fo(a,b,c) for(register ll a=b;a<=c;++a) 24 #define fr(a,b,c) for(register ll a=b;a>=c;--a) 25 #define mem(a,b) memset(a,b,sizeof(a)) 26 #define pr printf 27 #define sc scanf 28 #define ls rt<<1 29 #define rs rt<<1|1 30 typedef long long ll; 31 void swapp(ll &a,ll &b); 32 double fabss(double a); 33 ll maxx(ll a,ll b); 34 ll minn(ll a,ll b); 35 ll Del_bit_1(ll n); 36 ll lowbit(ll n); 37 ll abss(ll a); 38 //const long long INF=(1LL<<60); 39 const double E=2.718281828; 40 const double PI=acos(-1.0); 41 const ll inf=(1<<30); 42 const double ESP=1e-9; 43 //const ll mod=(ll)1e9+7; 44 const ll N=(ll)1e6+10; 45 46 ll n__; 47 map<ll,ll>mp; 48 long long phi(long long n)//a^(b mod phi(c)+phi(c)) mod c 49 { 50 if(mp.count(n))return mp[n];//记忆化; 51 long long i,rea=n,temp=n; 52 for(i=2;i*i<=n;i++) 53 { 54 if(n%i==0) 55 { 56 rea=rea-rea/i; 57 while(n%i==0) 58 n/=i; 59 } 60 } 61 if(n>1) 62 rea=rea-rea/n; 63 mp[temp]=rea; 64 return rea; 65 } 66 ll Mod(ll x, ll m) 67 { 68 return x>=m?x%m+m:x; 69 } 70 long long qpow(long long a,long long b,long long mod) 71 { 72 long long ans,fl=0; 73 // ll ta=a,tb=b,tta=a; 74 ans=1; 75 while(b!=0) 76 { 77 if(b&1) 78 { 79 if(ans*a>=mod)fl=1; 80 ans=ans*a%mod; 81 } 82 b/=2; 83 if(!b)break; 84 if(a*a>=mod)fl=1; 85 a=a*a%mod; 86 } 87 return ans+(fl?mod:0); 88 } 89 ll solve(ll l,ll r,ll mod)//返回l~r计算结果; 听说phi(phi(mod))~==~mod/2所有最多log次; 90 { 91 if(l==r||mod==1)return Mod(n__,mod);//任何数%1都是0,不用再算了; 92 return qpow(n__,solve(l+1,r,phi(mod)),mod);//假设我已经知道了l+1~r的结果:递归下去; 93 } 94 95 int main() 96 { 97 ll T; 98 sc("%lld",&T); 99 while(T--) 100 { 101 ll cnt; 102 ll p; 103 sc("%lld%lld%lld",&n__,&cnt,&p); 104 if(cnt==0) printf("%lld\n",(ll)(1%p)); 105 else pr("%lld\n",solve(1,cnt,p)%p); 106 } 107 return 0; 108 } 109 110 /**************************************************************************************/ 111 112 ll maxx(ll a,ll b) 113 { 114 return a>b?a:b; 115 } 116 117 void swapp(ll &a,ll &b) 118 { 119 a^=b^=a^=b; 120 } 121 122 ll lowbit(ll n) 123 { 124 return n&(-n); 125 } 126 127 ll Del_bit_1(ll n) 128 { 129 return n&(n-1); 130 } 131 132 ll abss(ll a) 133 { 134 return a>0?a:-a; 135 } 136 137 double fabss(double a) 138 { 139 return a>0?a:-a; 140 } 141 142 ll minn(ll a,ll b) 143 { 144 return a<b?a:b; 145 }