• 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;

    注意:快速幂里的break

      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(!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(int l,int r,ll mod)//返回l~r计算结果;    听说phi(phi(mod))~==~mod/2所有最多log次;
     90 {
     91     if(l==r||mod==1)return Mod(a[l],mod);//任何数%1都是0,不用再算了;
     92     return qpow(a[l],solve(l+1,r,phi(mod)),mod);//假设我已经知道了l+1~r的结果:递归下去;
     93 }
     94 
     95 int main()
     96 {
     97     int n;
     98     ll p;
     99     sc("%d%lld",&n,&p);
    100     fo(i,1,n)sc("%lld",&a[i]);
    101     int ask;sc("%d",&ask);
    102     while(ask--)
    103     {
    104         int l,r;
    105         sc("%d%d",&l,&r);
    106         pr("%lld
    ",solve(l,r,p)%p);
    107     }
    108     return 0;
    109 }
    110 
    111 /**************************************************************************************/
    112 
    113 int maxx(int a,int b)
    114 {
    115     return a>b?a:b;
    116 }
    117 
    118 void swapp(int &a,int &b)
    119 {
    120     a^=b^=a^=b;
    121 }
    122 
    123 int lowbit(int n)
    124 {
    125     return n&(-n);
    126 }
    127 
    128 int Del_bit_1(int n)
    129 {
    130     return n&(n-1);
    131 }
    132 
    133 int abss(int a)
    134 {
    135     return a>0?a:-a;
    136 }
    137 
    138 double fabss(double a)
    139 {
    140     return a>0?a:-a;
    141 }
    142 
    143 int minn(int a,int b)
    144 {
    145     return a<b?a:b;
    146 }
  • 相关阅读:
    index()方法
    extend()方法
    count()方法
    copy()方法
    clear()方法
    append()方法
    IE botton 点击文字下沉
    IE滚动条
    关闭windows10自动更新
    vue文件名规范
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11444923.html
Copyright © 2020-2023  润新知