CF 定理题
14.14%
1000ms
131072K
In Complexity theory, some functions are nearly O(1)O(1)O(1), but it is greater then O(1)O(1)O(1). For example, the complexity of a typical disjoint set is O(nα(n))O(nα(n))O(nα(n)). Here α(n)α(n)α(n) is Inverse Ackermann Function, which growth speed is very slow. So in practical application, we often assume α(n)≤4α(n) \le 4α(n)≤4.
However O(α(n))O(α(n))O(α(n)) is greater than O(1)O(1)O(1), that means if nnn is large enough, α(n)α(n)α(n) can greater than any constant value.
Now your task is let another slowly function log∗log*log∗ xxx reach a constant value bbb. Here log∗log*log∗ is iterated logarithm function, it means “the number of times the logarithm function iteratively applied on xxx before the result is less than logarithm base aaa”.
Formally, consider a iterated logarithm function loga∗log_{a}^* loga∗
Find the minimum positive integer argument xxx, let loga∗(x)≥blog_{a}^* (x) \ge bloga∗(x)≥b. The answer may be very large, so just print the result xxx after mod mmm.
Input
The first line of the input is a single integer T(T≤300)T(T\le 300)T(T≤300) indicating the number of test cases.
Each of the following lines contains 333 integers aaa , bbb and mmm.
1≤a≤10000001 \le a \le 10000001≤a≤1000000
0≤b≤10000000 \le b \le 10000000≤b≤1000000
1≤m≤10000001 \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 xxx mod mmm in a single line.
Hint
In the 4−th4-th4−th query, a=3a=3a=3 and b=2b=2b=2. Then log3∗(27)=1+log3∗(3)=2+log3∗(1)=3+(−1)=2≥blog_{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 272727 mod 16=1116 = 1116=11.
样例输入
5
2 0 3
3 1 2
3 1 100
3 2 16
5 3 233
样例输出
1
1
3
11
223
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll Mod(ll x,ll n)
{
return x<n?x:x%n+n;
}
ll E(ll n)
{
ll res=n;
for(ll i=2;i*i<=n;i++)
{
if(n%i==0) res=res/i*(i-1);
while(n%i==0)
{
n/=i;
}
}
if(n>1)
{
res=res/n*(n-1);
}
return res;
}
ll qpow(ll x,ll y,ll mod)
{
ll ans=1ll;
while(y)
{
if(y&1) ans=Mod(ans*x,mod);
x=Mod(x*x,mod);
y>>=1;
}
return ans;
}
ll func(ll n,ll m,ll mod)
{
if(m==0) return Mod(1,mod);
if(mod==1) return Mod(n,mod);
return qpow(n,func(n,m-1ll,E(mod)),mod);
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
ll n,m,mod;
scanf("%lld%lld%lld",&n,&m,&mod);
printf("%lld\n",func(n,m,mod)%mod);
}
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[N];
ll n;
map<ll,ll>mp;
inline ll Mod(ll x,ll mod)
{
return x<mod?x:x%mod+mod;
}
ll E(ll n)
{
if(mp.count(n))
{
return mp[n];
}
ll res=n;
for(ll i=2;i*i<=n;i++)
{
if(n%i==0) res=res/i*(i-1);
while(n%i==0)
{
n/=i;
}
}
if(n>1)
{
res=res/n*(n-1);
}
return mp[n]=res;
}
ll qpow(ll x,ll y,ll mod)
{
ll ans=1ll;
while(y)
{
if(y&1) ans=Mod(ans*x,mod);
x=Mod(x*x,mod);
y>>=1;
}
return ans;
}
ll func(ll l,ll r,ll mod)
{
if(mod==1||l==r) return Mod(a[l],mod);
return qpow(a[l],func(l+1,r,E(mod)),mod);
}
int main()
{
ll mod;
scanf("%lld%lld",&n,&mod);
for(int i=1;i<=n;i++)
{
scanf("%lld\n",&a[i]);
}
int q;scanf("%d",&q);
for(int i=0;i<q;i++)
{
ll l,r;
scanf("%lld%lld",&l,&r);
printf("%lld\n",func(l,r,mod)%mod);
}
}