New Start day 3 :2/10/2020 codechef october long CVDRUN

 question (source:https://www.codechef.com/OCT20B/problems/CVDRUN)

Covid Run

Covid-19 is spreading fast! There are N cities, numbered from 0 to (N1), arranged in a circular manner. City 0 is connected to city 11 to 2, city (N2) to city (N1), and city (N1) to city 0.

The virus is currently at city X. Each day, it jumps from its current city, to the city K to its right, i.e., from city X to the city (X+K)%N. As the virus jumps, the cities in between don't get infected. Cities once infected stay infected. You live in city YFind if it will reach your city eventually. If it will, print YES, else print NO.

Input:

  • The first line of the input consists of an integer , the number of test cases.
  • The first and only line of each test case contains four space-separated integers -  and , denoting the number of cities, the size of jumps, Covid's current city, and the city that you live in, respectively.

Output:

For each test case, in a new line, print YES if Covid shall reach your city after a finite number of days, else print NO.

Constraints

  • 1T100
  • 1N1000
  • 0X,YN1
  • 0K1000

Subtasks

  • Subtask 1 - 100% - Original constraints

Sample Input:

2
6 2 5 3
12 3 4 2

Sample Output:

YES
NO

Explanation:

  • In the first sample, Covid starts at city , then goes to city , and then from city  to city . Thus, it reaches the city that you live in.

  • In the second sample, Covid starts at city , goes to city , then , , then , , and so on. It never reaches city .



MY LOGIC:

I was trying to use conditional approach to get the answer but I don't know the reason why it always gave wrong answer. I assumed value of k into 4 categories as shown below with the conditions.


but it didn't work ..waiting for the editorial to get clearance on that.

approach that worked: I used brute force using while loop. I updated current infected city by formula cur=(cur+k)%N where initial value of cur was x.I ran the loop until cur was equal to x or y. if by any chance it's value again came to x then answer would be no.If it equals y then answer would be x.


my code:


Comments

Popular posts from this blog

Lowest common ancestor in a binary Tree

Interview

30. Substring with Concatenation of All Words