Pagini recente » Istoria paginii runda/moisil2009-5-8/clasament | Cod sursa (job #690180) | Cod sursa (job #1454551) | Cod sursa (job #758548) | Cod sursa (job #2586429)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <utility>
#include <cstring>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
#define ull long long int
int mod(ull a, ull b, ull modulo ){
return ((a%modulo)*(b%modulo))%modulo;
}
int gcd(ull a, ull b)
{
return (b==0)?a:gcd(b, a%b);
}
int totient(ull n)
{
int tot =0;
for(int i =1 ; i < n ; i++ )
{
if (gcd(i,n) == 1)
{
tot++;
}
}
return tot;
}
int main ( )
{
ull a,n;
in>>a>>n;
ull powertot = n-1;
ull modulo = n;
ull base=a,res=1;
while(powertot)
{
if(powertot&1)
res= mod(res,base, modulo);
base = mod(base,base, modulo);
powertot >>=1;
}
out<<res;
return 0;
}