-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervalgame.java
More file actions
62 lines (52 loc) · 1.43 KB
/
Copy pathIntervalgame.java
File metadata and controls
62 lines (52 loc) · 1.43 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
import java.util.*;
public class Intervalgame{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int x1=sc.nextInt();
int x2=sc.nextInt();
long minw=Long.MAX_VALUE;
int optv=-1;
for(int v=0;v<x1;v++){
long ways=calcw(v,x2);
if(ways<minw){
minw=ways;
optv=v;
}
if(minw==0)break;
}
int l1=optv+1;
int r1=x1;
System.out.println(l1+" "+r1);
}
}
private static long calcw(int v,int x2){
if(x2-1-v<0)return 0;
int maxk=(x2-1-v)/2;
if(maxk<0)return 0;
long countv=countk(v,maxk);
return countv * (1L <<Integer.bitCount(v));
}
private static long countk(int v, int maxK){
if (maxK <0) return 0;
int[] zeros =new int[21];
for (int i =1; i <=20; i++)
zeros[i] =zeros[i-1]+(((v>> (i-1)) & 1) ==0 ? 1 : 0);
long ans =0;
for (int i =19; i>=0; i--){
int bV =(v>> i) & 1;
int bM =(maxK>> i) & 1;
if (bV ==1){
if (bM ==1){
ans +=(1L <<zeros[i]);
return ans;
}
} else{
if (bM ==1)ans +=(1L <<zeros[i]);
}
}
ans++;
return ans;
}
}