pages:
  • 1
  • 2
mojdehsaadati SAYS

bool isOk(long j ,int i) { if(i <= 1) { if(i == 0) return true; else if(j == 0 || j == 2) return true; return false;
} else { if((long)pow(3,i-1) < j && j < (long)2*pow(3,i-1) ) return false; else if((long)pow(3,i-1) <= j ) return isOk((long)pow(3,i)-j-1, i-1 ); else return isOk( j , i-1 ); }
}

int main() { int i; long j; while(cin>>i>>j) {

if(i == 0 && j == 0 )
    break;
if(i< 0 || j < 0  )  
    {cout<<"No"<<endl;continue;}
if(j >= pow(3,i) || j == 0)
    {cout<<"Yes"<<endl;continue;}
if(!isOk(j-1,i))
    cout<<"No"<<endl;
else
    cout<<"Yes"<<endl;

} return 0; }

AhUrA SAYS

nabayad int begiri az bazeye int khareje !!

AMiR SAYS

Use long long instead of int.

P.S. Please post your code in code format. You just have to put a tab or 4 spaces before each line. Please read the markdown editor help on the top of the editor. You can also read it's wikipedia page.

mojdehsaadati SAYS

AMiR said:

Use long long instead of int.

P.S. Please post your code in code format. You just have to put a tab or 4 spaces before each line. Please read the markdown editor help on the top of the editor. You can also read it's [wikipedia page][1].

[1]: http://en.wikipedia.org/wiki/Markdown

i == tavan j == adad va tavan < 30 banabar in nabayad moshkeli ijad beshe ba in gereftanesh va hamchenin ke man taghiresh dadam bazam wrong khordam!

AMiR SAYS

The output specification mentioned:

Each test case is given in a line containing two numbers k (less than 30) and i (at most 3^k)

According to this, i can be 3^30 which is too large to fit in integer type. You should use long long (in your variable definition and type castings).

Your solution may have some other problems. I would suggest you to post your newest code here again.

mojdehsaadati SAYS

i in problem is j in my code!

AMiR said:

The output specification mentioned:

Each test case is given in a line containing two numbers k (less than 30) and i (at most 3^k)

According to this, i can be 3^30 which is too large to fit in integer type. You should use long long (in your variable definition and type castings).

Your solution may have some other problems. I would suggest you to post your newest code here again.

mojdehsaadati SAYS
bool isOk(long j ,long long i)
{
if(i <= 1)
    {
    if(i == 0)
    return true;
    else if(j == 0 || j == 2)
        return true;
    return false;   
    }
else 
{
if((long)pow(3,i-1) < j && j < (long)2*pow(3,i-1) )
    return false;
else 
    if((long)pow(3,i-1) <= j )
        return isOk((long)pow(3,i)-j-1, i-1 );
    else
        return isOk( j , i-1 );
}       
}

 int main()
{
long i;
long j;
while(cin>>i>>j)
{

if(i == 0 && j == 0 )
    break;
if(i< 0 || j < 0  )  
    {cout<<"No"<<endl;continue;}
if(j >= pow(3,i) || j == 0)
    {cout<<"Yes"<<endl;continue;}
if(!isOk(j-1,i))
    cout<<"No"<<endl;
else
    cout<<"Yes"<<endl;
}
 return 0;
}    
AMiR SAYS

You can't use int to save large values!

As you may know, long and int are the same and both using 32 bits to store values. Use long long for every variable! You also should cast the pow function's return value to long long.

Another problem is pow returns in double which is not accurate enough for large values. I would suggest that write your own power function that uses long long to calculate powers of 3, or use powl function that returns in long double which is more accurate.

The seccond approuch can be implemented by adding this: #define pow powl.

P.S. This problem can be solved simpler. Think about it. :)

mojdehsaadati SAYS

thank you my program accepted.

AMiR said:

You can't use int to save large values!

As you may know, long and int are the same and both using 32 bits to store values. Use long long for every variable! You also should cast the pow function's return value to long long.

Another problem is pow returns in double which is not accurate enough for large values. I would suggest that write your own power function that uses long long to calculate powers of 3, or use powl function that returns in long double which is more accurate.

The seccond approuch can be implemented by adding this: #define pow powl.

P.S. This problem can be solved simpler. Think about it. :)

MehrdadSComputer SAYS

for me, it shows runtime error, i can't use Long long in java, is there equal type for it? my code:

import java.util.Scanner;

public class Main {

private static Integer k, index;
private static String line;
private static String[] lineArray = new String[2];
private static Scanner input = new Scanner(System.in);
private static Integer powerK;
private static Integer plus, first, last, length;
private static String[] myArray;

public static void main(String[] args) {
    line = input.nextLine();
    lineArray = line.split(" ");
    k = Integer.parseInt(lineArray[0]);
    index = Integer.parseInt(lineArray[1]);
    while (!line.equals("0 0")) {
        powerK = 1;
        for (int j = 0; j < k; j++) {
            powerK = powerK * 3;
        }
        myArray = new String[powerK];
        for (int p = 0; p < powerK; p++) {
            myArray[p] = "-";
        }
        act(powerK, 0, powerK - 1);
        //making input ready for process
        if (myArray[index-1].equals("-")) {
            System.out.print("Yes");
        } else {
            System.out.print("No");
        }
        System.out.print("\n");
        line = input.nextLine();
        lineArray = line.split(" ");
        k = Integer.parseInt(lineArray[0]);
        index = Integer.parseInt(lineArray[1]);
    }
}

static void act(int powerKK, int first, int last) {
    if (powerKK != 1) {
        act(powerKK / 3, first, (powerKK) / 3 + first - 1);
        act(powerKK / 3, first + (2 * powerKK / 3), last);
        clean((powerKK / 3) + first, first + (2 * powerKK / 3) - 1);
    }
}

static void clean(int first, int last) {
    for (int t = first; t <= last; t++) {
        myArray[t] = " ";
    }
}

}