Cod sursa(job #1837240)

Utilizator medicinedoctoralexandru medicinedoctor Data 29 decembrie 2016 12:33:44
Problema Invers modular Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>

using namespace std;

ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");

int gcd(int x, int y)
{
    if (!y) return x;
    return gcd(y,x % y);
}

long long something(long long x, long long y)
{
    if (gcd(x,y)!=1) return -1;
    if (x==1 || x==y-1) return x;
    long long q=(x-1)*y+1;
    while (q % x!=0) q-=y;
    return q/x;
}

int main()
{
    long long a,n,x;
    cin >> a >> n;
    for (int c,i=1; i<a; i++)
    {
        c=something(i,n);
        if (c==a)
        {
            cout << i ;
            return 0;
        }
    }
    x=(a-1)*n+1;
    while (x % a !=0) x-=n;
    cout << something(a,n);
    return 0;
}