题意: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

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
//const int mod=(int)1e9+7;
const int N=(int)1e6+; ll a[N];
map<ll,ll>mp;
long long phi(long long n)//a^(b mod phi(c)+phi(c)) mod c
{
if(mp.count(n))return mp[n];//记忆化;
long long i,rea=n,temp=n;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
rea=rea-rea/i;
while(n%i==)
n/=i;
}
}
if(n>)
rea=rea-rea/n;
mp[temp]=rea;
return rea;
}
ll Mod(ll x, ll m)
{
return x>=m?x%m+m:x;
}
long long qpow(long long a,long long b,long long mod)
{
long long ans,fl=;
// ll ta=a,tb=b,tta=a;
ans=;
while(b!=)
{
if(b&)
{
if(ans*a>=mod)fl=;
ans=ans*a%mod;
}
b/=;
if(!b)break;
if(a*a>=mod)fl=;
a=a*a%mod;
}
return ans+(fl?mod:);
}
ll solve(int l,int r,ll mod)//返回l~r计算结果; 听说phi(phi(mod))~==~mod/2所有最多log次;
{
if(l==r||mod==)return Mod(a[l],mod);//任何数%1都是0,不用再算了;
return qpow(a[l],solve(l+,r,phi(mod)),mod);//假设我已经知道了l+1~r的结果:递归下去;
} int main()
{
int n;
ll p;
sc("%d%lld",&n,&p);
fo(i,,n)sc("%lld",&a[i]);
int ask;sc("%d",&ask);
while(ask--)
{
int l,r;
sc("%d%d",&l,&r);
pr("%lld\n",solve(l,r,p)%p);
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

Power Tower(广义欧拉降幂)的更多相关文章

  1. CodeForces - 906D Power Tower(欧拉降幂定理)

    Power Tower CodeForces - 906D 题目大意:有N个数字,然后给你q个区间,要你求每一个区间中所有的数字从左到右依次垒起来的次方的幂对m取模之后的数字是多少. 用到一个新知识, ...

  2. ACM-数论-广义欧拉降幂

    https://www.cnblogs.com/31415926535x/p/11447033.html 曾今一时的懒,造就今日的泪 记得半年前去武大参加的省赛,当时的A题就是一个广义欧拉降幂的板子题 ...

  3. 广义欧拉降幂(欧拉定理)——bzoj3884,fzu1759

    广义欧拉降幂对于狭义欧拉降幂任然适用 https://blog.csdn.net/qq_37632935/article/details/81264965?tdsourcetag=s_pctim_ai ...

  4. Codeforces Round #454 D. Power Tower (广义欧拉降幂)

    D. Power Tower time limit per test 4.5 seconds memory limit per test 256 megabytes input standard in ...

  5. Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)

    题目链接  Power Tower 题意  给定一个序列,每次给定$l, r$ 求$w_{l}^{w_{l+1}^{w_{l+2}^{...^{w_{r}}}}}$  对m取模的值 根据这个公式 每次 ...

  6. The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  7. BZOJ 3884——欧拉降幂和广义欧拉降幂

    理论部分 欧拉定理:若 $a,n$ 为正整数,且 $a,n$ 互质,则 $a^{\varphi (n)} \equiv 1(mod \ n)$. 降幂公式: $$a^b=\begin{cases}a^ ...

  8. Codeforces Round #454 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)

    题目链接:http://codeforces.com/contest/906/problem/D 题目大意:给定n个整数w[1],w[2],……,w[n],和一个数m,然后有q个询问,每个询问给出一个 ...

  9. D - Power Tower欧拉降幂公式

    题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...

随机推荐

  1. 顺序表应用3:元素位置互换之移位算法(SDUT 3326)

    题解:用一个for,循环m次,每次都把最前面的放到最后面,就可以了. #include <stdio.h> #include <stdlib.h> #include <s ...

  2. Joda-DateTime Date 与 String 相互转换

    [参考文章]:Joda-Time 的 DateTimeFormat 问题 public class DateFormatUtils { /** HH 必须大写 */ public static fin ...

  3. (十七)C语言之变量

  4. 3 Java 冒泡排序法

    冒泡排序( Bubble Sort)是一种简单的排序算法.它重复访问要数列, 一次比较两个元素,如果他们的顺序错误就把交换过来.访问数列工作是 一次比较两个元素,如果他们的顺序错误就把交换过来.访问数 ...

  5. IDEA怎么开启终端Terminal

    方法一:在IDEA中点击view→tool window→Terminal即可开启 方法二:按住ALT+F12(如果是笔记本按不出来的话再加个Fn键)

  6. ANTLR4将JSON翻译成XML

    实现功能:构建一个JSON到XML的翻译器. antlr4文件: grammar JSON; json : object | array ; object : '{' pair (',' pair)* ...

  7. Mac os文件名大小写不敏感

    Mac os文件名大小写不敏感,但是linux是大小写敏感的. 让我们代入一个场景, 创建一个新文件,你习惯以小写字母开头,接着在其他module中import,看起来一切都正常,emmm,确实没有任 ...

  8. html5-meta标签和搜索引擎

    emta标签的组成: meta标签分两大部分:HTTP-EQUIV  和  NAME  变量. HTTP-EQUIV:HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助 ...

  9. 反射 go

    reflect.Valueof 到底是个什么? 反射值对象(reflect.Value)提供一系列方法进行零值和空判定,如下表所示. 反射值对象的零值和有效性判断方法 方 法 说 明 IsNil() ...

  10. Windows10 修改键位映射

    https://blog.csdn.net/lhdalhd1996/article/details/90741092 1.为什么要修改键位我的笔记本键盘上下键是这样的: 很想捶死产品经理,你是从来不用 ...