521 字
3 分钟
Spring

题面#

Alice, Bob, and Carol visit a spring to collect water. Alice visits every aa days (on days a,2a,3a,a, 2a, 3a, \dots), Bob visits every bb days (on days b,2b,3b,b, 2b, 3b, \dots), and Carol visits every cc days (on days c,2c,3c,c, 2c, 3c, \dots).

When only one person visits, they collect 66 liters of water. If multiple people visit, the water is divided equally: two people take 33 liters each, and three people take 22 liters each.

Your task is to calculate how much water Alice, Bob, and Carol collect over mm days, starting from day 11.

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The only line of each test case contains four integers aa, bb, cc, and mm (1a,b,c1061 \le a, b, c \le 10^6; 1m10171 \le m \le 10^{17}).

Output

For each test case, print three integers — the number of liters of water that Alice, Bob, and Carol collect over mm days.

思路#

先把问题从按天模拟改成按事件计数

dd 天谁会来只由整除关系决定

  • Alice 来当且仅当 ada \mid d
  • Bob 来当且仅当 bdb \mid d
  • Carol 来当且仅当 cdc \mid d

因此前 mm 天内某类事件出现次数都可写成 mx\left\lfloor \frac{m}{x} \right\rfloor

A=ma,B=mb,C=mcA=\left\lfloor \frac{m}{a} \right\rfloor,\quad B=\left\lfloor \frac{m}{b} \right\rfloor,\quad C=\left\lfloor \frac{m}{c} \right\rfloorAB=mlcm(a,b),AC=mlcm(a,c),BC=mlcm(b,c)AB=\left\lfloor \frac{m}{\mathrm{lcm}(a,b)} \right\rfloor,\quad AC=\left\lfloor \frac{m}{\mathrm{lcm}(a,c)} \right\rfloor,\quad BC=\left\lfloor \frac{m}{\mathrm{lcm}(b,c)} \right\rfloorABC=mlcm(a,b,c)ABC=\left\lfloor \frac{m}{\mathrm{lcm}(a,b,c)} \right\rfloor

这样设的原因是一个人的收益不仅取决于自己来了多少天 还取决于和别人重合了多少天

以 Alice 为例做容斥修正

  • 先按每次来都拿 66 计入 得到 6A6A
  • 与 Bob 同天时每次应从 6633 需减去 3AB3AB
  • 与 Carol 同天时同理 需减去 3AC3AC
  • 三人同天在前两步被重复扣减 实际应拿 22 只需比 6644 因此前面多减了 22 需加回 2ABC2ABC

得到

WA=6A3AB3AC+2ABCW_A = 6A - 3AB - 3AC + 2ABC

同理

WB=6B3AB3BC+2ABCW_B = 6B - 3AB - 3BC + 2ABCWC=6C3AC3BC+2ABCW_C = 6C - 3AC - 3BC + 2ABC

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
long long a, b, c, m;
cin >> a >> b >> c >> m;
long long na = m/a, nb = m/b, nc = m/c;
long long nab = m/lcm(a,b), nbc = m/lcm(b,c), nac = m/lcm(a,c), nabc = m/lcm(lcm(a, b), c);
long long wa = 6*na - 3*nab - 3*nac + 2*nabc,
wb = 6*nb - 3*nab - 3*nbc + 2*nabc,
wc = 6*nc - 3*nac - 3*nbc + 2*nabc;
cout << wa << " " << wb << " " << wc << endl;
}
}
Spring
https://github.com/posts/spring/
作者
FZSGBall
发布于
2026-04-21
许可协议
CC BY-NC-SA 4.0