結果

問題 No.2744 Power! or +1
ユーザー Series_205Series_205
提出日時 2024-04-20 18:39:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 3,000 ms
コード長 2,464 bytes
コンパイル時間 2,457 ms
コンパイル使用メモリ 212,152 KB
実行使用メモリ 14,580 KB
最終ジャッジ日時 2024-04-20 18:39:28
合計ジャッジ時間 3,963 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 321 ms
14,268 KB
testcase_01 AC 47 ms
7,552 KB
testcase_02 AC 9 ms
5,376 KB
testcase_03 AC 8 ms
5,376 KB
testcase_04 AC 25 ms
5,376 KB
testcase_05 AC 224 ms
14,580 KB
testcase_06 AC 83 ms
6,364 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 76 ms
10,804 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define FOR(i, l, r) for(ll i = l; i < r; i++)
#define rep(i, n) FOR(i, 0, n)
#define FORR(i, l, r) for(ll i = r - 1; i >= l; i--)
#define ALL(ar) begin(ar), end(ar)

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
    return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
    return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
    is >> p.first >> p.second;
    return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << ' ' << p.second;
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for(T &x : v) is >> x;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for(size_t i = 0; i < v.size(); i++) os << (i ? " " : "") << v[i];
    return os;
}
//-------------------------------------------------

void solve() {
    ll n, a, b, c;
    cin >> n >> a >> b >> c;

    vector<ll> fact(n);
    vector<bool> up(n);
    fact[0] = 1;
    FOR(i, 1, n) {
        fact[i] = fact[i - 1] * i % n;
        if(fact[i - 1] * i >= n || up[i - 1]) {
            up[i] = true;
        }
    }

    ll mn = n * a;
    vector dist(2, vector<ll>(n, mn));
    dist[0][1] = 0;
    priority_queue<tuple<ll, ll, ll>> que;
    que.emplace(0, 0, 1);
    while(!que.empty()) {
        auto [d, x, y] = que.top();
        que.pop();
        d = -d;
        if(dist[x][y] < d) continue;
        if(chmin(dist[x || (y + 1) / n][(y + 1) % n], d + a)) {
            que.emplace(-d - a, x || (y + 1) / n, (y + 1) % n);
        }
        int nx = (x || up[y]);
        if(chmin(dist[nx][fact[y]], d + c)) {
            que.emplace(-d - c, nx, fact[y]);
        }
        if(x == 1 && chmin(dist[x][0], d + c)) {
            que.emplace(-d - c, x, 0);
        }
        ll cost = b * b;
        ll ny = y * y % n;
        nx = (x || y * y >= n);
        while(true) {
            if(cost > dist[1][0]) break;
            if(chmin(dist[nx][ny], d + cost)) {
                que.emplace(-d - cost, nx, ny);
            }
            cost *= b;
            nx = (nx || ny * y >= n);
            ny = ny * y % n;
        }
    }

    cout << dist[1][0] << endl;
}

int main() {
    int t = 1;
    // cin >> t;
    while(t--) solve();
}
0