Pagini recente » Cod sursa (job #308229) | Cod sursa (job #2338186) | Cod sursa (job #1368595) | Cod sursa (job #2920606)
#include<bits/stdc++.h>
using namespace std;
string numeFisier="inversmodular";
ifstream fin(numeFisier+".in"); ofstream fout(numeFisier+".out");
#define cin fin
#define cout fout
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int a, n;
int phi(int x){
int sq=sqrt(x); sq++; sq=min(sq, x);
int y=x;
int res=1;
for(int i=2; i<=sq; i++){
if( (y%i)==0 ){
int pow=1;
while( (y%i)==0){
y/=i;
pow*=i;
}
pow-=pow/i;
res*=pow;
}
}
if(y>1){
res*=(y-1);
}
return res;
}
int exp(int x, int e, int mod){
if(e==1){
return x%mod;
}
if(e==0){
return 1;
}
return (e%2)?( (x*exp( (x*x)%mod, e/2, mod ))%mod ): ( exp( (x*x)%mod, e/2, mod ) );
}
int32_t main(){
INIT
cin>>a>>n;
cout<<exp( a, phi(n)-1, n);
return 0;
}