Created
October 10, 2019 17:43
-
-
Save BreadFish64/fe50eabb422f7a6397e99291d4ac7129 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cmath> | |
#include <cstdint> | |
#include <iostream> | |
// I like rust's primitives better | |
using u8 = std::uint_fast8_t; | |
using u16 = std::uint_fast16_t; | |
using u32 = std::uint_fast32_t; | |
using u64 = std::uint_fast64_t; | |
using s8 = std::int_fast8_t; | |
using s16 = std::int_fast16_t; | |
using s32 = std::int_fast32_t; | |
using s64 = std::int_fast64_t; | |
using f32 = std::float_t; | |
using f64 = std::double_t; | |
using usize = std::size_t; | |
// https://stackoverflow.com/questions/10670379/find-xor-of-all-numbers-in-a-given-range | |
constexpr u32 RangeXor(u32 a, u32 b) { | |
auto f = [](const u32 x) constexpr->u32 { | |
switch (x & 3) { | |
case 0: | |
return x; | |
case 1: | |
return 1; | |
case 2: | |
return x + 1; | |
case 3: | |
return 0; | |
} | |
// appease the compiler | |
return 0; | |
}; | |
return f(b) ^ f(a - 1); | |
} | |
int main() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
u16 t{}; | |
std::cin >> t; | |
for (u16 i = 0; i < t; ++i) { | |
u32 l{}, h{}, n{}, d1{}, d2{}; | |
std::cin >> l >> h >> n >> d1 >> d2; | |
auto findX = [n, l](const u32 idx) { return (idx - n) % l; }; | |
auto findY = [n, l](const u32 idx) { return (idx - n) / l; }; | |
struct { | |
u32 x, y, w, h; | |
} inner_rectangle{std::min(findX(d1), findX(d2)), std::min(findY(d1), findY(d2)), | |
static_cast<u32>(std::abs(static_cast<s32>(findX(d1) - findX(d2)))), | |
static_cast<u32>(std::abs(static_cast<s32>(findY(d1) - findY(d2))))}; | |
// xor the entire outer rectangle | |
u32 xor_accum = RangeXor(n, n + l * h - 1); | |
u32 first_index = inner_rectangle.y * l + inner_rectangle.x + n; | |
// xor works in reverse, so "unxor" the inner rectangle's rows | |
for (u32 j = 0; j <= inner_rectangle.h; ++j) { | |
u32 row_start = first_index + j * l; | |
xor_accum ^= RangeXor(row_start, row_start + inner_rectangle.w); | |
} | |
std::cout << xor_accum << '\n'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment