Cod sursa(job #1624272)

Utilizator andrei.mardaleAndrei Mardale andrei.mardale Data 2 martie 2016 09:52:50
Problema Calcul Scor 10
Compilator java Status done
Runda Arhiva de probleme Marime 1.64 kb
//package calcul;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	
	BigInteger get_pow(int x, int n, BigInteger [] big) {
		if (n == 0){
			BigInteger b = new BigInteger("1"); 
			big[n] = b;
			return b;
		}
			
		if (n == 1){
			BigInteger b = new BigInteger(String.valueOf(x)); 
			big[n] = b;
			return b;
		}
			
		if (n % 2 == 0){
			BigInteger b;
			if (big[n/2] != null)
				b = big[n/2].multiply(big[n/2]);
			else
				b = get_pow(x, n/2, big).multiply(get_pow(x, n/2, big));
			big[n] = b;
			return  b;
		}
			
		else{
			BigInteger b;
			if (big[n/2] != null)
				b = big[n/2].multiply(big[n/2].multiply(new BigInteger(String.valueOf(x))));
			else
				b = get_pow(x, n/2, big).multiply(get_pow (x, n/2, big).multiply(new BigInteger(String.valueOf(x))));
			big[n] = b;
			return b;
		}
			
	}
	
	BigInteger computeSum (int a, int b, int c) {
		BigInteger sum = new BigInteger("0");
		BigInteger[] big = new BigInteger[b+1];
		for (int i = b; i >= 1; i--) {
			sum = sum.add(get_pow (a, i, big));
		}
		long m = (long)Math.pow(10, c);
		return sum.mod(new BigInteger(String.valueOf(m)));
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		
		Scanner scan = new Scanner (new FileInputStream("calcul.in"));
		int a, b, c;
		a = scan.nextInt();
		b = Integer.parseInt(scan.next(), 16);
		c = scan.nextInt();
		Main m = new Main ();
		
		PrintWriter pw = new PrintWriter("calcul.out");
		
		pw.println(m.computeSum(a,b,c) + "\n");
		
//		System.out.println(m.computeSum(a,b,c));
		
		scan.close();
		pw.close();
		
	}

}