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. System.Security.Cryptography.CryptographicException: 系统找不到指定的文件

    默认为false 改为true

  2. python collections module's defaultdict

    Collections is a high-performance container datatypes. defaultdict objects class collections.default ...

  3. Python极其简单的分布式异步作业管理系统RQ入门

    Python极其简单的分布式异步作业管理系统RQ入门 原创 2017-08-19 lixing 生信人 Python极其简单的分布式异步作业管理系统RQ入门 1. 什么是Job? Job直译过来就是工 ...

  4. django之设置缓存

    缓存 一句话总结:缓存可以对view.模板.数据进行缓存可以设置缓存在不同的地方(本地内存.redis.系统文档)可以为服务器节省性能.减少用户等待时间. 对于中等流量的网站来说,尽可能地减少开销是必 ...

  5. Python 小结

    1. Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占位语句. 2.删除一个list里面的重复元素 方法一:是利用map的fromkeys来自动过滤重复 ...

  6. 用C#操作IO端口1-用并口控制发光二极管

    什么是端口? 端口包含了一系列信号线, 通过这个端口CPU可以同其他外部设备交换数据, 比如我们经常见到的Modem,打印机等. 通常情况下, 打开的信号是”1”, 关闭的信号是”0”. 并口在同一时 ...

  7. leetcode189

    public class Solution { public void reverse(int[] nums, int start, int end) { while (start < end) ...

  8. From Ruby array to JS array in Rails- 'quote'?

    From Ruby array to JS array in Rails- 'quote'? <%= raw @location_list.as_json %>

  9. Tornado 多进程 & 异步

    另外一篇:http://www.cnblogs.com/xiaoshi657/p/6945208.html 基本版: #coding=utf-8 import tornado.web import t ...

  10. Unity脚本开发基础 C#

    1. MonoBehaviour 类 常用事件响应函数: 2. 访问游戏对象 (1) 通过名称来查找 (2) 通过标签来查找 上述函数比较费时,应避免在 Update 函数调用. 3. 访问组件 对于 ...