QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#202773 | #5259. Skills in Pills | ucup-team288# | TL | 0ms | 0kb | C++20 | 1.8kb | 2023-10-06 13:23:16 | 2023-10-06 13:23:16 |
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ll = long long;
#define pb emplace_back
#define X first
#define Y second
#define AI(i) begin(i), end(i)
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true);}
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true);}
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l++ << " \n"[l==r]; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, X, Y;
cin >> X >> Y >> n;
if (X == Y) {
if (n % X == X - 1) {
cout << n / X * 2 + 1 << '\n';
} else {
cout << n / X * 2 << '\n';
}
return 0;
}
i64 C = lcm<i64>(X, Y);
if (n < C) {
cout << n / X + n / Y << '\n';
return 0;
}
vector<int> nxt(2);
for (int t : {0, 1}) {
int x = t == 0 ? X : Y;
int y = t == 0 ? Y : X;
int cur = 0;
while ((cur + 1) % x != 0) {
cur += y;
}
nxt[t] = cur;
}
constexpr int INF = 1e9;
vector dp(n + 1, vector<int>(2, INF));
dp[C][0] = dp[C][1] = C / X + C / Y;
int ans = INF;
for (int i = 1; i <= n; i++) {
for (int j : {0, 1}) {
if (dp[i][j] == INF) {
continue;
}
int ni = i + nxt[j];
int tx = i - (j == 0);
int ty = i - (j == 1);
if (ni > n) {
ans = min(ans, dp[i][j] + (n - tx) / X + (n - ty) / Y);
} else {
int cost = (ni - tx) / X + (ni - ty) / Y;
for (auto &val : dp[ni]) {
val = min(val, dp[i][j] + cost);
}
}
}
}
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 9 20