D - Power Tower欧拉降幂公式
题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\)
题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还有一个问题是欧拉降幂公式,
\(a^{b}mod c=a^{b mod\phi(c)+\phi(c)}mod c(a>c)\)
需要改写快速幂
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=(ans*a>=c?ans*a%c+c:ans*a);a=(a*a>=c?a*a%c+c:a*a),b>>=1;}return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=100000+10,inf=0x3f3f3f3f;
ll eu(ll n)
{
ll ans=n;
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0)n/=i;
}
}
if(n!=1)ans=ans/n*(n-1);
return ans;
}
ll a[N],b[40];
int main()
{
int n;ll m;
scanf("%d%lld",&n,&m);
b[0]=m;
for(int i=1;i<=35;i++)b[i]=eu(b[i-1]);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
int q;scanf("%d",&q);
while(q--)
{
int l,r;scanf("%d%d",&l,&r);
if(r-l+1>30)r=l+30;
ll ans=1;
for(int i=r,j=r-l;i>=l;i--,j--)
{
ans=qp(a[i],ans,b[j]);
// printf("%d %lld %lld\n",i,b[j],ans);
}
printf("%lld\n",ans%b[0]);
}
return 0;
}
/********************
2 18
2 2
1
1 2
********************/
D - Power Tower欧拉降幂公式的更多相关文章
- 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个询问,每个询问给出一个 ...
- Applese涂颜色-欧拉降幂公式
链接:https://ac.nowcoder.com/acm/contest/330/E来源:牛客网 题目描述 精通程序设计的 Applese 叕写了一个游戏. 在这个游戏中,有一个 n 行 m 列的 ...
- bzoj3884: 上帝与集合的正确用法 欧拉降幂公式
欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert ...
- HDU4704(SummerTrainingDay04-A 欧拉降幂公式)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submi ...
- 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]
题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...
- 牛客OI测试赛 F 子序列 组合数学 欧拉降幂公式模板
链接:https://www.nowcoder.com/acm/contest/181/F来源:牛客网 题目描述 给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘 ...
- 欧拉降幂公式 Super A^B mod C
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=100000 ...
- HDU4704:Sum(欧拉降幂公式)
Input 2 Output 2 Sample Input 2 由公式,ans=2^(N-1)%Mod=2^((N-1)%(Mod-1)+(Mod-1)) %Mod. 注意:降幂的之后再加一个Mod- ...
- FZU1759(SummerTrainingDay04-B 欧拉降幂公式)
Problem 1759 Super A^B mod C Accept: 1056 Submit: 3444Time Limit: 1000 mSec Memory Limit : 327 ...
随机推荐
- Ubuntu关机时间过长,总是停在logo界面
有时候我们总能遇到ubuntu关机的时候卡住,无法关机,一查看发现是" a stop job is running..." 然后后面接着一串等待时间. 这时候我们需要修改一下sys ...
- [implements] - 一个接口的使用
4种货物,如何使用一个接口实现CRUD: package com.tansuo365.test1.service.goods; import com.tansuo365.test1.entity.Go ...
- AtomicReference实现单例模式
CAS是项乐观锁技术,当多个线程尝试使用CAS同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是被告知这次竞争中失败,并可以再次尝试. 乐观锁的一种实 ...
- Images之base image
Create a base image Most Dockerfiles start from a parent image. If you need to completely control th ...
- Ambari配置Hive,Hive的使用
mysql安装,hive环境的搭建 ambari部署hadoop 博客大牛:董的博客 ambari使用 ambari官方文档 hadoop 2.0 详细配置教程 使用Ambari快速部署Hadoop大 ...
- Python GIL(Global Interpreter Lock)
一.介绍 In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threa ...
- select2 使用方法总结
官网:http://select2.github.io/ 调用 <link href="~/Content/select2.min.css" rel="styles ...
- C++中substr函数的用法
#include<iostream> #include<string> using namespace std; int main(){ string str("12 ...
- File操作-将数据库里的数据写入到指定路径的txt文件里
package com.Cristin.File;//将数据库里的数据写入到指定路径的txt文件里 import java.io.File;import java.io.FileOutputStrea ...
- 【Python】【面向对象】
"""# [[面向对象]]#[访问限制]#如果要让内部属性不被外部访问,可加双下划线,编程私有变量.只有内部可以访问,外部不能访问.class Student(objec ...