Cod sursa(job #2967098)

Utilizator bkoncsagKoncsag Beata bkoncsag Data 19 ianuarie 2023 01:22:24
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <stdio.h>
#include <string.h>

const int n_max = 10001; // Definim numarul maxim de cifre al numerelor
const int m = 1999999973;

long long exp_by_squaring_iterative (long long x, long long n) {
    long long y = 1;
    while (n > 0) {
        if (n  % 2 != 0) {
            y = y*x;
            n = n - 1;
        }
        x = x*x;
        n = n/2;
    }
    return y;
}


int main()
{
	unsigned int i, n, p;
	long long a, sol = 1;

	freopen("lgput.in","r",stdin);
	freopen("lgput.out","w",stdout);
	scanf("%d %d", &n, &p);

	sol = exp_by_squaring_iterative(n,p);

	printf("%lld\n", sol);