[CodeForces - 906D] Power Tower——扩展欧拉定理
题意
给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$
分析
由扩展欧拉定理:
$$a^b\equiv \begin{cases} a^{b\%\phi(p)}~~~~~~~~~~~gcd(a,p)=1\\ a^b~~~~~~~~~~~~~~~~~~gcd(a,p)\neq1,b<\phi(p)\\ a^{b\%\phi(p)+\phi(p)}~~~~gcd(a,p)\neq1,b\geq\phi(p) \end{cases}~~~~~~~(mod~p)$$
与BZOJ 3384类似,但是在BZOJ 3384中,次方是无限的,所以说指数一定大于 $\varphi(p)$,但是这道题中指数不一定大于 $\varphi(p)$,需要重写 Mod。
phi需要记忆话,不然会超时。
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 1e5 + ;
ll n, p, a[maxn];
unordered_map<int, int>phi; ll Mod(ll x, ll mod)
{
return x < mod ? x : x % mod + mod;
} ll euler_phi(ll n)
{
ll m = (ll)sqrt(n + 0.5);
ll ans = n;
for (ll i = ; i <= m; i++)
{
if (n % i == )
{
ans = ans / i * (i - );
while (n % i == ) n /= i; //除尽
}
}
if (n > ) ans = ans / n * (n - ); //剩下的不为1,也是素数
return ans;
} ll get_phi(ll x)
{
if(phi[x]) return phi[x];
return phi[x] = euler_phi(x);
} ll qpow(ll a, ll b, ll p)
{
ll ret = ;
while(b)
{
if(b&) ret = Mod(ret * a, p);
a = Mod(a * a ,p);
b >>= ;
}
return ret;
} ll cal(ll l, ll r, ll p) //a^a^a..^a共b次
{
//printf("%lld %lld\n", t, p);
//if(t == 1) return Mod(a, p);
if(l == r) return Mod(a[l], p);
if(p == ) return Mod(a[l], p);
ll phip = get_phi(p);
return qpow(a[l], cal(l+, r, phip), p); //第一类和第三类
} int main()
{
scanf("%I64d%I64d", &n, &p);
for(int i = ;i <= n;i++) scanf("%I64d", &a[i]);
int q;
scanf("%d", &q);
while(q--)
{
ll l, r;
scanf("%I64d%I64d", &l, &r);
printf("%I64d\n", cal(l, r, p) % p); //这个取模不能少
}
return ;
}
参考链接:
1. https://blog.csdn.net/Charlie_jilei/article/details/79252689
2.https://blog.csdn.net/qq_35914587/article/details/79883547
[CodeForces - 906D] Power Tower——扩展欧拉定理的更多相关文章
- 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】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
虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,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 ...
- CodeForces 906D (欧拉降幂)
Power Tower •题意 求$w_{l}^{w_{l+1}^{w_{l+2}^{w_{l+3}^{w_{l+4}^{w_{l+5}^{...^{w_{r}}}}}}}}$ 对m取模的值 •思路 ...
- CF906D Power Tower
扩展欧拉定理 CF906D Power Tower 洛谷交的第二个黑题 题意 给出一个序列\(w-1,w_2,\cdots,w_n\),以及\(q\)个询问 每个询问给出\(l,r\),求: \[w_ ...
随机推荐
- 【转帖】PowerPC架构:IBM的一座金矿
PowerPC架构:IBM的一座金矿 https://www.eefocus.com/mcu-dsp/365599 <处理器史话>之十五 2016-07-15 14:01 作者:付丽华预计 ...
- hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- quartz2.3.0(四)JobDataMap—带状态集合的定时器内置集合
任务类 package org.quartz.examples.example4; import java.util.Date; import org.quartz.DisallowConcurren ...
- UOJ343 清华集训2017 避难所 构造、打表
传送门 玄学题 考虑构造三个数\(p_1p_2,p_1p_2,p_1p_2\)满足贪心分解会分解为\(p_1^3,p_2,p_2,p_2\),那么需要满足条件 1.\(p_1 , p_2 \in Pr ...
- 【1】BIO与NIO、AIO的区别
一.BIO 在JDK1.4出来之前,我们建立网络连接的时候采用BIO模式,需要先在服务端启动一个ServerSocket,然后在客户端启动Socket来对服务端进行通信,默认情况下服务端需要对每个请求 ...
- Centos 7 替换镜像源
1 备份原始源 [root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.b ...
- JavaTCP粘包、拆包
import java.nio.ByteBuffer; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBu ...
- postman 测试Api接口注意事项
1.简单数据传输 2.对象传输 使用的是post方式请求 在Headers设置: 在Body写入对象信息,主要红线的地方:1.raw选中 2.j'son格式 form表单提交数据测试 在header里 ...
- .net core使用ocelot---第五篇 服务质量
简介 .net core使用ocelot---第一篇 简单使用 .net core使用ocelot---第二篇 身份验证使用 .net core使用ocelot---第三篇 日志记录 .net c ...
- 8 search中的timeout参数
默认的search,是没有时间限制的.比如,一个search,可能要10分钟才能搜完,那么,es就会等10分钟,直到结果出来. 然而,在某些场景下,客户是等不了10分钟的.比如,电商网站,客户宁可 ...