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,表示数 ...
随机推荐
- zooland 新开源的RPC项目,希望大家在开发的微服务的时候多一种选择,让微服务开发简单,并且容易上手。
zooland 我叫它动物园地,一个构思很长时间的一个项目.起初只是觉得各种通信框架都封装的很好了,但是就是差些兼容,防错,高可用.同时在使用上,不希望有多余的代码,像普通接口一样使用就可以了. 基于 ...
- stl源码剖析 详细学习笔记deque(1)
//--------------------------15/3/12---------------------------- deque { deque没有容量(capacity)观念,是动态分段的 ...
- LeetCode Container With Most Water (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- Asp.Net_Form验证跟授权
配置文件的<system.web></system.web>结点下添加如下代码: <!--身份验证方式--> <authentication mode=&qu ...
- python中魔法方法__init__,__str__,__del__的详细使用方法
1. python中的魔法方法, 类似__init__, __str__等等,这些内置好的特定的方法进行特定的操作时会自动被调用 2. __init__的使用方法 class 类名(object): ...
- Docker部署Registry私有镜像库
拉取镜像 docker pull registry:2.6.2 生成账号密码文件,这里采用htpasswd方式认证 docker run --rm --entrypoint htpasswd re ...
- 3. Python3 基本数据类型
Python3 基本数据类型 Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在 Python 中,变量就是变量,它没有类型,我们所说的"类型& ...
- PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)
又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...
- Linux内核分析作业 NO.1
通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的 于佳心 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/cour ...
- A-Softmax的总结及与L-Softmax的对比——SphereFace
A-Softmax的总结及与L-Softmax的对比--SphereFace \(\quad\)[引言]SphereFace在MegaFace数据集上识别率在2017年排名第一,用的A-Softmax ...