Cod sursa(job #2661500)

Utilizator NoobUserNameMatei Otniel NoobUserName Data 22 octombrie 2020 09:52:00
Problema Invers modular Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>

using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");
int n,ct;
int fi(int n)
{
    int d=2;
    long long ans=n;
    while(n>1 and d*d<=n)
    {
        while(n%d==0)
            ans=1LL*ans*(d-1)/d%ct, n/=d;
        d++;
    }
    if(n!=1)
        ans=1LL*ans*(n-1)/n;
    return ans;
}
long long put(int a, int b)
{
    a%=ct;
    if(b==0)
        return 1;
    if(b%2)
        return 1LL*a*put(a,b-1)%ct;
    long long p=put(a,b/2);
    return 1LL*p*p%ct;
}
int main()
{
    int a;
    cin>>a>>n;
    ct=n;
    cout<<put(a,fi(n)-1)%ct;
}