2019 南京ICPC网络赛 B super_log

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.

学完欧拉降幂又来看这个题感觉挺简单的,其中也借鉴了一些大佬的思想,感觉这个代码应该是较为能使人接受的,比较容易理解的。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
ll phi(ll n)
{
    ll ans= n;
    for(ll i=2; i<=sqrt(n); i++)
        if(n%i==0)
        {
            ans=ans/i*(i-1);
            while(n%i==0)
                n/=i;
        }
    if(n>1)
        ans= ans /n*(n-1);
    return ans;
}//求一个数的欧拉函数;
ll power(ll a,ll b,ll p)
{
    ll ans=1%p;
    for(; b; b>>=1)
    {
        if(b&1)
            ans = (long long )ans*a%p;
        a=(long long )a*a%p;
    }
    return ans;
}//快速幂;
ll solve(ll a,ll u,ll x)
{

    if(x==1)
        return 0;
    if(u==0)
        return 1;
    ll y=phi(x);
    ll P = solve (a,u-1,y);//对于扩展欧拉降幂来说,不需要考虑互质了,算出幂数的大小
    if(P<y&&P)//在回溯之后进行比较
        return power(a,P,x);
    else
        return power(a,P+y,x);
}
int main()
{
    ll t,p,a,b;
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>p;
        if(b==0||a==1||p==1)
        {
            cout<<1%p<<endl;
            continue;
        }
        /*if(b==1)
        {
            cout<<a%p<<endl;
            continue;
        }*/     //首先判断可能出现的特殊情况;
        cout<<solve(a,b,p)<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值