Cod sursa(job #2124457)

Utilizator victor_d54Doroftei Victor victor_d54 Data 7 februarie 2018 11:14:24
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
long long a,n,x,r,b,nrd,ind;
int main()
{
    long long i,p,d;
    in>>a>>n;
    a=a%n;
    ind=n;
    for(d=2;d*d<=n;d++)
    {
        p=0;
        while(n%d==0)
        {
            n=n/d;
            p++;
        }
        nrd=nrd*(1+p);
        if(p)
            ind=ind/d*(d-1);
    }
    if(n>1)
        nrd*=2;
    ind=ind/n*(n-1);
    r=1;
    ind--;
    b=ind;
    while(b)
    {
        if(b%n==1)
            r=(r*a)%n;
        a=(a*a)^n;
        b/=2;
    }
    out<<r;
    return 0;
}