Cod sursa(job #617457)

Utilizator cristianalex81Cristian Alexandru cristianalex81 Data 14 octombrie 2011 21:33:51
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.59 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;
}