#37717: 避開 overflow


proglohas@gmail.com (david)

School : No School
ID : 221623
IP address : [114.42.157.234]
Last Login :
2024-06-06 16:00:39
a810. 1. 倍數關係 -- 102學年度高雄市資訊學科能力競賽複賽 | From: [122.117.95.179] | Post Date : 2023-10-01 22:32

ll lcm(ll m, ll n)
{
    ll g = __gcd(m, n);
    return (m / g) * n;
}

r -= b / (lcm(x, y));

這裡有個呼叫 lcm 時,如果 x, y 太大又互質時會 overflow
可以改個地方避開這個問題

ll lcm(ll m, ll n, ll y)
{
    ll g = __gcd(m, n);
    ll mg = m / g;
    if (y / n < mg) return y + 1;
    return (m / g) * n;
}

r -= b / (lcm(x, y, b));

 
ZeroJudge Forum