-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephusCircular.java
More file actions
34 lines (32 loc) · 969 Bytes
/
JosephusCircular.java
File metadata and controls
34 lines (32 loc) · 969 Bytes
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
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by Arpit on 14-Dec-15.
*/
public class JosephusCircular {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t,n,p;
String temp[];
try {
t=Integer.parseInt(br.readLine());
while(t-->0){
temp=br.readLine().split("\\s");
n= Integer.parseInt(temp[0]);
p= Integer.parseInt(temp[1]);
int m= (int) ( Math.log(n)/Math.log(2));
int f= (int) Math.pow(2,m);
if (f==n)f=p;
else {
f=2*(n-f)+p;
if (f>n)f=f%n;
}
System.out.println(f);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}