题目:https://nanti.jisuanke.com/t/41299
题意:定义,给定a,b,p,求满足的最小 x%p.
思路:
满足条件的最小b: 共 b个a
所以 。
考虑广义欧拉降幂
所以有:,对于是否互质的两种情况,在快速幂模的时候模完在加上模的值即可。
相关题目: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);
}
}