Cod sursa(job #3152443)

Utilizator RaduCalisovCalisovRadu RaduCalisov Data 25 septembrie 2023 10:12:01
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <fstream>

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

int main()
{
    int a,n;
    cin>>a>>n;
    bool prim = true;
    int x=2;
    if(n%2==0)
    prim=false;
    while(x*x<=n){
        x++;
        if(n%x==0)
        prim=false;
    }
    
    if(prim==true){
       int p=1,orgn=n;
       n-=2;
       while(n){
           if(n%2==1)
           p=(p*a)%orgn;
        a=(a*a)%orgn;
        n/=2;
       }
      cout<<p;
    }
}