题目链接  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 rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 1e5 + 10; int n, q;
LL a[N];
LL m;
map <LL, LL> f; LL phi(LL n){
if (f.count(n)) return f[n];
LL ans = n, z = 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;
return f[z] = ans;
} LL Pow(LL a, LL b, LL mod){
LL ret = 1;
LL fl = a >= mod;
for (; b; b >>= 1){
if (b & 1){
ret *= a;
if (ret >= mod) fl = 1, ret %= mod;
} a *= a;
if (a >= mod) a %= mod, fl = 1;
} return ret + fl * mod;
} LL solve(int l, int r, LL mod){
if (l == r) return a[l];
if (mod == 1) return 1;
return Pow(a[l], solve(l + 1, r, phi(mod)), mod);
} int main(){ scanf("%d%lld", &n, &m);
rep(i, 1, n) scanf("%lld", a + i); scanf("%d", &q);
while (q--){
int x, y;
scanf("%d%d", &x, &y);
printf("%lld\n", solve(x, y, m) % m);
} return 0;
}

  

Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)的更多相关文章

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

  2. Codeforces 871D Paths (欧拉函数 + 结论)

    题目链接  Round  #440  Div 1  Problem D 题意   把每个数看成一个点,如果$gcd(x, y) \neq 1$,则在$x$和$y$之间连一条长度为$1$的无向边.   ...

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

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

  4. D - Power Tower欧拉降幂公式

    题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...

  5. [Codeforces]906D Power Tower

    虽说是一道裸题,但还是让小C学到了一点姿势的. Description 给定一个长度为n的数组w,模数m和询问次数q,每次询问给定l,r,求: 对m取模的值. Input 第一行两个整数n,m,表示数 ...

  6. Please, another Queries on Array? CodeForces - 1114F (线段树,欧拉函数)

    这题刚开始看成求区间$\phi$和了........先说一下区间和的做法吧...... 就是说将题目的操作2改为求$(\sum\limits_{i=l}^{r}\phi(a[i]))\%P$ 首先要知 ...

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

    题意 给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$ 分析 由扩 ...

  8. Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]

    Codeforces 洛谷:咕咕咕 CF少有的大数据结构题. 思路 考虑一些欧拉函数的性质: \[ \varphi(p)=p-1\\ \varphi(p^k)=p^{k-1}\times (p-1)= ...

  9. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

随机推荐

  1. HihoCoder - 1636 Pangu and Stones(区间DP)

    有n堆石子,每次你可以把相邻的最少L堆,最多R堆合并成一堆. 问把所有石子合并成一堆石子的最少花费是多少. 如果不能合并,输出0. 石子合并的变种问题. 用dp[l][r][k]表示将 l 到 r 之 ...

  2. #include "*.c"文件的妙用

    在看uCOS II V2.91版本源代码时,在ucos_ii.c源文件中发现下面的代码: #include <os_core.c> #include <os_flag.c> # ...

  3. 2190: [SDOI2008]仪仗队(欧拉函数)

    2190: [SDOI2008]仪仗队 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3235  Solved: 2089 Description 作 ...

  4. data相关应用

    文案参考:HTML5中的data-*属性和jQuery中的.data()方法使用 data属性选择器 $("li[data-id='1']")//选择li元素中data-id属性等 ...

  5. Leetcode 502.IPO

    IPO 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  6. [oldboy-django][2深入django]django 官方中文文档 --扩展User

    https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#extending-the-existing-user-model # 另 ...

  7. Spring框架DataSource

    一 DataSource 简易介绍 JDK里 javax.sql的一个接口 public interface DataSource 表示无力数据源的连接,作为DriverManager设施的替代项, ...

  8. idea的ssm搭建(复制)

    1.工具/原料 • apache-tomcat-7.0.63 http://download.csdn.net/detail/lxfhahaha/9778163 • apache-maven-3.3. ...

  9. BZOJ 3625 [Codeforces Round #250]小朋友和二叉树 ——NTT 多项式求逆 多项式开根

    生成函数又有奇妙的性质. $F(x)=C(x)*F(x)*F(x)+1$ 然后大力解方程,得到一个带根号的式子. 多项式开根有解只与常数项有关. 发现两个解只有一个是成立的. 然后多项式开根.求逆. ...

  10. Codeforces Round #361 (Div. 2) B bfs处理

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...