Timus : 1012 (K-based Numbers. Version 2)



// http://acm.timus.ru/problem.aspx?space=1&num=1012

import java.math.BigInteger;
import java.util.Scanner;

/**
 *
 * @author shahab
 */
public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner (System.in);

        int n;
        BigInteger k, pure_nums, defaulter_num;

        while ( input.hasNext() ) {
            n = input.nextInt();
            k = input.nextBigInteger();
            k = k.subtract(BigInteger.ONE);
            pure_nums = k.multiply(k);
            defaulter_num = k;

            for ( int i = 3; i <= n; i++ ) {
                BigInteger tmp = pure_nums;
                BigInteger tmp1 = defaulter_num.multiply(k);
                pure_nums = pure_nums.multiply(k).add(tmp1);
                defaulter_num = tmp;
            }

            System.out.println (pure_nums.add(defaulter_num));
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s