Cod sursa(job #2118162)

Utilizator IsacLucianIsac Lucian IsacLucian Data 30 ianuarie 2018 10:42:12
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

int a,b,mod;

int Lgput(int x,int n)
{
    int p=1;
    while(n>0)
    {
        if(n%2==1)p=1LL*p*x%mod;
        x=1LL*x*x%mod;
        n/=2;
    }
    return p;
}

int Phi(int x)
{
    int p;
    int div;
    p=x;

    if(x%2==0)
    {
        p=p/2;
        while(x%2==0)
            x/=2;
    }

    div=3;
    while(1LL * div*div<=x && x>1)
    {
        if(x%div==0)
        {
            p = (p / div) * (div - 1);
            while(x%div==0)
                x/=div;
        }
        div+=2;
    }

    if(x>1)p = (p / x) * (x - 1);

    return p;
}

int main()
{
    fin>>a>>b;

    mod=b;
    b=Phi(b);

    fout<<1LL*Lgput(a,b-1)%mod;
    return 0;
}