Cod sursa(job #1676382)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 5 aprilie 2016 21:26:38
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#define DIM 25

using namespace std;

ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
int a,n;
char buff[DIM];
int poz = 0;

void Read(int &a)
{
    while(!isdigit(buff[poz]))
        if(++poz==DIM)
        in.read(buff,DIM),poz = 0;
    a = 0;
    while(isdigit(buff[poz]))
    {
        a = a*10 + buff[poz]-'0';
        if(++poz==DIM)
            in.read(buff,DIM),poz = 0;
    }
}

int euler(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x = 1;
        y = 0;
        return a;
    }
    else
    {
        int d,x0,y0;
        d = euler(b,a%b,x0,y0);
        x = y0;
        y = x0 - (a/b)* y0;
        return d;
    }
}

int main()
{
    Read(a);
    Read(n);
    in.close();
    int x,y;
    int d = euler(a,n,x,y);
    x*=d;
    y*=d;
    while(x<0)
        x+=n;
    out<<x<<"\n";
    out.close();
    return 0;
}