跳到正文

题解

CF2051B Journey tj

_colin1112_约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
1
题目
CF2051B

版本

  1. v1当前

思路

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

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

令剩余部分为 kk。

  1. kk 只需 11 天即可走完(k≤ak \leq a):天数加一;
  2. kk 只需 22 天即可走完(k≤a+bk \leq a + b):天数加二;
  3. kk 只需 33 天即可走完(k≤a+b+ck \leq a + b + c):天数加三。

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

时间复杂度:O(1)O(1)。

Code

C++
#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;
}
原文