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,表示数 ...
随机推荐
- Zabbix使用总结
1. CentOS 7上启动zabbix-server失败,/var/log/messages中的报错信息如下: Feb :: mysql-server1 systemd: Starting Zabb ...
- effective c++ 笔记 (41-44)
//---------------------------15/04/25---------------------------- //#41 了解隐式接口和编译期多态 { // 1:面向对象编 ...
- Azure : 通过 SendGrid 发送邮件
SendGrid 是什么? SendGrid 是架构在云端的电子邮件服务,它能提供基于事务的可靠的电子邮件传递.并且具有可扩充性和实时分析的能力.常见的用例有:1. 自动回复用户的邮件2. 定期发送信 ...
- 如何干净的卸载docker
先上服务器环境信息: 卸载的原因: 宿主机过段时间就磁盘100%了,导致continart异常退出,后来找了很多解决方案,才发现是安装docker的时候有个配置文件错误(正常的应该是|Storage ...
- sql 更新多条记录
转载:https://www.cnblogs.com/hfultrastrong/p/6905057.html 如果你想更新多行数据,并且每行记录的各字段值都是各不一样,你会怎么办呢?本文以一个示例向 ...
- 第二阶段Sprint9
昨天:重新规划主界面,把视频录制暂放到主页面里 今天:查看有关“共享平台”的资料,看如何实现上传下载功能,并尝试编码, 遇到的问题:看不懂什么意思,照例子做不行,还得需要联网等
- DataGridView列标题居中,内容居中
//列标题居中 dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleC ...
- delphi xe 的替代者 Lazarus
Lazarus的设计目标是应用Free Pascal,所以所有凡是Free Pascal能运行的平台,Lazarus都可以运行.最新版本能运行于Linux,Win32和Mac OS.整个界面的外观和操 ...
- ES6学习笔记(四):异步操作
Promise Promise三种状态 pending.resolved.rejected 使用语法 var promis = new Promise(function(resolve,reject) ...
- maven上传jar包到nexus私服后的存放路径 以及 使用IDEA上传jar包的步骤
maven上传jar包到nexus私服的方法,网上大神详解很多,那么上传后的jar包存放到哪里了呢? 在下使用nexus3.2.1版本,在本地搭建了私服,使用maven上传jar包.最后结果如下: 点 ...