In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For example, the complexity of a typical disjoint set is O(nα(n))O(*n**α(n)). Here α(n)α(n) is Inverse Ackermann Function, which growth speed is very slow. So in practical application, we often assume α(n) \le 4α(n*)≤4.

However O(α(n))O(α(n)) is greater than O(1)O(1), that means if nn is large enough, α(n)α(n) can greater than any constant value.

Now your task is let another slowly function loglog∗ xx* reach a constant value bb. Here loglog∗ is iterated logarithm function, it means “the number of times the logarithm function iteratively applied on xx* before the result is less than logarithm base aa”.

Formally, consider a iterated logarithm function log_{a}^loga*∗

Find the minimum positive integer argument xx, let log_{a}^* (x) \ge b*log**a∗(x)≥b. The answer may be very large, so just print the result xx* after mod mm.

Input

The first line of the input is a single integer T(T\le 300)T(T≤300) indicating the number of test cases.

Each of the following lines contains 33 integers aa , bb and mm.

1 \le a \le 10000001≤a≤1000000

0 \le b \le 10000000≤b≤1000000

1 \le m \le 10000001≤m≤1000000

Note that if a==1, we consider the minimum number x is 1.

Output

For each test case, output xx mod mm in a single line.

Hint

In the 4-th4−*t**h* query, a=3a=3 and b=2b=2. Then log_{3}^* (27) = 1+ log_{3}^* (3) = 2 + log_{3}^* (1)=3+(-1)=2 \ge blog3∗(27)=1+log3∗(3)=2+log3∗(1)=3+(−1)=2≥b, so the output is 2727 mod 16 = 1116=11.

样例输入复制

5
2 0 3
3 1 2
3 1 100
3 2 16
5 3 233

样例输出复制

1
1
3
11
223

本题为 CF-906D 题目的更改版,请进我这篇博客学习对应题目:

https://www.cnblogs.com/qieqiemin/p/11478970.html

 本题只需要改一下读入,加一个对幂次为0的特判即可通过。

#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;
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, m);return powmod(a, 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);scanf("%d", &q);int l, r;while (q--){scanf("%d %d %lld", &a, &r, &m);if (r == 0){printf("%lld\n", 1 % m );continue;}printf("%lld\n", solve(1, r, m) % m);}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';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11478985.html

The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)相关推荐

  1. H. Holy Grail(The Preliminary Contest for ICPC Asia Nanjing 2019题解)

    题目链接 As the current heir of a wizarding family with a long history,unfortunately, you find yourself ...

  2. F. Greedy Sequence(The Preliminary Contest for ICPC Asia Nanjing 2019题解)

    题目链接 You're given a permutation aaa of length n (1≤n≤105). For each i∈∈[1,n], construct a sequence s ...

  3. The Preliminary Contest for ICPC Asia Nanjing 2019ICPC南京网络赛

    B.super_log (欧拉降幂) •题意 定一个一个运算log*,迭代表达式为 给定一个a,b计算直到迭代结果>=b时,最小的x,输出对m取余后的值 •思路 $log*_{a}(a^{a}) ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019

    小   银   川,给大爷们AK穿了 比赛地址:https://www.jisuanke.com/contest/3005?view=challenges A.(待补) B.1e9的数据范围,一开始从 ...

  5. The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 XKC's basketball team

    XKC , the captain of the basketball team , is directing a train of nn team members. He makes all mem ...

  6. The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs(卡了线段树空间的思维题)

    传送门:https://nanti.jisuanke.com/t/41399 题目描述 There are NNN light bulbs indexed from 000 to N−1N−1N−1. ...

  7. The Preliminary Contest for ICPC Asia Shanghai 2019 Light bulbs

    https://nanti.jisuanke.com/t/41399 离散差分 #include <iostream> #include <cstring> #include ...

  8. The Preliminary Contest for ICPC Asia Shanghai 2019 BDL

    传送门 B:离散化+差分 #include <bits/stdc++.h> using namespace std; typedef long long ll; struct node{i ...

  9. The Preliminary Contest for ICPC Asia Xuzhou 2019 - C Buy Watermelon | 读题

    They want to cut the watermelon in two parts, and each part weighs two times as much as a kilogram . ...

最新文章

  1. 深入理解ES6笔记(九)JS的类(class)
  2. Generating RSA keys in PKCS#1 format in Java--转
  3. erlang精要(2)-数制
  4. java 线程 状态 图_Java提高——多线程(一)状态图
  5. 肖仰华 | 基于知识图谱的可解释人工智能:机遇与挑战
  6. Multiple Gift(AtCoder-3731)
  7. [机器学习-Sklearn]决策树学习与总结 (ID3, C4.5, C5.0, CART)
  8. 两个datatable的比较
  9. Go 语言 bytes.Buffer 源码详解之1
  10. python中get和getall_Scrapy框架get() 、getall() 、extract() 、extract_first()的区别
  11. 开发者强势围观!Gartner 发布 2020 年十大战略科技发展趋势
  12. Qt实现简单的密码加密
  13. 德赛西威领投智驾科技B轮3亿元,加速智能驾驶人人可享
  14. Vue播放m3u8视频格式的视频监控(适合移动端和pc端),用了Video.js,以及多视频列表循环播放代码,已测试,可播放
  15. (1)从1开始写一个操作系统
  16. nlp-问答任务-抽取式问答
  17. 一个很棒的字帖生成器
  18. [TJOI2013] 单词
  19. re模块(正则表达式)
  20. vue使用Echarts问题汇总

热门文章

  1. Eclipse里代码自动完成 auto completion的快捷键设置
  2. What happened when we perform Maven Update Project in Eclipse
  3. SAP Hybris Accelerator的安装和使用方式
  4. get metadata in QHD - still has cache logic
  5. 一个SAP加拿大实习生在当地观察到的美景
  6. 瑞幸咖啡官网的视频播放原理讲解
  7. 几分钟学会归并排序和快速排序
  8. php session支持,SESSION支持
  9. deinstall 卸载grid_oracle 11g RAC手动卸载grid,no deinstall   .
  10. 计算机没有游戏扫雷,系统没有扫雷游戏怎么办 简单几步轻松安装