10515 - Powers Et Al.

All about problems in Volume 105. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

garbage
New poster
Posts: 19
Joined: Thu Feb 21, 2013 5:46 am

Re: 10515 - Power et al.

Post by garbage » Mon Feb 25, 2013 9:27 am

Why TLE ???

Code: Select all

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

public class Main {
    public static void main(String[] args) {
        int power=0;
        BigInteger first,second,remainder,lastDigitOfFirst,result;

        BigInteger zero = BigInteger.ZERO;
        BigInteger one = BigInteger.ONE;
        BigInteger two = BigInteger.valueOf(2);
        BigInteger three = BigInteger.valueOf(3);
        BigInteger four = BigInteger.valueOf(4);
        BigInteger ten = BigInteger.TEN;
        
        Scanner in=new Scanner(System.in);

        while(in.hasNextBigInteger())
        {
            first=in.nextBigInteger();
            second=in.nextBigInteger();

            if(first.compareTo(zero) == 0 && second.compareTo(zero) == 0)
                break;

            if(first.compareTo(zero)!=0 && second.compareTo(zero) == 0)
                System.out.println("1");

            else
            {
                lastDigitOfFirst=first.remainder(ten);
                remainder=second.remainder(four);

                if(remainder.compareTo(one) == 0)
                    power = 1;

                if(remainder.compareTo(two) == 0)
                    power = 2;

                if(remainder.compareTo(three) == 0)
                    power = 3;

                if(remainder.compareTo(zero) == 0)
                    power = 4;

                result=lastDigitOfFirst.pow(power);

                System.out.println(result.remainder(ten));
            }
        }
    }

}

garbage
New poster
Posts: 19
Joined: Thu Feb 21, 2013 5:46 am

Re: 10515 - Power et al.

Post by garbage » Mon Feb 25, 2013 9:28 am

Why TLE ???

Code: Select all

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

public class Main {
    public static void main(String[] args) {
        int power=0;
        BigInteger first,second,remainder,lastDigitOfFirst,result;

        BigInteger zero = BigInteger.ZERO;
        BigInteger one = BigInteger.ONE;
        BigInteger two = BigInteger.valueOf(2);
        BigInteger three = BigInteger.valueOf(3);
        BigInteger four = BigInteger.valueOf(4);
        BigInteger ten = BigInteger.TEN;
        
        Scanner in=new Scanner(System.in);

        while(in.hasNextBigInteger())
        {
            first=in.nextBigInteger();
            second=in.nextBigInteger();

            if(first.compareTo(zero) == 0 && second.compareTo(zero) == 0)
                break;

            if(first.compareTo(zero)!=0 && second.compareTo(zero) == 0)
                System.out.println("1");

            else
            {
                lastDigitOfFirst=first.remainder(ten);
                remainder=second.remainder(four);

                if(remainder.compareTo(one) == 0)
                    power = 1;

                if(remainder.compareTo(two) == 0)
                    power = 2;

                if(remainder.compareTo(three) == 0)
                    power = 3;

                if(remainder.compareTo(zero) == 0)
                    power = 4;

                result=lastDigitOfFirst.pow(power);

                System.out.println(result.remainder(ten));
            }
        }
    }

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10515 - Power et al.

Post by brianfry713 » Mon Feb 25, 2013 10:50 pm

Try using BufferedReader and BufferedWriter. You can solve this without using BigInteger.
http://www.algorithmist.com/index.php/UVa_10515
Check input and AC output for thousands of problems on uDebug!

uradura
New poster
Posts: 11
Joined: Thu Jan 01, 2015 10:31 am

Re: 10515 - Powers Et Al.

Post by uradura » Wed Feb 25, 2015 6:47 am


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10515 - Powers Et Al.

Post by brianfry713 » Wed Feb 25, 2015 11:23 pm

Read this thread, try solving it without using BigInteger.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 105 (10500-10599)”