## 10515 - Powers Et Al.

Moderator: Board moderators

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

### Re: 10515 - Power et al.

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.

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.

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!

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

### Re: 10515 - Powers Et Al.

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