Cod sursa(job #2149505)

Utilizator andreilicaAndrei Lica Eduard andreilica Data 2 martie 2018 18:08:23
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 1.15 kb
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
	static class Task {
		public final static String INPUT_FILE = "lgput.in";
		public final static String OUTPUT_FILE = "lgput.out";
		public final static long mod = 1999999973;
		int N;
		int P;

		private void readInput() {
			try {
				Scanner sc = new Scanner(new File(INPUT_FILE));
				N = sc.nextInt();
				P = sc.nextInt();
				sc.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		private void writeOutput(long result) {
			try {
				PrintWriter pw = new PrintWriter(new File(OUTPUT_FILE));
				pw.printf("%d\n", result);
				pw.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		private long fastPow(int N, int P) {
			if(P == 1)
				return N % mod;
			
			if(P % 2 == 1)
				return (N * fastPow(N, P - 1) % mod);
			else {
				long c = (fastPow(N, P/2) % mod);
				return (1L * c * c ) % mod;
			}	
		}

		public void solve() {
			readInput();
			long result = fastPow(N, P);
			writeOutput(result);
		}
	}

	public static void main(String[] args) {
		new Task().solve();
	}
}