当前位置: 代码迷 >> 综合 >> 2019 ICPC南京站网络赛 B super_log和 CodeForces 906D Power Tower(幂塔函数+欧拉降幂模板)
  详细解决方案

2019 ICPC南京站网络赛 B super_log和 CodeForces 906D Power Tower(幂塔函数+欧拉降幂模板)

热度:86   发布时间:2024-01-15 06:15:03.0

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,b,mod,求log*a(x)>=b,输出min(x)%mod

分析:

本题就是求b个a的类似a^a^a^a^a的次方,自然可以想到欧拉降幂,比赛的时候找的是这题的模板,CodeForces 906D Power Tower

所以欧拉降幂即可

 

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
#define Mod(a,b) a<b?a:a%b+b
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{int ret=0, flag=0;char ch;if((ch=getchar())=='-')flag=1;else if(ch>='0'&&ch<='9')ret = ch - '0';while((ch=getchar())>='0'&&ch<='9')ret=ret*10+(ch-'0');return flag ? -ret : ret;
}
LL mod;
map<LL,LL> mp;
LL phi(LL k)
{LL i,s=k,x=k;if (mp.count(k))return mp[x];for(i = 2; i * i <= k; i++){if(k % i == 0)s = s / i * (i - 1);while(k % i == 0)k /= i;}if(k > 1)s = s / k * (k - 1);mp[x]=s;return s;
}LL qpow(LL x,LL n,LL mod)
{LL res=1LL;while(n){if (n&1LL)res=Mod(res*x,mod),n--;x=Mod(x*x,mod);n>>=1LL;}return res;
}
LL f(LL n,LL m,LL mod)
{/*if (m==1LL||mod==1LL)return Mod(n,mod); */if(m==0) return 1;if(mod==1) return 1;return qpow(n,f(n,m-1LL,phi(mod)),mod);
}int main()
{int T;scanf("%d",&T);while(T--){LL n,m;scanf("%lld%lld%lld",&n,&m,&mod);/*if(mod==1LL)printf("0\n");else if(n==1LL)printf("1\n");else if(m==0)printf("%lld\n",1LL);else */printf("%lld\n",f(n,m,mod)%mod);}return 0;
}

codefoce

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
#define Mod(a,b) a<b?a:a%b+b
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{int ret=0, flag=0;char ch;if((ch=getchar())=='-')flag=1;else if(ch>='0'&&ch<='9')ret = ch - '0';while((ch=getchar())>='0'&&ch<='9')ret=ret*10+(ch-'0');return flag ? -ret : ret;
}
map<LL,LL> mp;
LL a[100010];
LL phi(LL k)
{LL i,s=k,x=k;if (mp.count(k))return mp[x];for(i = 2; i * i <= k; i++){if(k % i == 0)s = s / i * (i - 1);while(k % i == 0)k /= i;}if(k > 1)s = s / k * (k - 1);mp[x]=s;return s;
}LL qpow(LL x,LL n,LL mod)
{LL res=1LL;while(n){if (n&1LL)res=Mod(res*x,mod),n--;x=Mod(x*x,mod);n>>=1LL;}return res;
}
LL slove(LL l,LL r,LL mod)
{if(r<l)  return 1;if(mod==1) return 1; //if(l==r||mod==1) return Mod(a[l],mod);return qpow(a[l],slove(l+1,r,phi(mod)),mod);
}
int main()
{LL mod,n;scanf("%I64d%I64d",&n,&mod);for(LL i=1; i<=n; i++)scanf("%I64d",&a[i]);LL q;scanf("%I64d",&q);for(int i=1; i<=q; i++){int l,r;scanf("%d%d",&l,&r);printf("%I64d\n",slove(l,r,mod)%mod);}return 0;
}

 

  相关解决方案