ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1110. Power

Time Limit Exceed #8 (Java)
Posted by Mad_Sanek 1 Feb 2013 21:18
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static BigInteger step(int a, int b)
    {
        BigInteger temp=BigInteger.valueOf(a);
        for (int i=1; i<b; i++)
        {
            temp=temp.multiply(BigInteger.valueOf(a));
        }
        return temp;
    }

    public static void main(String[] args)
    {
        Scanner inp=new Scanner(System.in);
        int n=inp.nextInt();
        int m=inp.nextInt();
        int y=inp.nextInt();
        boolean f=false;
        for (int i=0;i<m;i++)
        {
            BigInteger x=step(i,n);
            BigInteger c=x.mod(BigInteger.valueOf(m));
            if (c.compareTo(BigInteger.valueOf(y))==0)
            {
                System.out.print(i+" ");
                f=true;
            }
        }
        if (f==false)
            System.out.print("-1");
    }
}

I don't know how to do it faster, than now. Please help me.

Edited by author 03.02.2013 15:10
Re: Time Limit Exceed #8 (Java)
Posted by Andrey Lopukhov 6 Jul 2013 15:07
you don't need to use BigInteger =)