-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC_XOR_factorization.cpp
More file actions
76 lines (61 loc) · 1.49 KB
/
C_XOR_factorization.cpp
File metadata and controls
76 lines (61 loc) · 1.49 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
#include <bits/stdc++.h>
using namespace std;
// First you make it work, then you can always make it beautiful
/*
n = 30, k = 4
optimal 15 23 27 29 not 30 30 23 9
xor req = 1 1 1 1 0
1st 2nd 3rd 4th 5th <= iterations
1 1 1 0 1 => 29 ptr = 3
1 1 0 1 1 => 27 ptr = 2
1 0 1 1 1 => 23 ptr = 1
0 1 1 1 1 => 15 ptr = 0
try to make loose numbers more and more
if(n >> i & 1) {
since k is even => k-1 place pe 1 hona chayie
i will skip that index from which loose numbers increases
}
else {
even 1 hone chayie so loose index par rkh skta hu only
}
*/
void solve() {
int n, k;
cin >> n >> k;
vector<int> ans(k);
if(k % 2 == 0) {
int ptr = 0;
for(int i=30;i>=0;i--) {
if(n >> i & 1) {
int leave = (ptr < k) ? ptr : 0;
for(int j=0;j<k;j++) {
if(j == leave) continue;
ans[j] |= (1 << i);
}
if(ptr < k) ptr++;
}
else {
int even = (ptr / 2) * 2;
for(int j=0;j<even;j++) {
ans[j] |= (1 << i);
}
}
}
}
else {
ans.assign(k, n);
}
for(auto i : ans) cout << i << " ";
cout << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _;
cin >> _;
while (_-->0) {
solve();
}
return 0;
}