Cod sursa(job #2702848)

Utilizator AlexZeuVasile Alexandru AlexZeu Data 6 februarie 2021 07:36:17
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.48 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("lgput.in");
ofstream fout("lgput.out");
int putere(int a, int b, int c) {
  if (b == 0){
    return 1 % c;
  }
  if (b == 1) {
    return a % c;
  }
  if (b % 2 == 0){
    return ((putere(a, b / 2, c) % c) * (putere(a, b / 2, c) % c)) % c;
  }
  return ((a % c) * (putere(a, b - 1, c))) % c;
}

int main() {
    int N, P;
    fin >> N >> P;
    fout << putere(N, P, 1999999973);
    return 0;
}