2019 ICPC Preliminary Contest Nanjing: super_log(欧拉降幂)

题目:https://nanti.jisuanke.com/t/41299

题意:定义\log_{a}^{*}(x) = \left\{\begin{matrix} -1 \cdot \cdot \cdot \cdot \cdot \cdot \cdot \cdot \cdot \cdot x<1\\ 1+\log_{a}^{*}(\log_{a}x) \cdot \cdot \cdot \cdot \cdot x>=1 \end{matrix}\right.,给定a,b,p,求满足\log_{a}^{*}(x) \geqslant b的最小 x%p.

思路:a^{\log_{a}^{*}(x)} = a\cdot a^{\log_{a}^{*}(x1)} = ... = a\cdot \cdot \cdot a \geqslant a^{b}

满足条件的最小b:b = a^{a^{a^{...}}} 共 b个a

所以 ans = a^{a^{a^{...}}} \mod p

考虑广义欧拉降幂

2019 ICPC Preliminary Contest Nanjing: super_log(欧拉降幂)_第1张图片

 

所以有:F(a,b,p) = pow(a, F(a, b-1, p), p),对于是否互质的两种情况,在快速幂模的时候模完在加上模的值即可。

相关题目:http://codeforces.com/problemset/problem/906/D | 题解

代码:

#include
using namespace std;
#define int long long
typedef long long LL;
unordered_map mp;
inline int mod(int a,int m) {return a>=m?a%m+m:a;}
LL quick_pow(LL a,LL b,LL m){
	LL ans=1;
	while(b){
		if(b&1) ans=mod(ans*a,m);
		a=mod(a*a,m); b>>=1;
	} return ans;
}
int a, b, m, t;
int phi(int n)
{
	if(mp.count(n)) return mp[n];
	int rea=n;
	for(int i=2; i*i<=n; i++)
	if(n%i==0)
	{
		rea=rea-rea/i;
		do
			n/=i;
		while(n%i==0);
	}
	if(n>1)
	rea=rea-rea/n;
	mp[n]=rea;
	return rea;
}
int solve(int cnt,int m){
	if(cnt==0||m==1) return 1;
	return quick_pow(a, solve(cnt-1, phi(m)), m);
}
int32_t main()
{
    //phi_table(1e6+3);
 	scanf("%lld", &t);
 	while(t--){
        scanf("%lld%lld%lld",&a, &b, &m);
        printf("%lld\n",solve(b,m)%m);
 	}
}

 

你可能感兴趣的:(数论)