Cod sursa(job #1113538)

Utilizator AlphaZoneRAlbert Ferencz AlphaZoneR Data 20 februarie 2014 18:06:13
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb

    #include <fstream>
    using namespace std;
    int main() {
        ifstream in("lgput.in");
        ofstream out("lgput.out");
        int x,y;
        long long int z = 1;
        long long int k = x;
        int P[50];

        in >> x >> y;

        for(int i = 0;i<50;i++){
            k *= k;
            k %= 1999999973;
            P[i] = k;
           // out << P[i] << "\n";
        }

        while (y > 1) {
            int i = 0;
            int l = x;
            while (l * l < y){
                l *= l;
                i++;
            }

            z *= P[i];
            z %= 1999999973;
            y = y - l;
        }

        if(y == 1){
            z *= x;
            z %= 1999999973;
        }

        out << z;

        in.close();
        out.close();


    }