OJUMPS - Editorial

PROBLEM LINK:

Practice
Contest

Author: Dmytro Berezin
Tester: Sergey Kulik
Editorialist: Lalit Kundu

DIFFICULTY:

CAKEWALK

PREREQUISITES:

AD-HOC

PROBLEM:

Chef is at x=0.
1-jump: he will move from x -> x + 1
2-jump: he will move from x -> x + 2
3-jump: he will move from x -> x + 3
He will perform a lot of jumps in such a sequence: 1-jump, 2-jump, 3-jump, 1-jump, 2-jump, 3-jump, 1-jump, and so on.
Given an integer 0 ≤ a ≤ 1018, find will he ever arrive at a.

QUICK EXPLANATION:

In one sequence of 1-jump, 2-jump and 3-jump, he moves from x -> x + 6. So, if intermediate jumps are removed for a minute, x will always be a multiple of 6. Now, if we consider intermediate jumps, we will also consider points of form 6*k - 3, 6*k - 5.

Therefore,

a=input()    
if a%6==0 || a%6==1 || a%6==3:    
     print "yes"    
else:
     print "no"     

Complexity: O(1)

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution
Setter’s solution

1 Like

@admins: still waiting for the answer why there is compilation error for my JavaScript code - http://www.codechef.com/viewsolution/3813082

It works fine on IdeOne - http://ideone.com/yc3MFR

#include
using namespace std;
int main()
{
long a;
cout<<“enter value of a=”;
cin>>a;
if(((a%6)==0)||((a%6)==1)||((a%6)==3))
cout<<“yes”<<endl;
else
cout<<“no”<<endl;

    system("pause");
return 0;

}

Can someone tell me why my solution is coming as a wrong answer.
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

public static void main(String[] args) throws IOException {
        PrintWriter printWriter = new PrintWriter(System.out);
        double destination  = new Scanner(System.in).nextDouble();
        if (destination == 0) {
            printWriter.println("yes");
            
        } else {
            if (((destination + 5) % 6) == 0 || destination % 6 == 0 || ((destination+3) % 6) == 0) {
                printWriter.println("yes");
                printWriter.flush();
            } else {
                printWriter.println("no");
                printWriter.flush();
            }
        }
        printWriter.close();
}

}

Tip: One has to use the right size for the integer (long long in c++) in order to fit 18 digits.

https://www.codechef.com/viewsolution/18791814

I used the same logic. why still it gives WA?

@vivekshah1801 it took me 5 minutes to find the bug in your code. The bug is that the problem does not have any test cases!! You just have to take the number and show the output. I took care of this when I coded my solution but don’t know why it took so much time to find it in your code, lol.

https://www.codechef.com/viewsolution/20257191

Don’t know why I am getting a TLE… it’s working fine for me. I am using divisibility tests and char to int conversion. (I don’t want to use long long int.)

you don’t have to print “Enter the value of a”.

I am assuming that you are asking why your code isn’t working!

The series being formed is: 0 1 3 6 7 9 12 13 15 18 19 21 24 25 27 30 and so on… (the numbers in bold are generated due to 1-Jump)

So, I came up with the logic that if a is divisible by 3 (non-bolded numbers) or if (a-1) or (a+2) is divisible by 3 then it’s a yes otherwise no.

if(a%3 == 0)
    out.println("yes");
else if(((a+2)%3 == 0) || (a-1)%3 == 0)
   out.println("yes");
else
   out.println("no");

But I’m getting WA. Can someone pls explain why this logic is failing?

use long data type to declare ‘a’