Codeforces 906 D. Power Tower
http://codeforces.com/contest/906/problem/D
欧拉降幂
#include<cstdio>
#include<iostream> using namespace std; int pi[],phi[],lim; int a[]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} int get_phi(int n)
{
int ph=n;
for(int i=;i*i<=n;++i)
if(!(n%i))
{
ph=ph/i*(i-);
while(!(n%i)) n/=i;
}
if(n>) ph=ph/n*(n-);
return ph;
} int Pow(int a,int b,int mod,bool &flag)
{
int res=;
while(b)
{
if(b&)
{
flag|=(1LL*res*a>=mod);
res=1LL*res*a%mod;
}
flag|=(1LL*a*a>=mod);
a=1LL*a*a%mod;
b>>=;
}
return res;
} int f(int x,int dep)
{
if(dep>lim)
{
x-=dep-lim;
dep=lim;
}
if(dep==) return a[x]%pi[];
int tmp=a[x];
if(tmp>=phi[dep-]) tmp=tmp%phi[dep-]+phi[dep-];
x--;
bool flag;
for(int i=dep-;i;--i)
{
flag=false;
tmp=Pow(a[x],tmp,pi[i],flag);
if(flag) tmp+=phi[i-];
x--;
}
return tmp;
} int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int n,mod;
read(n); read(mod);
for(int i=;i<=n;++i) read(a[i]);
int tmp=mod;
while(pi[lim]!=)
{
pi[++lim]=tmp;
phi[lim]=get_phi(tmp);
tmp=phi[lim];
}
int q;
read(q);
int l,r;
while(q--)
{
read(l); read(r);
printf("%d\n",f(r,r-l+));
}
return ;
}
Codeforces 906 D. 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 ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- 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 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]906D Power Tower
虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,m,表示数 ...
随机推荐
- 腾讯/阿里/百度 BAT人才体系的职位层级、薪酬、晋升标准
互联网圈有这么一句话:百度的技术,阿里的运营,腾讯的产品.那么代表互联网三座大山的BAT,内部人才体系有什么区别呢?今天老李就带领大家看一看~ ★ 腾讯 ★ 1. 职级 腾讯职级体系分6级,最低1 ...
- PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)
又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...
- 简言MVC
什么是MVC? MVC是一种代码的组织结构,在一个工程项目中,将代码的数据处理,逻辑单元和交互部分分离开来达到一种低耦合的效果,便于工程的修改.MVC中M代表Model,V代表View,C代表Cont ...
- Spring学习总结之面向切面(AOP)
AOP术语 通知(advice):定义切面是什么以及什么时候使用 连接点(join point):应用在执行过程中能够插入切面的点 切点(pointcut):切点的定义会匹配通知所要织入的一个或多个连 ...
- 云平台项目--学习经验--打包压缩工具requirejs
requirejs是一个JavaScript模块加载器.适合在浏览器中国使用,也可以在其他脚本环境使用,它鼓励了代码的模块化.使用RequireJS加载模块化脚本将提高代码的加载速度和质量.如何加载R ...
- acegi security实践教程—入门
Acegi Security概念 Acegi Security是基于J2EE的企业软件应用提供全面的安全服务.通俗的说,就是封装的安全框架.提到安全,大家脑子中第一反应肯定是权限控制.的确如此, ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- PAT 甲级 1079 Total Sales of Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805388447170560 A supply chain is a ne ...
- docker cp 和docker exec 查看docker 运行的容器信息
1. 使用docker 运行 redis 和 postgresql docker run -d -p : -v /redis/data/:/data redis docker run -d -p : ...
- PHP仿LED点阵,读取字库文字,并转化为二进制输出
<?php $xml=simplexml_load_file("zimu.xml");//导入XML文件,从XML文件里知道需要提取的字体的信息 $font_height=$ ...