扩展欧拉定理

图依旧偷huge的
批注 2026-04-17 121126

#include<bits/stdc++.h>
using namespace std;
long long a,m,b;
long long mo;
bool f=0;
long long gcd(long long a,long long b) {return b?gcd(b,a%b):a;}
inline void read(long long &x)
{
	char ch=getchar();
	x=0;
	while (!isdigit(ch) && ch^'-') ch=getchar();
	while (isdigit(ch))
	{
		x=x*10+ch-'0';
		if (x>=mo) f=true;
		x%=mo;
		ch=getchar();
	}
}
long long phi(long long n)
{
	long long ans=n;
	for (long long i=2;i*i<=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;
}
long long ksm(long long x,long long y)
{
	long long cnt=1;
	while (y)
	{
		if (y&1) cnt=(cnt*x)%m;
		x=(x*x)%m;
		y>>=1;
	}
	return cnt;
}
int main()
{
	cin >> a >> m;
	if (gcd(a,m) == 1)
	{
		mo=phi(m);
		read(b);
	}
	else
	{
		mo=phi(m);
		read(b);
		if (!f)
		{
			cout << ksm(a,b) << endl;
			return 0;
		}
		b+=mo;
	}
	cout << ksm(a,b) << endl;
	return 0;
}
posted @ 2026-04-17 12:12  msjing  阅读(12)  评论(0)    收藏  举报