Cod sursa(job #1389731)

Utilizator witselWitsel Andrei witsel Data 16 martie 2015 16:25:28
Problema Invers modular Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
using namespace std;
vector<long long> vec;
long long a, n, sol, m ;

long long exp(long long x,long long p)
{
    if(p<0) return exp(1/x,-p);
    if(p==0) return 1;
    if(p==1) return x;
    if(p%2==0) return (exp((x*x)%n,p/2))%n;
    if(p%2) return (x%n*exp((x*x)%n,(p-1)/2))%n;
}
void descompunere(long long x)
{
    int i=2;
    while(x!=1)
    {
        if(x%i == 0)
        {
            vec.push_back(i);
            while(x%i==0)
                x/=i;
        }
        i++;
    }
}
int main()
{
    ifstream fin("inversmodular.in");
    ofstream fout("inversmodular.out");
    fin>>a>>n;
    long long  m=sol=n;
    descompunere(n);
    for(int i=1;i<n;++i)
        for(int j=0;j<vec.size();++i)
        if(i%vec[j]==0)
        j=vec.size(),sol--;
    sol=exp(a,sol-1);
    fout << sol;
    return 0;
}