题意

给你 $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——扩展欧拉定理的更多相关文章

  1. CodeForces 907F Power Tower(扩展欧拉定理)

    Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is u ...

  2. 【CodeForces】906 D. Power Tower 扩展欧拉定理

    [题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [ ...

  3. CodeForces - 906D Power Tower(欧拉降幂定理)

    Power Tower CodeForces - 906D 题目大意:有N个数字,然后给你q个区间,要你求每一个区间中所有的数字从左到右依次垒起来的次方的幂对m取模之后的数字是多少. 用到一个新知识, ...

  4. [Codeforces]906D Power Tower

    虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,m,表示数 ...

  5. Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)

    题目链接  Power Tower 题意  给定一个序列,每次给定$l, r$ 求$w_{l}^{w_{l+1}^{w_{l+2}^{...^{w_{r}}}}}$  对m取模的值 根据这个公式 每次 ...

  6. 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个询问,每个询问给出一个 ...

  7. 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 ...

  8. CodeForces 906D (欧拉降幂)

    Power Tower •题意 求$w_{l}^{w_{l+1}^{w_{l+2}^{w_{l+3}^{w_{l+4}^{w_{l+5}^{...^{w_{r}}}}}}}}$ 对m取模的值 •思路 ...

  9. CF906D Power Tower

    扩展欧拉定理 CF906D Power Tower 洛谷交的第二个黑题 题意 给出一个序列\(w-1,w_2,\cdots,w_n\),以及\(q\)个询问 每个询问给出\(l,r\),求: \[w_ ...

随机推荐

  1. C++中的虚函数以及虚函数表

    一.虚函数的定义 被virtual关键字修饰的成员函数,目的是为了实现多态 ps: 关于多态[接口和实现分离,父类指针指向子类的实例,然后通过父类指针调用子类的成员函数,这样可以让父类指针拥有多种形态 ...

  2. Linux命令sort和uniq 的基本使用

    uniq 123.txt  去除连续重复uniq -u 123.txt  保留唯一uniq -c 123.txt  去重并计算出现的个数sort -n 123.txt | uniq -c 排序后去重s ...

  3. 面试6 --- 当List<String> list =new ArrayList<String>(20); 他会扩容多少次

    当List<String> list =new ArrayList<String>(20); 他会扩容多少次?A 0       B 1 C 2 D 3答案是A: 因为这个集合 ...

  4. [SOJ #537]不包含 [CF102129I]Incomparable Pairs(2019-8-6考试)

    题目大意:给定一个长度为$n$的字符串$s$,求有多少个无序字符串二元组$(x,y)$满足:$x,y$是$s$的字串,且$x$不是$y$的字串,$y$不是$x$的字串 题解:发现满足$x,y$是$s$ ...

  5. harbor helm仓库使用

    harbor helm仓库使用 官方文档地址:https://github.com/goharbor/harbor Monocular 从1.0 开始专注于helm 的UI展示,对于部署以及维护已经去 ...

  6. Windows服务创建及发布

    二.创建Windows Service 1.新建一个Windows Service,并将项目名称改为“MyWindowsService”,如下图所示: 2.在解决方案资源管理器内将Service1.c ...

  7. .net core使用ocelot---第八篇 Consul

    简介 .net core使用ocelot---第一篇 简单使用   .net core使用ocelot---第二篇 身份验证使用  .net core使用ocelot---第三篇 日志记录  .net ...

  8. ADO.NET 八(一个例子)

    可视化方式绑定 DataGridView 控件(写的不详细,结合上一篇) 使用可视化数据绑定方式可以快速完成将数据表中的数据显示在 DataGridView 控件中的操作,并可以很容易地对绑定列的属性 ...

  9. SQLSEVER导出 xml文件

    各种都可以参照: 链接:https://wenku.baidu.com/view/778f794bfe4733687e21aa90.html 怎样把SQL Server里的某个表的数据导出成XML文件 ...

  10. Deep learning_CNN_Review:A Survey of the Recent Architectures of Deep Convolutional Neural Networks——2019

    CNN综述文章 的翻译 [2019 CVPR] A Survey of the Recent Architectures of Deep Convolutional Neural Networks 翻 ...