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个询问,每个询问给出一个l,r,求w[l]^w[l+1]^w[l+2]……w[r] %m ,即a[l]到a[r]的幂次方
解题思路:利用欧拉降幂公式

第一个要求a和p互质,第2个和第3个为广义欧拉降幂,不要求a和p互质,用在这题刚好。
因为有两种情况,所以我们需要自定义一下降幂取模公式。
我们对整个区间进行递归处理,每一个数的指数是它后一个数到右端点的幂。
递归终止条件为到右端点或者p的欧拉函数值为1,再求欧拉函数值的时候我们需要进行记忆化,否则会超时
代码:
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define ll long long
#define MOD(a,b) a>=b?a%b+b:a
#define N 100005
map<ll,ll> mp;
int n,l,r,q;
ll mod,w[N];
ll qpow(ll a,ll b,ll p){
ll res=;
while(b){
if(b&) res=MOD(res*a,p); //为保证指数结果正确,应该用自定义取模
b>>=;
a=MOD(a*a,p);
}
return res;
}
ll phi(ll x){
if(mp[x]) return mp[x];
ll tmp=x,res=x;
for(int i=;i*i<=x;i++){
if(x%i==){
res=res*(i-)/i;
while(x%i==) x/=i;
}
}
if(x>) res=res*(x-)/x;
return mp[tmp]=res;
}
ll solve(int l,int r,ll m){
if(l==r||m==) return MOD(w[l],m);
else return qpow(w[l],solve(l+,r,phi(m)),m);
}
int main() {
scanf("%d%I64d",&n,&mod);
for(int i=;i<=n;i++) scanf("%I64d",&w[i]);
scanf("%d",&q);
while(q--){
scanf("%d%d",&l,&r);
printf("%I64d\n",solve(l,r,mod)%mod);
}
return ;
}
bzoj 3884 上帝与集合的正确用法
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3884
题目大意:和上题很像,只不过所有数都是2,且次方是无穷的了,给定一个正整数p,求2^(2^(2^(2^(2^...)))) mod p的值
解题思路:方法几乎是一样的,因为每次递归幂的模数就会变成原来的欧拉函数值,所以最多经过log(p),模数就会变成1,然后后面结果都一样的了,没必要递归下去,直接结束就好了。
代码:
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define ll long long
#define MOD(a,b) a>=b?a%b+b:a
#define N 100005
map<ll,ll> mp;
int n,l,r,q;
ll mod;
ll qpow(ll a,ll b,ll p){
ll res=;
while(b){
if(b&) res=MOD(res*a,p);
b>>=;
a=MOD(a*a,p);
}
return res;
}
ll phi(ll x){
if(mp[x]) return mp[x];
ll tmp=x,res=x;
for(int i=;i*i<=x;i++){
if(x%i==){
res=res*(i-)/i;
while(x%i==) x/=i;
}
}
if(x>) res=res*(x-)/x;
return mp[tmp]=res;
}
ll solve(ll m){
if(m==) return ;
else return qpow(,solve(phi(m)),m);
}
int main() {
int T;
scanf("%d",&T);
while(T--){
scanf("%lld",&mod);
printf("%lld\n",solve(mod)%mod);
}
return ;
}
Codeforces Round #454 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)的更多相关文章
- Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...
- Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路
D. Tanya and Password Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/508 ...
- Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛
D. Recover it! Authors guessed an array aa consisting of nn integers; each integer is not less than ...
- D - Power Tower欧拉降幂公式
题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...
- Codeforces Round #524 (Div. 2) codeforces 1080A~1080F
目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...
- Codeforces Round #454 Div. 1 [ 906A A. Shockers ] [ 906B B. Seating of Students ] [ 906C C. Party ]
PROBLEM A. Shockers 题 http://codeforces.com/contest/906/problem/A 906A 907C 解 水题,按照题意模拟一下就行了 如果是 ‘ ! ...
- Codeforces Round #454 Div. 1
B:考虑2*m怎么构造.因为要求相邻的数不能再相邻,容易想到黑白染色之类的东西,考虑染个色然后大概把黑点扔一边白点扔一边.显然m<=3时无解.对m>4,m为偶数时,如1 2 3 4 5 6 ...
- Codeforces Round #454 Div. 2 A B C (暂时)
A. Masha and bears 题意 人的体积为\(V\),车的大小为\(size\),人能钻进车的条件是\(V\leq size\),人对车满意的条件是\(2V\geq size\). 现知道 ...
- Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs
D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...
随机推荐
- postman与soapui操作
get和post请求的区别? get请求:直接在浏览器里直接调用就可以了,不用借助工具 向服务端获取数据的 数据是放在url里面 post请求:向服务端发送数据的 数据放在body里 ...
- php上传文件的原理
文件上传原理 将客户端的文件上传到服务器,再将服务器的临时文件上传到指定目录 客户端配置 提交表单 表单的发送方式为post 添加enctype="multipart/form-data&q ...
- Java WebService服务
其中cxf框架 http://cxf.apache.org/ Apache CXF™: An Open-Source Services Framework Overview Apache CXF™ i ...
- 笨办法学Python(learn python the hard way)--练习程序21-30
下面是练习21-30,基于python3 #ex21.py 1 def add(a, b): print("ADDING %d + %d" %(a, b)) return a+b ...
- Maven安装本地jar包至本地repository
1.安装jar包 Maven 安装 JAR 包的命令是: mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -Dartifa ...
- 阿里云数据库导出-本地mysql导入
因阿里云数据库标准访问模式不支持外网接入 因此导出一份到本地,注意选择编码为utf8 mysql 命令行用source导入有utf8编码的sql文件时导入的数据有乱码解决办法 set names ut ...
- LintCode之移动零
题目描述: 分析:由于要使非零元素保持原数组的顺序,我只能想出在找到一个0时,逐个移动数组元素使得后一个元素覆盖前一个元素,再将这个0移到后头去. 我的代码: public class Solutio ...
- HDU4089 Activation(概率DP+处理环迭代式子)
题意:有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有一下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后(概 ...
- 128、TensorFlow元数据MetaData
#tf.Session.run也接收一个可选的参数options #能够让你来配置训练时的参数 #run_metadata参数让你能够收集关于训练的元信息 #列如你可以使用这些可选项来追踪执行的信息 ...
- spring4.1.8扩展实战之八:Import注解
spring4.1.8扩展实战之八:Import注解 2018年09月10日 12:53:57 博陵精骑 阅读数:441更多 所属专栏: spring4源码分析与实战 版权声明:欢迎转载,请注明 ...