Cod sursa(job #626597)

Utilizator suzanicaSuzanica Mihu suzanica Data 27 octombrie 2011 19:04:46
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <stdio.h>
#include <stdlib.h>
#define m 1999999973

int ct;
unsigned long long x,n,rez;

void nr_cif()
{
    unsigned long long temp = n;
    ct = 0;
    while (temp)
    {
        temp=temp>>1;
        ct++;
    }
}

void log_pow()
{
    int i;
    nr_cif();
    rez = 1;
    ct = ct - 1;
    for (i=ct;i>=0;i--)
    {
        rez = ((rez%m)*(rez%m))%m;
        if (n & (1<<i))
            rez = (rez * (x%m))%m;
    }
}

int main()
{
    freopen("lgput.in","r",stdin);
    freopen("lgput.out","w",stdout);
    scanf("%lld %lld",&x,&n);
    log_pow();
    printf("%lld",rez);
    return 0;
}