Cod sursa(job #2234064)

Utilizator cory1211Corina cory1211 Data 25 august 2018 11:45:58
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <vector>

const long mod = 1999999973;

using namespace std;

class Task {
public:
	void solve() {
		read_input();
		print_output(fast_pow(base, exponent, mod));
	}

private:
	long long base, exponent;

	void read_input() {
		ifstream fin("lgput.in");
		fin >> base >> exponent;
		fin.close();
	}

	int fast_pow(long base, long exponent, long mod) {

     if(exponent == 0) return 1;
     else if(exponent == 1) return base % mod;
     else if(exponent % 2  == 0) return fast_pow((base * base % mod), exponent / 2, mod) % mod;
     else if(exponent % 2 != 0) return base * fast_pow(base * base % mod, (exponent - 1) / 2, mod) % mod;

		return 0;
	}

	void print_output(int result) {
		ofstream fout("lgput.out");
		fout << result;
		fout.close();
	}
};

int main() {
	Task task;
	task.solve();
	return 0;
}