Google-Code-Jam/pascal.cpp

39 lines
1 KiB
C++
Raw Normal View History

2020-05-12 02:01:15 +00:00
#include <bits/stdc++.h>
using namespace std;
int main() {
if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
2020-05-12 15:51:42 +00:00
int T;
cin >> T;
for (int t = 0; t < T; ++t) {
cout << "Case #" << t + 1 << ":\n";
int N;
cin >> N;
2020-05-16 13:57:25 +00:00
if (N <= 30) {
// if N <= 30 then just use naïve method
2020-05-12 15:51:42 +00:00
for (int i = 0; i < N; ++i) cout << i + 1 << " " << 1 << '\n';
}
else {
2020-05-16 13:57:25 +00:00
// first we try to make N - 30
int sum = 0, side = 0, goal = N - 30;
for (int i = 0; i < 30; ++i) {
2020-05-12 15:51:42 +00:00
cout << i + 1 << " " << (side ? i + 1 : 1) << '\n';
2020-05-17 17:59:00 +00:00
// each row sums to 2 ^ i
// check if goal has a 2 ^ i in its binary representation
2020-05-12 15:51:42 +00:00
if (goal & (1 << i)) {
// walk across the row
for (int j = 1; j <= i; ++j) cout << i + 1 << " " << (side ? i - j + 1 : j + 1) << '\n';
side = !side; // toggle the side
sum += (1 << i);
}
2020-05-12 02:01:15 +00:00
else ++sum;
}
2020-05-12 15:51:42 +00:00
2020-05-16 13:57:25 +00:00
// we've undershot so we still need to walk down until our sum is N
for (int i = 30; sum < N; ++i, ++sum) cout << i + 1 << ' ' << (side ? i + 1 : 1) << '\n';
2020-05-12 15:51:42 +00:00
}
}
2020-05-12 02:01:15 +00:00
}