-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKMXOR.java
More file actions
82 lines (54 loc) · 1.57 KB
/
KMXOR.java
File metadata and controls
82 lines (54 loc) · 1.57 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
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class Ideone{
//https://www.codechef.com/problems/KMXOR
public static Scanner scn = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception{
int T = scn.nextInt();
while(T-- > 0){
int N = scn.nextInt();
int K = scn.nextInt();
//getting highest perfect power of 2
int highest = (int)Math.pow(2, (int)Math.floor(Math.log(K)/Math.log(2)));
if(N == 1){
System.out.println(K);
} else if(K == 1){
for(int i = 1 ; i <= N ; i++)
System.out.print("1 ");
System.out.println();
} else if(K == 2){
System.out.print("2 ");
for(int i = 1 ; i <= N-1 ; i++)
System.out.print("1 ");
System.out.println();
} else if(K == 3){
if(N == 2){
System.out.println("1 2");
continue;
}else if(N % 2 == 0){
System.out.print("1 2 ");
N -= 2;
}else{
System.out.print("3 ");
N--;
}
while(N-- > 0)
System.out.print("1 ");
System.out.println();
} else {
if(N%2 == 0){
System.out.print("" + highest + " " + (highest - 1) + " ");
N-=2;
} else {
System.out.print("" + highest + " " + (highest - 2) + " " + 1 + " ");
N-=3;
}
while(N-- > 0)
System.out.print("1 ");
System.out.println();
}
}
}
}