D. Power Tower

time limit per test

4.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power w**k then value of power of a new tower will be {w**k}p.

Rocks are added from the last to the first. That is for sequence w1, ..., w**m value of power will be

After tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.

Input

First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109).

Second line of input contains n integers w**k (1 ≤ w**k ≤ 109) which is the power of rocks that priests have.

Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you.

k**th of next q lines contains two integers l**k and r**k (1 ≤ l**k ≤ r**k ≤ n).

Output

Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks l**k, l**k + 1, ..., r**k.

Example

input

Copy

6 10000000001 2 2 3 3 381 11 62 22 32 44 44 54 6

output

Copy

1124256327597484987

Note

327 = 7625597484987

思路:

因为euler( euler(x) ) <= x/2 所以在log(x)次内欧拉函数值就会降为1,并且一直为1.而任何数对1取模的答案都是0,所以我们可以遇见模数为1时就可以结束迭代,

  • 因此每次询问最多迭代log(m)次,每一次迭代只需要一个快速幂的时间复杂度,也是log(m)

  • 因此对于每一个询问综合的时间复杂度是O(log(m)^2)

    注意,在指数循环节中快速幂时,需要在ans>=mod时,取模后再加上mod,以此才满足欧拉降幂定理。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll mod(ll x, ll m)
{
return x >= m ? x % m + m : x;
}
ll powmod(ll a, ll b, ll MOD)
{
ll ans = 1;
while (b)
{
if (b % 2)
ans = mod(ans * a, MOD);
// ans = ans * a % MOD;
// a = a * a % MOD;
a = mod(a * a, MOD);
b /= 2;
}
return ans;
} ll m;
int n;
int q;
ll a[maxn];
map<ll, ll> vis;
ll euler(ll n) { //log(n)时间内求一个数的欧拉值
if (vis.count(n))
{
return vis[n];
}
ll ans = n;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0)
{
ans -= ans / i;
while (n % i == 0) n /= i;
}
}
if (n > 1) ans -= ans / n;
vis[n] = ans;
return ans;
} ll solve(int l, int r, ll m)
{
if (l == r || m == 1)
return mod(a[r], m);
return powmod(a[l], solve(l + 1, r, euler(m)), m);
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
// gbtb;
// cin >> n >> m;
scanf("%d%lld", &n, &m);
repd(i, 1, n)
{
scanf("%lld", &a[i]);
// cin >> a[i];
}
// cin >> q;
scanf("%d", &q);
int l, r;
while (q--)
{
scanf("%d %d", &l, &r);
printf("%lld\n", solve(l, r, m) % m);
// cin >> l >> r;
// cout << solve(l, r, m) % m << endl;
} return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Codeforces Round #454 D. Power Tower (广义欧拉降幂)的更多相关文章

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

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

  2. Power Tower(广义欧拉降幂)

    题意:https://codeforc.es/contest/906/problem/D 计算区间的: ai ^ ai+1 ^ ai+2.......ar . 思路: 广义欧拉降幂: 注意是自下而上递 ...

  3. ACM-数论-广义欧拉降幂

    https://www.cnblogs.com/31415926535x/p/11447033.html 曾今一时的懒,造就今日的泪 记得半年前去武大参加的省赛,当时的A题就是一个广义欧拉降幂的板子题 ...

  4. 广义欧拉降幂(欧拉定理)——bzoj3884,fzu1759

    广义欧拉降幂对于狭义欧拉降幂任然适用 https://blog.csdn.net/qq_37632935/article/details/81264965?tdsourcetag=s_pctim_ai ...

  5. CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)

    Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...

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

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

  7. The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  8. BZOJ 3884——欧拉降幂和广义欧拉降幂

    理论部分 欧拉定理:若 $a,n$ 为正整数,且 $a,n$ 互质,则 $a^{\varphi (n)} \equiv 1(mod \ n)$. 降幂公式: $$a^b=\begin{cases}a^ ...

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

随机推荐

  1. vue中如何编写可复用的组件?

    原文地址 Vue.js 是一套构建用户界面的渐进式框架.我们可以使用简单的 API 来实现响应式的数据绑定和组合的视图组件. 从维护视图到维护数据,Vue.js 让我们快速地开发应用.但随着业务代码日 ...

  2. Top 5 Business Messaging Announcements at Facebook F8 2019

    Top 5 Business Messaging Announcements at Facebook F8 2019 By Iaroslav Kudritskiy May 2, 2019 With t ...

  3. 某CTF比赛writeup

    看到群里别人参加比赛发上来的附件,自己尝试解了一下. 1.提示RSA,提供flag.enc和pub.key附件 一看就是解RSA,公钥分解得到n和e n=86934482296048119190666 ...

  4. 2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path

    Pathfinding is a task of finding a route between two points. It often appears in many problems. For ...

  5. Django2.2 数据库的模块model学习笔记

    一.前言 为什么选用Django2.2,因为从2019年下半年起Django2.2逐渐成为长期支持版本,官网也有数据,所以当然选用维护时间长的版本 二.models的建立 Django的models也 ...

  6. MySQL慢查询—开启慢查询

    ###一.简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. ###二.参数说明 slow_query_log 慢查询开启状态 ...

  7. 大数据备忘录———将数据从oracle导入impala中

    上周遇到了将数据从oracle导入到impala的问题,这个项目耽误了我近一周的时间,虽然是种种原因导致的,但是还是做个总结. 需求首先是跑数据,跑数据这个就不叙述,用的是公司的平台. 讲讲耽误我最久 ...

  8. Springmvc使用注解实现执行定时任务(定时器)

    1.在Spring配置文件中添加 <task:annotation-driven/> 2.在需要调用执行定时任务的类中使用注解 @Service @Lazy(false) //避免spri ...

  9. SQL Server 验证身份证合法性函数(使用VBScript.RegExp)

    原文:SQL Server 验证身份证合法性函数(使用VBScript.RegExp) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wzy0623 ...

  10. length 和 size 区分

    ​ 总是混淆length和size,今天专门区分一下 1.在java代码(.java)中 1.length属性是针对Java中的数组来说的,要求数组的长度可以用其length属性: 2.length( ...