Cod sursa(job #2034135)

Utilizator aditoma2001Toma Adrian aditoma2001 Data 7 octombrie 2017 14:56:19
Problema Invers modular Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
#include <limits>
using namespace std;

typedef unsigned long long ll;

ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

ll fi(ll n)
{
    ll prod=1,inloc=n;
    for (int i=2;i*i<=n;++i)
    {
        if (inloc%i==0)
        {
            while (inloc%i==0&&inloc!=1)
            {
                inloc/=i; prod*=i;
            }
            prod/=i;
        }
        if (inloc==1) break;
    }
    return prod*(n-1);
}

ll exp_log(ll n,ll e,ll mod)
{
    ll sol=1;
    while (e!=0)
    {
        if (e%2==1) sol*=n%mod;
        n*=n%mod;
        e/=2;
    }
    return sol%mod;
}

int main()
{
    ll a,n;
    f>>a>>n;
    g<<exp_log(a,fi(n)-1,n);
    return 0;
}