-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChefAndCakes.java
More file actions
90 lines (71 loc) · 1.94 KB
/
ChefAndCakes.java
File metadata and controls
90 lines (71 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by hardCode on 1/26/2016.
*/
public class ChefAndCakes {
class Robot {
int index;
boolean cake;
public Robot(int index) {
this.index = index;
}
public boolean isCake() {
return cake;
}
public void setCake(boolean cake) {
this.cake = cake;
}
public void increaseIndex(int m){
index+=m;
}
public void decreaseIndex(int m){
index-=m;
}
public int getIndex() {
return index;
}
@Override
public String toString() {
return "Robot{" +
"index=" + index +
", cake=" + cake +
'}';
}
}
public static void main(String[]args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t,n,m;
String s[];
t=Integer.parseInt(br.readLine());
while (t-->0){
s=br.readLine().split("\\s");
n=Integer.parseInt(s[0]);
m=Integer.parseInt(s[1]);
new ChefAndCakes().solve(m,n);
}
}
void solve(int m,int n){
Robot[]r=new Robot[n+1];
int k=1,p,count=0;
for (int i = 1,j=m+1; i <=(n-m); i++,j++) {
r[i]=new Robot(j);
}
for (int i =n-m+1,j=1; i <=n ; i++,j++) {
r[i]=new Robot(j);
}
//System.out.println(Arrays.toString(r));
do {
r[k].setCake(true);
k=r[k].getIndex();
//System.out.print(k+" ");
count++;
}while (!r[k].isCake());
if (count==n) System.out.println("YES");
else System.out.println("NO "+count);
}
}