CodeForces 907F Power Tower(扩展欧拉定理)
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p.
Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will be

After tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109).
Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have.
Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you.
kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
6 1000000000
1 2 2 3 3 3
8
1 1
1 6
2 2
2 3
2 4
4 4
4 5
4 6
1
1
2
4
256
3
27
597484987
327 = 7625597484987
题意:给出一个数字序列和一个固定的模数mod,给出q个询问,每次询问f(l,r)
f(l,r) =a[l]^(a[l+1]^(a[l+2]^(a[l+3]^(...^a[r])))%mod (^是幂次的意思)
题解:扩展欧拉定理告诉我们

然后我们尝试展开a^b^c

再往下也是一样的,我们可以先预处理出phi[p],phi[phi[p]]……
大概要处理几层呢?logn层,为什么呢?
假设phi[now]=1了
那么之上不管多少层
x=1,2,3,4,5……
这些数模一都是一
所以就成了欧拉函数的衰变速度(我瞎糊的名词,意思是经过几次phi,p会变成1)
这个复杂度是logn的,我们可以对这进行一发dfs,加上快速幂的logn复杂度,总复杂度是loglogn的,值得一提的是,快速幂中也要改成扩展欧拉定理的形式,否则小心炸掉~
顺便可以研究一下这道题是怎么被博主伪装成线段树的
U23882 天真♂哲学家♂树(Naive Philosopher Tree)
代码如下:
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int a[],phi[],n,m,mod; int get(int x)
{
int ans=x;
for(int i=;i*i<=x;i++)
{
if(x%i==)
{
ans=ans/i*(i-);
while(x%i==)
{
x/=i;
}
}
}
if(x!=)
{
ans=ans/x*(x-);
}
return ans;
} int gg(long long x,int p)
{
return x>=p?x%p+p:x;
} int kasumi(int a,int b,int p)
{
int ans=;
while(b)
{
if(b&)
{
ans=gg(1ll*ans*a,p);
}
a=gg(1ll*a*a,p);
b>>=;
}
return ans;
} int dfs(int l,int r,int i)
{
if(l==r||phi[i]==)
{
return gg(a[l],phi[i]);
}
return kasumi(a[l],dfs(l+,r,i+),phi[i]);
} int main()
{
scanf("%d%d",&n,&mod);
phi[]=mod;
for(int i=;i<=;i++)
{
phi[i]=get(phi[i-]);
}
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dfs(l,r,)%mod);
}
return ;
}
CodeForces 907F Power Tower(扩展欧拉定理)的更多相关文章
- [CodeForces - 906D] Power Tower——扩展欧拉定理
题意 给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$ 分析 由扩 ...
- 【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
虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,m,表示数 ...
- 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 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)
题目链接:http://codeforces.com/contest/906/problem/D 题目大意:给定n个整数w[1],w[2],……,w[n],和一个数m,然后有q个询问,每个询问给出一个 ...
- 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_ ...
- [luogu4139]上帝与集合的正确用法【欧拉定理+扩展欧拉定理】
题目大意 让你求\(2^{2^{2^{\cdots}}}(mod)P\)的值. 前置知识 知识1:无限次幂怎么解决 让我们先来看一道全国数学竞赛的一道水题: 让你求解:\(x^{x^{x^{\cdot ...
随机推荐
- react.js的了解
React很大的特点就是“轻”,再加上VDOM这个很好的idea让React非常非常快(在上面那个测试里面0.3s左右就载入完毕).另外React和Angular一个很大的不同就是React采用的是o ...
- thinkphp5集成微信支付【公众号支付】快捷路径
1 下载官方的测试用例PHP版 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1 2 到vendor目录新建weixin文件夹 ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery
一.简介 spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...
- [Z] SVN的trunk、branch、tag
Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是 svn://proj/|+-trunk+-branches+-ta ...
- 爬虫——回顾HTTP 协议
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- create-react-app react脚手架
create-react-app react脚手架 官方脚手架 1.安装 npm install -g create-react-app 2.创建项目 create-react-app react-c ...
- C# 有关控件、自定义类事件中的委托链的获取、移除操作
直接来代码吧,这样干脆直接,也不耽误我午休了.一切尽在源码中. public class ControlEventTool { /// <summary> /// 移除控件的某类事件, 如 ...
- ojective-c convert to pascal pattern
ojective-c convert to pascal pattern http://www.cnblogs.com/cnsoft/archive/2013/06/09/3128619.html C ...
- Java使用 VelocityEngine模板引擎快速生成HTML等各种代码
https://blog.csdn.net/icannotdebug/article/details/79725297 一.简介 Velocity 是一个基于 Java 的模板引擎框架,提供的模板语言 ...
- Server_id 冲突导致 IO 等待故障
问题描述: 线上添加新的 MySQL Slave 后,服务器异常. 1.show processlist; Queueing master event to the relay log Reconne ...