pages:
  • 1
  • 2
AMiR SAYS

You can use BigInteger for large values! But it is a bit slow! Although I believe it is not a problem here!

Try double if BigInteger wasn't fast enugh!

MehrdadSComputer SAYS

Actually i used BigInteger before but as you said the problem is not here, the problem is with this line

myArray = new String[powerK];

when input is 17 and bigger and so powerk is 3power17 and bigger, it shows error, i think array can't have large index as big as this one, do you have any suggestion for it?

MehrdadSComputer SAYS

in java size of array atmost can be 2 billion, so we can't use array, i wonder how other guys solved this problem with java?!:)

AMiR SAYS

MehrdadSComputer said:

in java size of array atmost can be 2 billion, so we can't use array, i wonder how other guys solved this problem with java?!:)

It is not needed an array with that large size!

You can solve this problem easier, just think about it! If you didn't come up with the right solution, I will post my sulution here!

MehrdadSComputer SAYS

i changed my approach, this is my new code, this time i have problem with time "time limit exceed":

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
private static boolean flag;
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long a=sc.nextLong();long b=sc.nextLong();
    while(b!=0)
    {
    long power= (long) Math.pow(3, a);
    flag=true;
    dash(b,power,1,power);
    if(flag) System.out.print("Yes\n");
    else System.out.print("No\n");
    a=sc.nextLong();b=sc.nextLong();
    }     
}

static void dash(long b, long power, long first, long last) {
    if (power != 1) {
        space(b, (power / 3) + first, first + (2 * power / 3) - 1);
        if(flag==true)
        {
            if(((power+1)/2)<(b)) dash(b, power / 3, first + (2 * power / 3), last);
            else dash(b, power / 3, first, (power) / 3 + first - 1);
        }
    }
}

static void space(long b, long first, long last) {
    for (long t = first; t <= last; t++) {
        if (t == b) {
            flag = false;
        }
    }
}

}

AMiR SAYS

In every step, if the index%3 equals to 1, it will be deleted! think about it!