QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#274647#5259. Skills in Pillsucup-team859#TL 0ms0kbC++171.8kb2023-12-03 19:38:282023-12-03 19:38:29

Judging History

你现在查看的是最新测评结果

  • [2023-12-03 19:38:29]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-12-03 19:38:28]
  • 提交

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

output:


result: