Power Tower(广义欧拉降幂)
作者:互联网
题意:https://codeforc.es/contest/906/problem/D
计算区间的:
ai ^ ai+1 ^ ai+2.......ar 。
思路:
广义欧拉降幂:
注意是自下而上递归使用欧拉降幂,比如求:a^b^c == a^(b^c%phi(mod)+?) == a^(b^(c%phi(phi(mod))+?+?)
而不是:a^b^c == a^b^(c%phi(mod)+?) == a^(b^(c%phi(mod)+?)%phi(mod)+?) 这样本身就是不对的,次方不是这么算的。
注意:因为判断要不要+phi(mod),所有快速幂里面就要开始搞搞,自己标个flag,或者直接重定义Mod == return x>=m?x%m+m:x;
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<int, vector<int>, greater<int> > 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 int a=b;a<=c;++a) 24 #define fr(a,b,c) for(register int 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(int &a,int &b); 32 double fabss(double a); 33 int maxx(int a,int b); 34 int minn(int a,int b); 35 int Del_bit_1(int n); 36 int lowbit(int n); 37 int abss(int a); 38 //const long long INF=(1LL<<60); 39 const double E=2.718281828; 40 const double PI=acos(-1.0); 41 const int inf=(1<<30); 42 const double ESP=1e-9; 43 //const int mod=(int)1e9+7; 44 const int N=(int)1e6+10; 45 46 ll a[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(a*a>=mod)fl=1; 84 a=a*a%mod; 85 } 86 return ans+(fl?mod:0); 87 } 88 ll solve(int l,int r,ll mod)//返回l~r计算结果; 听说phi(phi(mod))~==~mod/2所有最多log次; 89 { 90 if(l==r||mod==1)return Mod(a[l],mod);//任何数%1都是0,不用再算了; 91 return qpow(a[l],solve(l+1,r,phi(mod)),mod);//假设我已经知道了l+1~r的结果:递归下去; 92 } 93 94 int main() 95 { 96 int n; 97 ll p; 98 sc("%d%lld",&n,&p); 99 fo(i,1,n)sc("%lld",&a[i]); 100 int ask;sc("%d",&ask); 101 while(ask--) 102 { 103 int l,r; 104 sc("%d%d",&l,&r); 105 pr("%lld\n",solve(l,r,p)%p); 106 } 107 return 0; 108 } 109 110 /**************************************************************************************/ 111 112 int maxx(int a,int b) 113 { 114 return a>b?a:b; 115 } 116 117 void swapp(int &a,int &b) 118 { 119 a^=b^=a^=b; 120 } 121 122 int lowbit(int n) 123 { 124 return n&(-n); 125 } 126 127 int Del_bit_1(int n) 128 { 129 return n&(n-1); 130 } 131 132 int abss(int 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 int minn(int a,int b) 143 { 144 return a<b?a:b; 145 }
标签:phi,return,Power,降幂,int,long,Tower,include,mod 来源: https://www.cnblogs.com/--HPY-7m/p/11444923.html