CF2051B Journey tj

· · 题解

思路

注意到 n \leq 10^9,于是考虑批量处理。

因为是 3 次一循环,所以只需按照 3 天处理一次,最后 3 天会走多的部分特殊处理即可。

令剩余部分为 k。

最后输出 3 天循环总数乘三再加上特殊处理的天数即可。

时间复杂度:O(1)。

Code

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long

#define PII pair<ll, ll>
#define x first
#define y second

using namespace std;
const int E = 1e6 + 5;
ll t, n, a, b, c, cnt;
inline void solve()
{
    cnt++;
    // cout << cnt << endl;
    cin >> n >> a >> b >> c;
    ll x = a + b + c;
    ll s = n / x;
    cerr << s << endl;
    if (n - s * x == 0)
        cout << s * 3 << endl;
    else
    {
        ll k = n - s * x;
        cerr << k << ' ' << s << endl;
        if (k <= a)
            cout << s * 3 + 1 << endl;
        else if (k <= a + b)
            cout << s * 3 + 2 << endl;
        else
            cout << s * 3 + 3 << endl;
    }
}
int main()
{
    cin >> t;
    while (t--)
        solve();
    return 0;
}