Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Forget the actual calendar days. You only need to count how many days each possible group of visitors appears.
A person with period visits exactly times in the first days.
Two people with periods and meet on multiples of . All three meet on multiples of the LCM of all three periods.
For Alice, days where she is alone are .
For a person with period and the other periods , the total simplifies to .
Research summary: I checked the official Codeforces editorial at https://codeforces.com/blog/entry/152150, the problem page at https://codeforces.com/contest/2204/problem/C, and Maspy's Educational CF Round 188 notes at https://maspypy.com/educational-codeforces-round-188, which link an accepted submission for 2204C. The shared intended approach is subset counting with LCMs. The supplied statement is still the source of truth, so the derivation below comes from the statement itself.
The trap is simulation. can be , so looping over days is dead on arrival. We need arithmetic counts.
Fix one person with visiting period . Let the other two periods be and . Define:
Here counts all days this person visits. counts days they also meet the person with period , including triple-meeting days. Same for . counts days all three visit.
By inclusion-exclusion, days where only this fixed person visits are:
Days where they share with exactly one other person are:
Days where all three visit are:
The water amounts are liters when alone, liters when sharing with one person, and liters when sharing with two people. So the total for this fixed person is:
Now simplify it:
That is the whole problem. Apply this formula three times: once for Alice as , once for Bob as , and once for Carol as .
Why this is correct: every day a fixed person visits falls into exactly one of three categories: alone, with exactly one other person, or with both other people. The LCM counts exactly the days where multiple periodic visits coincide. Inclusion-exclusion removes the double-counting of triple days. Then the formula assigns exactly the water amount from the statement to each category. No vibes, no hand-waving, no calendar cosplay.
Edge cases:
long long: totals are at most , and LCMs of numbers up to fit below . Still, using __int128 during multiplication is the clean no-drama move.Each test case uses a constant number of gcd/lcm computations, so the complexity is time and memory, where is the maximum period value.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll lcm_safe(ll x, ll y) {
return (ll)((__int128)x / std::gcd(x, y) * y);
}
ll water_for(ll x, ll y, ll z, ll m) {
ll xy = lcm_safe(x, y);
ll xz = lcm_safe(x, z);
ll xyz = lcm_safe(xy, z);
return 6 * (m / x) - 3 * (m / xy) - 3 * (m / xz) + 2 * (m / xyz);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll a, b, c, m;
cin >> a >> b >> c >> m;
cout << water_for(a, b, c, m) << ' '
<< water_for(b, a, c, m) << ' '
<< water_for(c, a, b, m) << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll lcm_safe(ll x, ll y) {
return (ll)((__int128)x / std::gcd(x, y) * y);
}
ll water_for(ll x, ll y, ll z, ll m) {
ll xy = lcm_safe(x, y);
ll xz = lcm_safe(x, z);
ll xyz = lcm_safe(xy, z);
return 6 * (m / x) - 3 * (m / xy) - 3 * (m / xz) + 2 * (m / xyz);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll a, b, c, m;
cin >> a >> b >> c >> m;
cout << water_for(a, b, c, m) << ' '
<< water_for(b, a, c, m) << ' '
<< water_for(c, a, b, m) << '\n';
}
}