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 blog**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';
}
}
}

The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)的更多相关文章

  1. The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解

    (施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...

  2. [The Preliminary Contest for ICPC Asia Nanjing 2019] A-The beautiful values of the palace(二维偏序+思维)

    >传送门< 前言 这题比赛的时候觉得能做,硬是怼了一个半小时,最后还是放弃了.开始想到用二维前缀和,结果$n\leq 10^{6}$时间和空间上都爆了,没有办法.赛后看题解用树状数组,一看 ...

  3. 计蒜客 The Preliminary Contest for ICPC Asia Nanjing 2019

    F    Greedy Sequence You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each  ...

  4. The Preliminary Contest for ICPC Asia Nanjing 2019

    传送门 A. The beautiful values of the palace 题意: 给出一个\(n*n\)的矩阵,并满足\(n\)为奇数,矩阵中的数从右上角开始往下,类似于蛇形填数那样来填充. ...

  5. The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail

    题目链接:https://nanti.jisuanke.com/t/41305 题目说的很明白...只需要反向跑spfa然后输入-dis,然后添-dis的一条边就好了... #include < ...

  6. 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019

    题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...

  7. B.super_log(The Preliminary Contest for ICPC Asia Nanjing 2019)

    同:https://www.cnblogs.com/--HPY-7m/p/11444923.html #define IOS ios_base::sync_with_stdio(0); cin.tie ...

  8. H.Holy Grail ( floyd )(The Preliminary Contest for ICPC Asia Nanjing 2019)

    题意: 给出一个有向图,再给出6条原来不存在的路径,让你在这6条路径上添加一个最小的数,使图不存在负环. 思路: 直接6遍 floyd 输出就行了. #include <bits/stdc++. ...

  9. F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)

    题意: 查找区间k的后继. 思路: 直接主席树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio&g ...

随机推荐

  1. 三、使用VSCode配置简单的vue项目

    由于最近要使用的项目框架为前后端分离的,采用的是vue.js+webAPI的形式进行开发的.因为之前我没有接触过vue.js,也只是通过视频文档做了一些简单的练习.今天技术主管说让大家熟悉下VSCod ...

  2. ASP.NET Core 入门笔记3,使用ASP.NET Core MVC框架构建Web应用

    一.ASP.NET Core MVC 输出Hello World,Friend! 1.引入 ASP.NET Core MVC 修改应用启动类(Startup.cs),引入MVC模块并配置默认路由 pu ...

  3. DRF视图-请求与响应

    DRF视图 drf的代码简写除了在数据序列化体现以外,在视图中也是可以的.它在django原有的django.views.View类基础上,drf内部封装了许多子类以便我们使用. Django RES ...

  4. Java和SQL取两个字符间的值

    Java String str = "abcdefg"; String result = str.substring(str.indexOf(">")+1 ...

  5. Linux中权限控制ACL命令

    很多小伙伴觉得,Linux的权限管理命令不就是chown和chmod命令吗,什么时候有了ACL了? 什么是ACLACL是访问控制列表(Access Control List)的缩写,主要的目的是在提供 ...

  6. krpano下全屏后弹窗失效问题解决方法

    原因 krpano 自身的全屏仅全屏自身,以外的html效果将无法显示 解决方法 把全屏按钮换成浏览器自身的全屏效果 解决步骤 vtourskin.xml <layer name="n ...

  7. Excel 下来公式 内容却一样

    首先我们打开我们电脑里面的excel2007的软件   我们随便输入一点输入,进行公式计算   我们在上边输入=A1+B1,就能算出这个的结果   我们把上边的公式算好了,点击下拉试试   我们发现虽 ...

  8. 数据排序 sort

    排序命令: 常和管道进行协作的命令  -sort  (默认使用字符的第一个字符进行排序) -n  按数字排序 -r  反序排序 -o  结果 输出到文件 -t  分隔符 (sort -n -t &qu ...

  9. if(!ConnectDBProc(strCmd,m_dbUserName,m_dbPassword))

    https://wenku.baidu.com/view/826b3d426bec0975f565e204.html

  10. django F与Q查询 事务 only与defer

    F与Q 查询 class Product(models.Model): name = models.CharField(max_length=32) #都是类实例化出来的对象 price = mode ...