Power Tower
题目大意:给出一段长为 \(n\) 的序列 \(a_1,a_2,\cdots,a_n\)
,一个模数 \(m\) .每次询问给定 \(l,r\)
求 \(a_l^{{a_{l+1}^\cdots}^{a_r}} mod\) \(m\)
思路:不断欧拉降幂即可,\(\log m\)次就可以达到1,由于套了一个快速幂,复杂度 \(O(\log ^2 n)\)。注意取模时应满足广义欧拉定理 在这里卡了好久
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=50005;
int a[N],m,l,r,n,q;
unordered_map<int,int>mp;
int phi(int x)
{
int tmp=x;
if(mp.count(x))return mp[x];
int res=x;
for(int i=2;i*i<=x;++i)
{
if(x%i==0)
{
res=res-res/i;
while(x%i==0)x/=i;
}
}
if(x>1)res=res-res/x;
return mp[tmp]=res;
}
int mod(int x,int m)
{
return x>=m?x%m+m:x;
}
int qpow(int n,int k,int p)
{
int base=n,res=1;
while(k)
{
if(k&1)res=mod(res*base,p);
base=mod(base*base,p);
k>>=1;
}
return res;
}
int f(int l,int r,int m)
{
if(l==r||m==1)return mod(a[r],m);
return qpow(a[l],f(l+1,r,phi(m)),m);
}
signed main()
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;++i)scanf("%lld",&a[i]);
scanf("%lld",&q);
while(q--)
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",f(l,r,m)%m);
}
return 0;
}
Power Tower的更多相关文章
- 【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(欧拉降幂定理)
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 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_ ...
- CodeForces 907F Power Tower(扩展欧拉定理)
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is u ...
- [Codeforces]906D Power Tower
虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,m,表示数 ...
- D - Power Tower欧拉降幂公式
题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...
- Codeforces 906 D. Power Tower
http://codeforces.com/contest/906/problem/D 欧拉降幂 #include<cstdio> #include<iostream> usi ...
- [CodeForces - 906D] Power Tower——扩展欧拉定理
题意 给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$ 分析 由扩 ...
随机推荐
- 指定GPU运行python程序
一.命令行运行python程序时 1.首先查看哪些GPU空闲,nvidia-smi显示当前GPU使用情况. nvidia-smi 2.然后指定空闲的GPU运行python程序. CUDA_VISIBL ...
- Memcache和Redis的详细理解与区别
1. Memcache Memcache是一个高性能,分布式内存对象缓存系统,通过在内存中缓存一个巨大的hash表,他能够存储包括图像,文件,索引,sql语句结果等数据,可以理解为它理解为一个为提升读 ...
- JavaWeb--概述
1.Java Web应用由一组Servlet.HTML页.类以及其他可以被绑定的资源构成.它可以在何种供应商提供的实现Servlet规范的Servlet容器中运行: 2.Java Web应用中包含如下 ...
- 6、mysql事务
1.mysql事务 —mysql中,事务其实是一个最小的不可分割的工作单元.事务能够保证一个业务的完整性,例如:银行存款: a - > -100 >update user set ...
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- 「AMPPZ2014」The Prices
传送门 Luogu团队题链接 解题思路 看到 \(m\) 这么小,马上想到状压 \(\text{DP}\). 设 \(dp[i][j]\) 表示在前 \(i\) 家商店中已买商品的状态为 \(j\) ...
- Python—网络通信编程之udp通信编程
服务端代码 import socket # 1.创建实例,即数据报套接字 server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 2.绑 ...
- python3列表操作
1.Python列表脚本操作符 2.Python列表截取 切片的公式:[start : end : step] 1)切片的取值: list1 = [1, 4, 9, 16, 25] print(lis ...
- 使用Vue 和 内网穿透:返回 invalid host header
原因:新版的webpack-dev-server出于安全考虑,默认检查hostname,如果它不是配置内的,将会中断访问. -------------------------------------- ...
- obtainFreshBeanFactory方法源码跟踪
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...