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 ...
随机推荐
- 英语单词composing
composing 来源——书籍Python.Crash.Course.2015.11 Using Individual Values from a List You can use individu ...
- mycat操作mysql示例之分库
准备工作: 服务器192.168.96.12,centos7, jdk,mysql5.7,mycat1.6.x,navicat 搭建步骤: 1.在服务器192.168.96.12服务器上安装mysql ...
- ht-5 treemap特性
(1)TreeMap类通过使用红黑树实现Map接口 (2)TreeMap提供按排序顺序存储键值对的有效手段,同时允许快速检索 (3)不同于散列映射,树映射保证它的元素按键的自然顺序升序排列 (4)Tr ...
- springboot整合 thymeleaf 案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- 20180805-Java 异常处理
try{ //程序代码}catch(ExceptionName e1){ //Catch 块} 下面的例子中声明有两个元素的一个数组,当代码试图访问数组的第三个元素的时候就会抛出一个异常. //文件名 ...
- 初学Cadence 一
点击打开 Design Entry CIS 弹出 不要选 OrCAD Capture,这个组件和OrCAD Capture CIS 相比少了很多东西,对元件的管理不方便.选 OrCAD Capture ...
- AUC
https://www.cnblogs.com/earendil/p/9400275.html
- 007-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐,随机字母数字串
一.转换 1.1.TreeMap 有序Map 无序有序转换 使用默认构造方法: public TreeMap(Map<? extends K, ? extends V> m) 1.2.Ma ...
- response.getWriter()和jsp中的out对象的区别
(1) out和response.getWriter属于的类不同,前者是JspWriter,后者是java.io.PrintWriter.而JspWriter是一个抽象类, PrintWriter是一 ...
- Invalid column name on sql server update after column create
问题:新建一个测试表xx as code into xx select * from xx 给这个表添加一个列val, val列不允许为空,将表中已有的数据val值更新为1 alter table x ...