CodeForces 907F Power Tower(扩展欧拉定理)
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.
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 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.
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
1
1
2
4
256
3
27
597484987
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(扩展欧拉定理)的更多相关文章
- [CodeForces - 906D] Power Tower——扩展欧拉定理
题意 给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$ 分析 由扩 ...
- 【CodeForces】906 D. Power Tower 扩展欧拉定理
[题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [ ...
- [Codeforces]906D Power Tower
虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,m,表示数 ...
- CodeForces - 906D Power Tower(欧拉降幂定理)
Power Tower CodeForces - 906D 题目大意:有N个数字,然后给你q个区间,要你求每一个区间中所有的数字从左到右依次垒起来的次方的幂对m取模之后的数字是多少. 用到一个新知识, ...
- Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)
题目链接 Power Tower 题意 给定一个序列,每次给定$l, r$ 求$w_{l}^{w_{l+1}^{w_{l+2}^{...^{w_{r}}}}}$ 对m取模的值 根据这个公式 每次 ...
- 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个询问,每个询问给出一个 ...
- 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 ...
- CF906D Power Tower
扩展欧拉定理 CF906D Power Tower 洛谷交的第二个黑题 题意 给出一个序列\(w-1,w_2,\cdots,w_n\),以及\(q\)个询问 每个询问给出\(l,r\),求: \[w_ ...
- [luogu4139]上帝与集合的正确用法【欧拉定理+扩展欧拉定理】
题目大意 让你求\(2^{2^{2^{\cdots}}}(mod)P\)的值. 前置知识 知识1:无限次幂怎么解决 让我们先来看一道全国数学竞赛的一道水题: 让你求解:\(x^{x^{x^{\cdot ...
随机推荐
- 【汇总】C#数据类型及转换
13.C# 16进制与字符串.字节数组之间的转换 https://www.cnblogs.com/seventeen/archive/2009/10/29/1591936.html C# 对象.文件与 ...
- SVN版本控制系统最佳实践
第1章SVN介绍及应用场景 1.1什么是SVN(Subversion) Svn(subversion)是近年来崛起非常优秀的版本管理工具,与CVS管理工具一样,SVN是一个跨平台的开源的版本控制系统. ...
- Spring高级话题
Spring Aware 在实际项目中,你不可避免的要用到spring容器本身的功能资源,这时你的bean要意识到spring容器的存在,才能调用spring提供的资源.spring aware本来就 ...
- git命令用法
git svn 说明 git pull svn update git add 要提交的文件名 svn add git rm svn rm,del git commit -m '备注一下提 ...
- 用jQuery获取table中行id和td值
<%@ page language="java" pageEncoding="UTF-8"%> <% String path = reques ...
- python 中类的初始化过程
首先元类中的__new__被调用 所有使用该元类的类都会调用一次,不管其有没有初始化,所以元类__new__的作用是修改/验证类的定义 返回的是一个元类的实例,即一个类的定义 元类的__init__由 ...
- 字符图元 & 显示列表
[字符图元] 1.typeface(字样),即设计风格,如Courier等. 2.font(字体),如10磅Courier斜体. 3.monspace即为等宽字体,proportional为非等宽字体 ...
- 3D数学基础 KeyNote 1
[计算几何复习要点] 1.向量加法的几何含意: a+b的释意为:a的尾连上b的头,新建一条从a的尾指向b的头的向量. 2.向量减法的几何含意: a-b的释意为:尾部相连,新建一个从b的头指向a的头的向 ...
- Spark之 RDD转换成DataFrame的Scala实现
依赖 <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2. ...
- java tomcat报错: Starting Tomcat v7.0 Server at localhost' has encountered a problem问题
运行web项目的时候出现下面错误: 出现这个问题的原因是 这个tomcat在其他项目中正在运行 关掉即可.