Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p.

Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will be

After tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.

Input

First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109).

Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have.

Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you.

kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).

Output

Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.

Example
Input
6 1000000000
1 2 2 3 3 3
8
1 1
1 6
2 2
2 3
2 4
4 4
4 5
4 6
Output
1
1
2
4
256
3
27
597484987
Note

327 = 7625597484987

题意:给出一个数字序列和一个固定的模数mod,给出q个询问,每次询问f(l,r)

f(l,r) =a[l]^(a[l+1]^(a[l+2]^(a[l+3]^(...^a[r])))%mod (^是幂次的意思)

题解:扩展欧拉定理告诉我们

然后我们尝试展开a^b^c

再往下也是一样的,我们可以先预处理出phi[p],phi[phi[p]]……

大概要处理几层呢?logn层,为什么呢?

假设phi[now]=1了

那么之上不管多少层

x=1,2,3,4,5……

这些数模一都是一

所以就成了欧拉函数的衰变速度(我瞎糊的名词,意思是经过几次phi,p会变成1)

这个复杂度是logn的,我们可以对这进行一发dfs,加上快速幂的logn复杂度,总复杂度是loglogn的,值得一提的是,快速幂中也要改成扩展欧拉定理的形式,否则小心炸掉~

顺便可以研究一下这道题是怎么被博主伪装成线段树的

U23882 天真♂哲学家♂树(Naive Philosopher Tree)

代码如下:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int a[],phi[],n,m,mod; int get(int x)
{
int ans=x;
for(int i=;i*i<=x;i++)
{
if(x%i==)
{
ans=ans/i*(i-);
while(x%i==)
{
x/=i;
}
}
}
if(x!=)
{
ans=ans/x*(x-);
}
return ans;
} int gg(long long x,int p)
{
return x>=p?x%p+p:x;
} int kasumi(int a,int b,int p)
{
int ans=;
while(b)
{
if(b&)
{
ans=gg(1ll*ans*a,p);
}
a=gg(1ll*a*a,p);
b>>=;
}
return ans;
} int dfs(int l,int r,int i)
{
if(l==r||phi[i]==)
{
return gg(a[l],phi[i]);
}
return kasumi(a[l],dfs(l+,r,i+),phi[i]);
} int main()
{
scanf("%d%d",&n,&mod);
phi[]=mod;
for(int i=;i<=;i++)
{
phi[i]=get(phi[i-]);
}
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dfs(l,r,)%mod);
}
return ;
}

CodeForces 907F Power Tower(扩展欧拉定理)的更多相关文章

  1. [CodeForces - 906D] Power Tower——扩展欧拉定理

    题意 给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$ 分析 由扩 ...

  2. 【CodeForces】906 D. Power Tower 扩展欧拉定理

    [题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [ ...

  3. [Codeforces]906D Power Tower

    虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,m,表示数 ...

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

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

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

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

  6. 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个询问,每个询问给出一个 ...

  7. 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 ...

  8. CF906D Power Tower

    扩展欧拉定理 CF906D Power Tower 洛谷交的第二个黑题 题意 给出一个序列\(w-1,w_2,\cdots,w_n\),以及\(q\)个询问 每个询问给出\(l,r\),求: \[w_ ...

  9. [luogu4139]上帝与集合的正确用法【欧拉定理+扩展欧拉定理】

    题目大意 让你求\(2^{2^{2^{\cdots}}}(mod)P\)的值. 前置知识 知识1:无限次幂怎么解决 让我们先来看一道全国数学竞赛的一道水题: 让你求解:\(x^{x^{x^{\cdot ...

随机推荐

  1. 11.solr学习速成之MoreLikeThis

    Solr相似匹配    在网页搜索或电商产品搜索结果页面,很多时候会看到一个相似文档.相似产品或找相似的链接.Solr 使用 MoreLikeThisComponent(MLT)和 MoreLikeT ...

  2. 关于springboot中文件上传,properties配置

    spring.http.multipart.enabled=true #默认支持文件上传. spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘. ...

  3. java 蓝桥杯算法提高 _2最大最小公倍数

    解题思路: 1. n是奇数,那就最大的三个数相乘2. n是偶数,得分两种情况了, ①如果n不是3的倍数,那就s=n*(n-1)*(n-3)---n与n-2同为偶数,故排除一个n-2: ②n是3的倍数, ...

  4. Kibana(elasticsearch操作工具)的安装

    在安装完es集群的基础上 1.创建文件夹并赋权 # 使用root进行操作 mkdir -p /export/data/kibana mkdir -p /export/logs/kibana # 赋权给 ...

  5. Java网络编程小结 URLConnection协议处理器

    URL和URLConnection类 网络中的URL(Uniform Resource Locator)是统一资源定位符的简称.它表示Internet上某一资源的地址.通过URL我们可以访问Inter ...

  6. strncmp memcmp区别

    内部实现:前者逐每个字符进行比较,并判当前字符是否为0: 后者逐内存块进行比较. 效率:后者自然要优,不论从内部实现上,还是系统优化上. 场景:后者无法替代前者.在项目中遇到一种情况,两个字符串比较, ...

  7. O(n)求中位数和第k大数

    解题关键:模板与思路.面试题 #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  8. iOS端一次视频全屏需求的实现(转)

    对于一个带有视频播放功能的app产品来说,视频全屏是一个基本且重要的需求.虽然这个需求看起来很简单,但是在实现上,我们前后迭代了三套技术方案.这篇文章将介绍这三种实现方案中的利弊和坑点,以及实现过程中 ...

  9. cs4.1 编译与安装

    cs4.1编译报 https://issues.apache.org/jira/browse/CLOUDSTACK-2913 cs4.1安装报

  10. linux之shell编程初步

    #################适用于CentOS6################## #!/bin/bash ########################################## ...