思路
注意到 ,于是考虑批量处理。
因为是 次一循环,所以只需按照 天处理一次,最后 天会走多的部分特殊处理即可。
令剩余部分为 。
- 只需 天即可走完():天数加一;
- 只需 天即可走完():天数加二;
- 只需 天即可走完():天数加三。
最后输出 天循环总数乘三再加上特殊处理的天数即可。
时间复杂度:。
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;
}