Pagini recente » Cod sursa (job #398516) | Cod sursa (job #1222313) | Cod sursa (job #1391663) | Cod sursa (job #2807926) | Cod sursa (job #2112792)
#include <fstream>
#define ll long long
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
long long n, a, phi, inv;
ll get_phi(ll n){
ll x = n;
ll d = 2;
ll p = 0;
while(n > 1){
if(!(n % d)){
x /= d;
x *= (d - 1);
}
while(!(n % d))
n /= d;
++ d;
}
return x;
}
ll log_pow(ll x, ll pow){
ll nr = 1;
while(pow > 1){
if(pow % 2){
nr = (nr * x) % n;
-- pow;
}
x = (x * x) % n;
pow /= 2;
}
return (x * nr) % n;
}
int main()
{
f>>a>>n;
phi = get_phi(n);
inv = log_pow(a, phi - 1);
g<<inv;
return 0;
}