Cod sursa(job #2862867)

Utilizator TudosieRazvanTudosie Marius-Razvan TudosieRazvan Data 5 martie 2022 22:47:19
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <climits>
#include <unordered_map>

#define NMAX 2000003

using namespace std;

vector<int> sol;
char cuv1[NMAX],cuv2[NMAX];

FILE *fin,*fout;

int pow2(int num,int p)
{
    int sol=1;
    int inter=num;
    while(p>0)
    {
        if(p%2==1)
        {
            sol*=inter;
            p--;
        }
        inter=inter*inter;
        p=p/2;
    }
    return sol;
}

int phi(int num)
{
    int val=num;
    int sol=num;
    bool ePrim=true;
    for(int i=2; i*i<=val; i++)
    {
        if(num%i==0)
        {
            ePrim=false;
            sol=sol*(i-1)/i;
            while(num%i==0)
            {
                num/=i;
            }
        }
    }
    if(ePrim)
    {
        return val-1;
    }
    return sol;
}

int inv_mod(int a,int b)
{
    return pow2(a,phi(b)-1);
}

int main()
{
    fin=fopen("inversmodular.in","r");
    fout=fopen("inversmodular.out","w");
    
    int a,b;
    fscanf(fin,"%d %d",&a,&b);
    int sol=inv_mod(a,b);
    fprintf(fout,"%d",sol);
    return 0;
}