-
-
Save MinecraftFuns/7ead2aeaf1bb3eab34daa1befb441466 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 <bits/stdc++.h> | |
#define alive std::cout << "$LINE @" << __LINE__ << " ALIVE" << std::endl | |
#define watch(var) std::cout << "$ LINE @" << __LINE__ << " FUN @" << __FUNCTION__ \ | |
<< " VAR @" << (#var) << ": " << (var) << std::endl | |
using namespace std; | |
typedef int i32; | |
typedef unsigned int u32; | |
typedef long long i64; | |
typedef unsigned long long u64; | |
typedef double f64; | |
typedef long double f128; | |
typedef pair<int, int> pii; | |
template <typename Tp> | |
inline void read(Tp &x) | |
{ | |
x = 0; | |
bool neg = false; | |
char c = getchar(); | |
for (; !isdigit(c); c = getchar()) | |
{ | |
if (c == '-') | |
{ | |
neg = true; | |
} | |
} | |
for (; isdigit(c); c = getchar()) | |
{ | |
x = x * 10 + c - '0'; | |
} | |
if (neg) | |
{ | |
x = -x; | |
} | |
} | |
template <typename Tp> | |
inline Tp gcd(Tp n, Tp m) | |
{ | |
while (m) | |
{ | |
Tp t = n % m; | |
n = m; | |
m = t; | |
} | |
return n; | |
} | |
inline void solve() | |
{ | |
i64 a, b, c, d, p, m; | |
read(a), read(b), read(c), read(d), read(p), read(m); | |
auto calc = [&](i64 a, i64 b) -> i64 { | |
if (a < 0LL || b < 0LL) | |
{ | |
return 0LL; | |
} | |
++a, ++b; | |
i64 aD = a / p, bD = b / p, aM = a % p, bM = b % p; | |
i64 res = (p * aD) * bD; | |
res += aM * bD; | |
res += bM * aD; | |
--aM, --bM; | |
if (aM > bM) | |
{ | |
swap(aM, bM); | |
} | |
for (i64 sum = m, len = aM + bM; sum <= len; sum += p) | |
{ | |
if (sum < aM) | |
{ | |
res += sum + 1; | |
} | |
else if (aM <= sum && sum <= bM) | |
{ | |
res += aM + 1; | |
} | |
else if (sum > bM) | |
{ | |
res += len - sum + 1; | |
} | |
} | |
return res; | |
}; | |
i64 mole = calc(b, d) - calc(a - 1, d) - calc(b, c - 1) + calc(a - 1, c - 1), | |
deno = (b - a + 1) * (d - c + 1); | |
i64 div = gcd(mole, deno); | |
mole /= div, deno /= div; | |
printf("%lld/%lld\n", mole, deno); | |
} | |
int main() | |
{ | |
int T; | |
read(T); | |
for (int cas = 1; cas <= T; ++cas) | |
{ | |
printf("Case #%d: ", cas); | |
solve(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment