Codeforces 906 D Power Tower

Discription

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 wk then value of power of a new tower will be {wk}p.

Rocks are added from the last to the first. That is for sequence w1,?...,?wm 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 wk (1?≤?wk?≤?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.

kth of next q lines contains two integers lk and rk (1?≤?lk?≤?rk?≤?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 lk,?lk?+?1,?...,?rk.

Example

Input

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

Output

1124256327597484987

Note

327?=?7625597484987

首先你得需要知道一个定理:

当x>φ(p)时,a^x  mod p=a^(x mod φ(p)  +φ(p))  mod p。

因为φ(x)迭代不超过log(x)次就成1了(考虑2这个质因子),所以我们直接暴力迭代就行了。

由于我们并不知道下一层的值(也就是这一层的次数)是否大于φ(p),所以%p改成%2p就行了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#define ll long long
#define maxn 100005
using namespace std;
ll a[maxn],n,mod[105],l,r,q,tot=0;

inline ll MO(ll x,ll y){
    return x>y?x%y+y:x;
}

inline ll ksm(ll x,ll y,const ll ha){
    ll an=1;
    for(;y;y>>=1,x=MO(x*x,ha)) if(y&1) an=MO(an*x,ha);
    return an;
}

inline ll phi(ll x){
    int tp=sqrt(x+0.5),y=1;
    for(int i=2;i<=tp;i++) if(!(x%i)){
        x/=i,y*=i-1;
        while(!(x%i)) x/=i,y*=i;
        if(x==1) break;
    }
    if(x!=1) y*=x-1;
    return y;
}

ll solve(int now,ll mo){
    if(now==r||mo==1) return MO(a[now],mo);
    else return ksm(a[now],solve(now+1,mod[now-l+1]),mo);
}

int main(){
    scanf("%lld%lld",&n,mod);
    while(mod[tot]!=1) mod[tot+1]=phi(mod[tot]),tot++;

    for(int i=1;i<=n;i++) scanf("%lld",a+i);
    scanf("%lld",&q);
    while(q--){
        scanf("%lld%lld",&l,&r);
        ll ans=solve(l,mod[0]);
        if(ans>=mod[0]) ans-=mod[0];
        printf("%lld\n",ans);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/JYYHH/p/8384375.html

时间: 2024-07-30 16:56:52

Codeforces 906 D Power Tower的相关文章

【CodeForces】906 D. Power Tower 扩展欧拉定理

[题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [题解]扩展欧拉定理的形式: $$a^b\equiv a^{b\%\varphi(p)+\varphi(p)} \ \ mod \ \ p \ \ (b\leq \varphi(p))$$ 特别注意当b<φ(p)且(a,p)≠1时不变. 假如现在是三个累乘幂a^(b^c),那么根据扩展欧拉定理: $$a^

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

题目链接  Power Tower 题意  给定一个序列,每次给定$l, r$ 求$w_{l}^{w_{l+1}^{w_{l+2}^{...^{w_{r}}}}}$  对m取模的值 根据这个公式 每次递归计算. 因为欧拉函数不断迭代,下降到$1$的级别大概是$log(m)$的,那么对于每一次询问最多需要递归$log(m)$次 注意每次求解欧拉函数的时候要用map存下来,方便以后查询 #include <bits/stdc++.h> using namespace std; #define re

CodeForces 907F Power Tower(扩展欧拉定理)

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

CodeForces 906D Power Tower &lt;&lt;欧拉降幂

题意 给定n个数,q次询问,每次输出[l,r]区间的超级幂,对m取模. 思路 超级幂问题就想到用欧拉降幂来处理 欧拉降幂公式:$a^b \% m=a^{b\%\varphi (m)+\varphi(m)}\%m,(b>\varphi(m))$ 本题用递归处理欧拉降幂,在$logm$次降幂后$\varphi(m)=1$,然后回溯时用快速幂进行计算,总的复杂度大约是$log^{2}m$ $w_0^{w_1^{w_2^{w_3^{...}}}}\% m = w_0^{[w_1^{w_2^{w_3^{.

[CodeForces - 906D] Power Tower——扩展欧拉定理

题意 给你 $n$ 个数 $w$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l^{(w_{l+1}^{(w_{l+2}…^{w_r})})}\mod p$. 分析 由扩展欧拉定理: $$a^b\equiv \begin{cases} a^{b\%\phi(p)}~~~~~~~~~~~gcd(a,p)=1\\ a^b~~~~~~~~~~~~~~~~~~gcd(a,p)\neq1,b<\phi(p)\\ a^{b\%\phi(p)+\phi(p)}~~~~gcd(

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want

【CF906D】Power Tower

题目 题目链接:https://codeforces.com/problemset/problem/906/D 给出一个数列 \(a\),每次询问给出 \(l,r\),求 思路 根据扩展欧拉定理,当 \(b\geq \varphi(p)\) 时, \[a^b\equiv a^{b\bmod \varphi(p)+\varphi(p)}\pmod p \] 所以我们可以考虑递归求解,类似 这题,直到 \(l>r\) 或 \(p=1\). 由于 \(\varphi(\varphi(n))...\)

Codeforces 679B - Bear and Tower of Cubes

679B - Bear and Tower of Cubes 题目大意:一个数x定义一种拆分方式,每次拆分取最大的a 且 a^3<=x,x减去a^3,之后重复同样的操作,直到 x变为0.给你一个数m( m<=1e15 ),让你取一个数q<=m,q能执行的操作数在小于等于m的数里面最大,且在操作数 最大的里面,值是最大的. 感觉这种思维题就是特别难.... 思路:设a为当前小于等于m的最大立方数.则对于当前的 m 我们有两种情况要考虑,第一种是res1=m-a^3 第二种是不想减去a^3,

Codeforces 994F Compute Power 二分+DP

题意:给n个任务 每个任务有两个值$a,b$ 现有许多机器 每台最多可以执行两次任务 若存在第二次任务则满足$a_{second}<a_{first}$ 定义代价$val = \frac { \sum_{i \in S } a[i]} { \sum_{i \in S} b[i] }$ 其中$S$为当做第一次来执行的任务的集合 求$val$的最小值 $n \leq 60,a_i \leq 10^8,b_i \leq 100$ 很容易想到二分最小值并且联想到经典的$01$分数规划 即 $\frac