#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll inv[100000001];
bool check(ll x)
{
if(x==1||x==0) return false;
if(x==2) return true;
for(int i=2;i<=sqrt(x);i++)
if(x%i==0) return false;
return true;
}
ll ksc(ll x,ll y,ll mod)
{
return (x*y-(ll)((ld)x/mod*y)*mod+mod)%mod;
}
ll ksm(ll a,ll b,ll mod)
{
ll res=1;
while(b)
{
if(b&1) res=ksc(res,a,mod);
b>>=1;
a=ksc(a,a,mod);
}
return res;
}
void extgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b)
{
d=a;
x=1;
y=0;
}
else
{
extgcd(b,a%b,d,y,x);
y-=(a/b)*x;
}
}
ll inverse(ll a,ll mod)
{
ll d,x,y;
extgcd(a,mod,d,x,y);
return d==1?(x+mod)%mod:-1;
}//拓欧求逆元
ll fminverse(ll a,ll mod)
{
return ksm(a,mod-2,mod);
}//费马小定理求逆元,前提是mod为素数,不是的话要求phi(mod)
//求出phi(mod),结果为ksm(a,phi(mod)-1,mod);
ll phi(ll x)
{
ll res=x;
for(ll i=2;i*i<=x;++i)
{
if(x%i==0)
{
res=res/i*(i-1);
while(x%i==0) x/=i;
}
}
if(x>1) res=res/x*(x-1);
return res;
}//单个筛素数,res记录结果,复杂度为根号N
//无视数组限制直接求单个的x前有多少和x互质的数
//当N为质数并且0<a<N时,a肯定存在逆元
void inversebiao(ll n,ll mod)//打表求前1-n(%mod)的逆元
{
inv[1]=1;
for(int i=2;i<=n;i++)
inv[i]=(ll)(mod-mod/i)*inv[mod%i]%mod;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
cout<<check(347)<<endl;
cout<<inverse(32489,347)<<endl;
cout<<fminverse(32489,347)<<endl;
long long d=212353;
long long e=823816093931522017;
long long k=1001733991047948000;
cout<<inverse(d,k)<<endl;
long long num=phi(k);
cout<<num<<endl;
cout<<ksm(d,num-1,k)<<endl;
return 0;
}