Cod sursa(job #3236073)

Utilizator AndreasAntoniuAntoniu Andreas AndreasAntoniu Data 25 iunie 2024 22:54:51
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <stdio.h>

#define M 1999999973

long long exp_log(long long y, long long x, int n)
{
    if (!n)
    {
        return y;
    }
    if (n & 1)
    {
        return exp_log((y * x) % M, (x * x) % M, (n - 1) / 2);
    }
    else
    {
        return exp_log(y % M, (x * x) % M, n / 2);
    }
}

int main()
{
    FILE *filein = fopen("lgput.in", "rb");
    FILE *fileout = fopen("lgput.out", "wb");
    long long n, p;
    fscanf(filein, "%lld", &n);
    fscanf(filein, "%lld", &p);

    fclose(filein);

    fprintf(fileout, "%lld\n", exp_log(1, n, p));

    fclose(fileout);
    return 0;
}