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个询问,每个询问给出一个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 (欧拉降幂)的更多相关文章
- 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} \] 思路 我们通过迭代发 ...
- 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 ...
- 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 ...
- D - Power Tower欧拉降幂公式
题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...
- Codeforces Round #524 (Div. 2) codeforces 1080A~1080F
目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...
- 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 解 水题,按照题意模拟一下就行了 如果是 ‘ ! ...
- Codeforces Round #454 Div. 1
B:考虑2*m怎么构造.因为要求相邻的数不能再相邻,容易想到黑白染色之类的东西,考虑染个色然后大概把黑点扔一边白点扔一边.显然m<=3时无解.对m>4,m为偶数时,如1 2 3 4 5 6 ...
- Codeforces Round #454 Div. 2 A B C (暂时)
A. Masha and bears 题意 人的体积为\(V\),车的大小为\(size\),人能钻进车的条件是\(V\leq size\),人对车满意的条件是\(2V\geq size\). 现知道 ...
- 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 ...
随机推荐
- 【leetcode】1091. Shortest Path in Binary Matrix
题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...
- Jmeter参数化控件意见收集
1.可以读取EXCEL,可以自定义SHEET,行和列: 2.数据可以加密传输,加密方式如下: 1)SHA1 2)SHA224 3)SHA256 4)SHA384 5)SHA512 6)MD5 7)Hm ...
- php substr_replace()函数 语法
php substr_replace()函数 语法 作用:替换字符串中某串为另一个字符串大理石平台价格 语法:substr_replace(string,replacement,start,lengt ...
- codeforces_D. Treasure Hunting_[DP+Binary Search]
http://codeforces.com/contest/1201/problem/D 题意:n行m列的矩阵中,有k个targets,从[1, 1]出发,每次只能向上下左右四个方向移动一步,且只有在 ...
- js中元素更新value页面体现不出来的原因
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ORA-00020: maximum number of processes (800) exceeded
[oracle@db04-1 ~]$ sqlplus -prelim / as sysdba SQL*Plus: Release 11.2.0.3.0 Production on 星期四 8月 31 ...
- 测开之路七十四:python处理kafka
kafka-python地址:https://github.com/dpkp/kafka-python 安装kafka-python:pip install kafka-python 接收消息 fro ...
- Memecached 服务器安装(一)
Memecached 服务器安装(一) 前提:首先您的php环境已经安装完成,如若没有则参考 http://www.cnblogs.com/xulele/p/5264781.html 安装环境链接:h ...
- node+express解决前端跨域问题
var express = require('express') , app = express(); //解决跨域 app.all('*',function (req, res, next) { r ...
- Aizu - ALDS1_4_C Dictionary
Search III Your task is to write a program of a simple dictionary which implements the following ins ...