A King's problem

Once again, the great and mighty King of Swadia, absolute ruler of his kingdom, is bored. In such moments, the King tends to annoy his advisers with bunch of hard and unnecessary problems. For today, King made quite a nice problem. He will give two number, first A A , then B B , and he wants to know two last digits of number A B A^{B} .

Of course, the King will give more such pairs and he wants a solution to all of them. The first man who will accomplish this task, will be given a lot of gold, mead and some other privileges.

However, a smart man named Billy already solved this, so he gets all the privileges. But if You correctly solve this task, you will earn certain amount of brilliant points.


Input:

Input is done through file "King.txt" . First number represents how many questions will be. Question is asked with two numbers, first number A A , then B B . Answer to such question is A B m o d 100 A^{B} \mod 100 . Find the sum of all such answers.


Some details and guarantees:

  • In each question, numbers will be taken from [ 0 , 2 30 ] [ 0, 2^{30} ] .
  • There will be no questions which will ask for 0 0 0^{0}
  • File can be downloaded from here , as well.

Example:

1 1 15 = 41772481694156 51 ; 13 4 5 = 432040034 24 ; 7 10 = 2824752 49 11^{15} = 41772481694156\underline{51}; \space 134^{5} = 432040034\underline{24}; \space 7^{10} = 2824752\underline{49}

51 + 24 + 49 = 124 51 + 24 + 49 = 124


Questions:

Questions related to this problem can be asked here .


The answer is 3714210.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Levent B
Jan 17, 2017

When confronted with a problem similar to this, I find it very helpful to put the code aside at first and just focus on the mathematical aspects. I used knowledge of exponents in order to solve this problem. First, I determined which digits in integer number A A determined the last two digits of A B A^B , or A B m o d 100 A^B mod{100} . After a little bit of math, I determined that only the last two digits of number A A affected the last two digits of A B A^B .

The idea behind this was that any integer number can be re-written as the sum of its components. For instance, 347 347 can be rewritten as 300 + 47 300 + 47 , and we know that 34 7 2 347^2 is the same as ( 300 + 47 ) 2 (300 + 47)^2 . Since 300 300 has two zeros as its last two digits, we know that multiplying it by any integer will result in a number whose last two digits are also zeros. This shows us that when raising 347 347 to a power, any numbers before the 47 47 are irrelevant when determining the last two digits of the result. This idea can be expanded and applied to any integer and any digit.

Now that I know this, I only need to work with the last to digits of A A to get my answer. But where do I go from here? The B value is still terribly large.

Through laws of exponents, we know that A B = A C A C A^B = A^C * A^ C for C = B / 2 C = B/2 since 2 C = B 2C = B and A C A C = A C + C + A 2 C A^C * A^C = A^{C + C} + A^{2C} . If B B is odd, we can easily account for this by decreasing B by one to make it even and then accounting for this later by multiplying A C A C A^C * A^C by A A (this adds one to the exponent.) We've now finished all of the mathematical aspects of this problem. Now we just have to put it into code.

Please ask any questions if any of my explanation is not clear, this is my first time writing an explanation. My program uses a recursive method to find the last two digits of A B A^B . note: Make sure to include import java.util.*; at the top of your program.

public static int solveProblem(int A, int B)
{
    if(B == 0 || B == 1)
        return (int)Math.pow(A,B) % 100;

    //Figure out the last two digits.
    int lastTwoDigits = A % 100;
    int currentLastTwo = lastTwoDigits;

    int desiredPower = B;

    //If power is odd, temporarily make it even.
    if(desiredPower % 2 != 0)
        desiredPower--;

    //Recursively find the last two digits of A to half the desired power.
    int answer = solveProblem(lastTwoDigits, desiredPower / 2);

    //Raise the answer to the desired power.
    answer *= answer;

    //If the desired power was altered, account for this. 
    if(desiredPower == B - 1)
        answer *= lastTwoDigits;

    return answer % 100; 

}

public static void main(String[] args)
{
   long startTime = System.nanoTime();
   Scanner keyboard = new Scanner(System.in);

   int numberOfProblems = keyboard.nextInt();

   int sum = 0;

   for(int i = 1; i<= numberOfProblems; i++)
   {
       int a = keyboard.nextInt();
       int b = keyboard.nextInt();

       sum += solveProblem(a,b);
   }
   long endTime = System.nanoTime();

   System.out.println("Your sum is " + sum);
   System.out.println("Time to complete was " + ((endTime - startTime) / 1000000));

}

Nice. Java's BigInteger class can do it all in one function though (.modPow()). I was recently doing a problem that boils down to a harder version of this problem, with inputs as big as 10^(10^6). http://codeforces.com/contest/17/problem/D.

A solution can be seen here: http://codeforces.com/blog/entry/451

Alex Li - 4 years ago
Kumar Edward
Nov 30, 2017
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def mod(x,y):
    if y<3:
        return (x**y)%100
    return ((mod(x,y//2)**2)*(x**(y%2)))%100
def solve(input_list):
    s=0
    for i in range(0,len(input_list),2):
        b,c=input_list[i:i+2]
        s+=mod(b%100,c)
    return s

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...