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 log*log∗ xx reach a constant value bb. Here log*log∗ 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 bloga∗​(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−th 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

题解:求a^a^...(b次)%n的结果。因为n与a不一定互质,所以要利用广义欧拉定理进行降幂。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<ctime>
#include<stack>
using namespace std;
#define mm(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + ;
#define inf 0x3f3f3f3f
const double PI = acos(-1.0); ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);} #define Mod(a,b) a<b?a:a%b+b //根据欧拉定理重定义mod ll fpow(ll a,ll n,ll mod)
{
ll res=;
while(n)
{
if(n&) res=Mod(res*a,mod);
a=Mod(a*a,mod);
n>>=;
}
return res;
} ll phi(ll x) //求x的欧拉函数
{
ll ans=x,tp=sqrt(x);
for(ll i=;i<=tp;++i)
{
if(x%i==)
{
ans=ans-ans/i;
while(x%i==) x/=i;
}
}
if(x>) ans=ans-ans/x;
return ans;
} ll solve(ll a,ll b,ll m)
{
if(m==) return ;
if(b<=) return fpow(a,b,m);
ll p=phi(m);
ll t=solve(a,b-,p); //递归求解
ll g=gcd(a,m);
if(g==||b<p) return fpow(a,t,m);
else return fpow(a,t+p,m);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ll a,b,m;
scanf("%lld %lld %lld",&a,&b,&m);
ll ans=solve(a,b,m)%m;
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. jquery如何实现(textarea) placeholder自动换行?
  2. Hui之Hui.js 官方文档
  3. Mobile Safari调用本地App, 否则进入App Store下载
  4. Pandas-数据探索
  5. 用ADMM求解大型机器学习问题
  6. CentOS----使用yum命令出现“could not retrieve mirrorlist http://mirrorlist.centos.org ***” - ybq155”
  7. MySQL删除更新数据时报1175错误的问题
  8. 将数组之中的省份市区地区ID改成对用中文字符
  9. HTML5新增的CSS类API
  10. java.sql.SQLException: Can not issue executeUpdate() for SELECTs
  11. C语言递归分析
  12. css3制作3d旋转相册
  13. C语言陷阱:浮点运算
  14. 【Alpha版本】冲刺阶段 - Day6 - 乘风
  15. CSS position(定位)属性
  16. (转载)WinRAR离购买许可只剩x天
  17. Linux基础入门-环境变量与文件查找
  18. 设计模式笔记:简单工厂模式(Simple Factory)
  19. 2、订单填写页面 /items/write?skuId=10&amp;orderNo=201903211033410001
  20. BZOJ1819 [JSOI]Word Query电子字典 Trie

热门文章

  1. 第二章、Go-基础语法
  2. 【Spring】org.springframework.web.context.ContextLoaderListen 报错
  3. ubuntu搭建环境
  4. Extjs的文件上传问题
  5. Flink 从0到1学习 —— Flink 中如何管理配置?
  6. Linux及Windows下ActiveMQ下载与安装教程
  7. 9-2、大型项目的接口自动化实践记录----递归判断两个json串是否相等
  8. jQuery插件之路(三)——文件上传(支持拖拽上传)
  9. manifest.json 解析--手机web app开发笔记(三-2)
  10. Spring项目集成ShiroFilter简单实现权限管理