Cod sursa(job #1509104)
| Utilizator | Data | 23 octombrie 2015 15:10:39 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 60 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <cstdio>
using namespace std;
FILE *f = fopen ( "inversmodular.in" , "r" ) , *g = fopen ( "inversmodular.out" , "w" );
long long A , N;
long long toPower ( long long x , long long exp )
{
long long result = 1;
while ( exp )
{
if ( exp % 2 == 1 )
{
exp --;
result = ( result * x ) % N;
}
x = ( x * x ) % N;
exp /= 2;
}
return result;
}
int main()
{
//read
fscanf ( f , "%lld %lld" , &A , &N );
//compute A ^ ( N - 2 )
fprintf ( g , "%lld\n" , toPower ( A , N - 2 ) );
return 0;
}
