Cod sursa(job #3368965)

Utilizator georgiemanea georege caharisitan georgie Data 16 octombrie 2026 18:07:30
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a, n;
int calc(int x)
{
    int cr=x;
    for(int i=2; i*i<=x; i++)
    {
        if(x%i==0)
        {
            while(x%i==0)
                x/=i;
            cr=(cr/i)*(i-1);
        }
    }
    if(x!=1)
        cr=cr/x*(x-1);
    return cr;
}
int put(int x, int y)
{
    int p=1;
    while(y)
    {
        if(y%2==1)
            p=(1LL*p*x)%n;
        x=(1LL*x*x)%n;
        y/=2;
    }
    return p;
}
int main()
{
    fin >> a >> n;
    int phi=calc(n);
    fout << put(a, phi-1);
    return 0;
}