QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#274647 | #5259. Skills in Pills | ucup-team859# | TL | 0ms | 0kb | C++17 | 1.8kb | 2023-12-03 19:38:28 | 2023-12-03 19:38:29 |
answer
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
using ull = unsigned long long;
using ll = long long;
using namespace std;
inline ll Get(int a, int b) {
int i = 1;
while (true) {
ll pos = 1LL * i * a;
if ((pos - 1) % b == 0) {
return pos;
}
i++;
}
return -1;
}
int main() {
#ifdef HOME
ifstream cin("input.in");
ofstream cout("output.out");
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, a, b;
cin >> a >> b >> n;
if (a == b) {
int answer = 0;
for (int i = a; i <= n; i += a) {
answer++;
}
for (int i = a - 1; i <= n; i += a) {
answer++;
}
cout << answer << "\n";
return 0;
}
ll ab = Get(a, b);
ll ba = Get(b, a);
ll C = 1LL * a * b / __gcd(a, b);
if (C > n) {
cout << n / a + n / b << "\n";
return 0;
}
vector<int> cntA(n + 1), cntB(n + 1);
for (int i = n; i >= 1; i--) {
cntA[i] = 1 + (i + a <= n ? cntA[i + a] : 0);
cntB[i] = 1 + (i + b <= n ? cntB[i + b] : 0);
}
vector<int> dp(n + 1, n + 1);
dp[n - 1] = 2;
for (int i = n - 2; i >= 1; i--) {
ll pos = i + ab;
if (pos > n) {
dp[i] = min(dp[i], cntA[i] + cntB[i + 1]);
} else {
dp[i] = min(dp[i], dp[pos - 1] + cntA[i] - cntA[pos] + cntB[i + 1] - cntB[pos]);
}
pos = i + ba;
if (pos > n) {
dp[i] = min(dp[i], cntB[i] + cntA[i + 1]);
} else {
dp[i] = min(dp[i], dp[pos - 1] + cntB[i] - cntB[pos] + cntA[i + 1] - cntA[pos]);
}
}
cout << dp[C - 1] + a + b - 2 << "\n";
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
3 9 20