题目链接:http://codeforces.com/contest/906/problem/D

题目大意:给定n个整数w[1],w[2],……,w[n],和一个数m,然后有q个询问,每个询问给出一个l,r,求w[l]^w[l+1]^w[l+2]……w[r]  %m  ,即a[l]到a[r]的幂次方

解题思路:利用欧拉降幂公式

第一个要求a和p互质,第2个和第3个为广义欧拉降幂,不要求a和p互质,用在这题刚好。

因为有两种情况,所以我们需要自定义一下降幂取模公式。

我们对整个区间进行递归处理,每一个数的指数是它后一个数到右端点的幂。

递归终止条件为到右端点或者p的欧拉函数值为1,再求欧拉函数值的时候我们需要进行记忆化,否则会超时

代码:

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define ll long long
#define MOD(a,b) a>=b?a%b+b:a
#define N 100005
map<ll,ll> mp;
int n,l,r,q;
ll mod,w[N];
ll qpow(ll a,ll b,ll p){
ll res=;
while(b){
if(b&) res=MOD(res*a,p); //为保证指数结果正确,应该用自定义取模
b>>=;
a=MOD(a*a,p);
}
return res;
}
ll phi(ll x){
if(mp[x]) return mp[x];
ll tmp=x,res=x;
for(int i=;i*i<=x;i++){
if(x%i==){
res=res*(i-)/i;
while(x%i==) x/=i;
}
}
if(x>) res=res*(x-)/x;
return mp[tmp]=res;
}
ll solve(int l,int r,ll m){
if(l==r||m==) return MOD(w[l],m);
else return qpow(w[l],solve(l+,r,phi(m)),m);
}
int main() {
scanf("%d%I64d",&n,&mod);
for(int i=;i<=n;i++) scanf("%I64d",&w[i]);
scanf("%d",&q);
while(q--){
scanf("%d%d",&l,&r);
printf("%I64d\n",solve(l,r,mod)%mod);
}
return ;
}

bzoj 3884 上帝与集合的正确用法

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3884

题目大意:和上题很像,只不过所有数都是2,且次方是无穷的了,给定一个正整数p,求2^(2^(2^(2^(2^...)))) mod p的值

解题思路:方法几乎是一样的,因为每次递归幂的模数就会变成原来的欧拉函数值,所以最多经过log(p),模数就会变成1,然后后面结果都一样的了,没必要递归下去,直接结束就好了。

代码:

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define ll long long
#define MOD(a,b) a>=b?a%b+b:a
#define N 100005
map<ll,ll> mp;
int n,l,r,q;
ll mod;
ll qpow(ll a,ll b,ll p){
ll res=;
while(b){
if(b&) res=MOD(res*a,p);
b>>=;
a=MOD(a*a,p);
}
return res;
}
ll phi(ll x){
if(mp[x]) return mp[x];
ll tmp=x,res=x;
for(int i=;i*i<=x;i++){
if(x%i==){
res=res*(i-)/i;
while(x%i==) x/=i;
}
}
if(x>) res=res*(x-)/x;
return mp[tmp]=res;
}
ll solve(ll m){
if(m==) return ;
else return qpow(,solve(phi(m)),m);
}
int main() {
int T;
scanf("%d",&T);
while(T--){
scanf("%lld",&mod);
printf("%lld\n",solve(mod)%mod);
}
return ;
}

Codeforces Round #454 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)的更多相关文章

  1. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...

  2. Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路

    D. Tanya and Password Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/508 ...

  3. Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛

    D. Recover it! Authors guessed an array aa consisting of nn integers; each integer is not less than ...

  4. D - Power Tower欧拉降幂公式

    题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...

  5. Codeforces Round #524 (Div. 2) codeforces 1080A~1080F

    目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...

  6. Codeforces Round #454 Div. 1 [ 906A A. Shockers ] [ 906B B. Seating of Students ] [ 906C C. Party ]

    PROBLEM A. Shockers 题 http://codeforces.com/contest/906/problem/A 906A 907C 解 水题,按照题意模拟一下就行了 如果是 ‘ ! ...

  7. Codeforces Round #454 Div. 1

    B:考虑2*m怎么构造.因为要求相邻的数不能再相邻,容易想到黑白染色之类的东西,考虑染个色然后大概把黑点扔一边白点扔一边.显然m<=3时无解.对m>4,m为偶数时,如1 2 3 4 5 6 ...

  8. Codeforces Round #454 Div. 2 A B C (暂时)

    A. Masha and bears 题意 人的体积为\(V\),车的大小为\(size\),人能钻进车的条件是\(V\leq size\),人对车满意的条件是\(2V\geq size\). 现知道 ...

  9. Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs

    D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...

随机推荐

  1. macaca搭建

    对于新鲜的事务总是那么好奇,在自动化的过程中,有幸了解到macaca,记录下安装过程,具体介绍请移步官网:https://github.com/macacajs/ python版本参考:https:/ ...

  2. Change the environment variable for python code running

    python程序运行中改变环境变量: Trying to change the way the loader works for a running Python is very tricky; pr ...

  3. Jmeter参数化控件意见收集

    1.可以读取EXCEL,可以自定义SHEET,行和列: 2.数据可以加密传输,加密方式如下: 1)SHA1 2)SHA224 3)SHA256 4)SHA384 5)SHA512 6)MD5 7)Hm ...

  4. 04 全局配置,java 编意默认版本,1.6.修改配置

    https://www.cnblogs.com/liu-s/p/5371289.html <!-- 修改Intellij Idea 创建maven项目默认Java编译版本 --> < ...

  5. Python3解leetcode Count Binary Substrings

    问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  6. 【HDOJ6628】permutation 1(dfs)

    题意:求1到n的排列中使得其差分序列的字典序为第k大的原排列 n<=20,k<=1e4 思路:爆搜差分序列,dfs时候用上界和下界剪枝 #include<bits/stdc++.h& ...

  7. CTF_工具网址收藏

    杂项 条形码在线扫描 :    https://online-barcode-reader.inliteresearch.com/ PS弄反色ctr+i    :  https://zhidao.ba ...

  8. windows下源码安装调试postgresql

    环境:windows 10 postgresql版本:postgresql-9.6.5 使用工具:vs2017社区版 辅助工具:perl.diff.flex.bison 相关工具下载地址: perl下 ...

  9. 138、Tensorflow serving 实现模型的部署

    将Tensorflow模型部署成Restful接口 下面是实现过程,整个操作都是在Linux上面实现的,因为Tensorflow Serving 目前还只支持Linux 这个意义真的是革命性的,因为从 ...

  10. JAVA File的创建及相对路径绝对路径

    http://blog.sina.com.cn/s/blog_9386f17b0100w2vv.html JAVA File的创建及相对路径绝对路径 (2011-12-09 08:27:56) 转载▼ ...