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,表示数 ...
随机推荐
- 设计模式 笔记 组合模式 Composite
//---------------------------15/04/16---------------------------- //Composite 组合模式----对象结构型模式 /* 1:意 ...
- Nginx浅析
Nginx浅析 Nginx是什么 总的来说,Nginx其实就是一个和apache类似的服务器软件. Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD-like ...
- Unity游戏AI记录(2d横板为例)
using System.Collections;using System.Collections.Generic;using UnityEngine; public class GeneralPeo ...
- pip virtualenv requirement
原文:https://blog.csdn.net/u011860731/article/details/46561513 pip可以很方便的安装.卸载和管理Python的包.virtualenv则可以 ...
- Muduo学习笔记(一) 什么都不做的EventLoop
Muduo学习笔记(一) 什么都不做的EventLoop EventLoop EventLoop的基本接口包括构造.析构.loop(). One Loop Per Thread 一个线程只有一个Eve ...
- EOS开发基础之三:使用cleos命令行客户端操作EOS——关于钱包wallet和账户account
好了,上一节我们已经讲了关于wallet的一些基础操作,基本了解了怎么去创建一个钱包,怎么去查看钱包.上锁和解锁钱包等,这一节咱们就来开始操作账户account吧. 上一节讲到了每一个account都 ...
- 微软职位内部推荐-Senior Program Manager
微软近期Open的职位: Title: Senior Program Manager – Bing Multimedia Relevance Group: Search Technology Cent ...
- 词频统计 SPEC 20160911
本文档随时可能修改,并且没有另行通知. 请确保每一次在开始修改你的代码前,读标题中的日期,如果晚于你上次阅读, 请重读一次. 老五在寝室吹牛他熟读过<鲁滨逊漂流记>,在女生面前吹牛热爱&l ...
- Daily Scrum NO.1
工作概况 符美潇(PM): 今日工作 1.根据开发进程分配第一步开发工作,对相应的成员提出今日的开发要求:要求成员自己所负责的线程池,动态爬取,去重,文件分类等部分进行资料的相关了解. 2.Daily ...
- Leetcode题库——38.报数
@author: ZZQ @software: PyCharm @file: countAndSay.py @time: 2018/11/9 14:07 说明:报数序列是一个整数序列,按照其中的整数的 ...