-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathB.cpp
More file actions
executable file
·45 lines (39 loc) · 838 Bytes
/
B.cpp
File metadata and controls
executable file
·45 lines (39 loc) · 838 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
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
void pascal_walk(int N) {
bool left = true;
vector<pair<int, int>> walk;
int r, cnt = 0, max_r = min(30, N);
N -= max_r;
// construct using binary representation of N - 30
for (r = 1; r <= max_r; r++, N >>= 1) {
walk.emplace_back(r, left ? 1 : r);
if (N % 2 == 0)
continue;
for (int k = 2; k <= r; k++)
walk.emplace_back(r, left ? k : r - k + 1);
cnt++;
left = !left;
}
// add required number of 1s
while (cnt--) {
walk.emplace_back(r, left ? 1 : r);
r++;
}
for (pair<int, int>& pos: walk)
cout << pos.first << " " << pos.second << endl;
}
int main() {
int T;
cin >> T;
for (int x = 1; x <= T; x++) {
int N;
cin >> N;
cout << "Case #" << x << ": " << endl;
pascal_walk(N);
}
return 0;
}