Cod sursa(job #1244910)

Utilizator TudorMTudor Moldovanu TudorM Data 18 octombrie 2014 13:28:26
Problema Invers modular Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include<fstream>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
int a, n, c;
int cmmdc(int a, int b)
{
    int x=a, y=n;
    while(x!=y)
    {
        if(x>y)x-=y;
        else y-=x;
    }
    return x;
}
int ecuatie(int a, int b, int c)
{
    int q, r, v0st=1, v0dr=0, vst,vdr,v1st=0, v1dr=1;
    while(b)
    {
        q=a/b;
        r=a%b;
        a=b;
        b=r;
        vst=v0st;
        vdr=v0dr;
        v0st=v1st;
        v0dr=v1dr;
        v1st=vst-q*v1st;
        v1dr=vdr-q*v1dr;
    }
    return v0st;
}
int main()
{
    f>>a>>n;
    c=cmmdc(a,n);
    g<<ecuatie(a,n,c);
    f.close();
    g.close();
    return 0;
}