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. 巧用setTimeout函数设置程序后门

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. HTTP接口开发专题二(发送http请求的接口工具类)

    import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; imp ...

  3. ArcGIS 二次开发增加工具栏按钮时,给按钮图标设置失败

    在类的构造函数中通过下面的代码获得的bitmap为null: string bitmapResourceName = GetType().Name + ".bmp"; base.m ...

  4. 聊聊flutter的UI布局

    UI布局多半是套路,熟悉套路的规则. Flutter的UI布局也有一套规则 center center可以让任何元素在屏幕中居中,既是水平居中又是垂直居中,如果想让元素从上而下排列要怎么办呢?那就得使 ...

  5. 数据库连接池--druid

    数据库连接池常用的有:dbcp,c3p0,druid 代码仓库(https://github.com/) package com.huawei.test; import java.sql.Connec ...

  6. 在Yii Framework中利用PHPMailer发送邮件(2011-06-02 14:06:23)

    转载▼ 标签: it 分类: 技术共享 官方扩展链接:http://www.yiiframework.com/extension/mailer/这个扩展配置十分方便,如果有问题的话,可以打开Debug ...

  7. easyui容易被忽略掉的部分

    官方文档有这么一段话: Each component of easyui has properties, methods and events. Users can extend them easil ...

  8. 获取崩溃时的调用栈和生成dump文件,然后自动重启

    首先要说明的是:  linux 下 比较方便可以得到 崩溃时的调用栈,win下 比较难办   1. linux 获取调用栈 代码奉上: #include <execinfo.h> //在头 ...

  9. Mask_RCNN训练自己的模型(练习)

  10. Opencv 直方图比较

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...