結果

問題 No.2744 Power! or +1
ユーザー Nzt3Nzt3
提出日時 2024-04-13 15:29:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 624 ms / 3,000 ms
コード長 1,718 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 202,832 KB
実行使用メモリ 21,952 KB
最終ジャッジ日時 2024-04-17 18:32:49
合計ジャッジ時間 4,556 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 624 ms
21,952 KB
testcase_01 AC 29 ms
7,680 KB
testcase_02 AC 6 ms
5,376 KB
testcase_03 AC 13 ms
5,376 KB
testcase_04 AC 47 ms
5,376 KB
testcase_05 AC 451 ms
14,484 KB
testcase_06 AC 160 ms
7,128 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 92 ms
12,864 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;

int main() {
  ll N, A, B, C;
  cin >> N >> A >> B >> C;
  vector fac(N, array<ll, 2>());
  fac[0] = {1, 0};
  for (int i = 1; i < N; i++) {
    fac[i][1] = fac[i - 1][1];
    fac[i][0] = fac[i - 1][0] * i % N;
    if (fac[i - 1][0] * i >= N) fac[i][1] = 1;
  }

  vector dist(N, array<ll, 2>({(ll)1e18, (ll)1e18}));
  dist[1][0] = 0;
  priority_queue<pair<ll, array<ll, 2>>, vector<pair<ll, array<ll, 2>>>,
                 greater<pair<ll, array<ll, 2>>>>
      pq;
  pq.push({0, {1, 0}});
  ll mCOST = (N - 1) * A;
  while (pq.size()) {
    auto [d, v] = pq.top();
    pq.pop();

    // 操作1
    if (v[0] + 1 == N) {
      if (dist[(v[0] + 1) % N][1] > d + A) {
        dist[(v[0] + 1) % N][1] = d + A;
        pq.push({d + A, {(v[0] + 1) % N, 1}});
      }
    } else {
      if (dist[(v[0] + 1) % N][v[1]] > d + A) {
        dist[(v[0] + 1) % N][v[1]] = d + A;
        pq.push({d + A, {(v[0] + 1) % N, v[1]}});
      }
    }

    // 操作2
    {
      ll b = B, v2 = v[0];
      int ov_N = v[1];
      while (b < mCOST) {
        if (dist[v2][ov_N] > d + b) {
          dist[v2][ov_N] = d + b;
          pq.push({d + b, {v2, ov_N}});
        }
        b *= B;
        v2 *= v[0];
        if (v2 >= N) {
          v2 %= N;
          ov_N = 1;
        }
      }
    }

    // 操作3
    if (v[1]) {
      if (dist[0][1] > d + C) {
        dist[0][1] = d + C;
        pq.push({d + C, {0, 1}});
      }
    } else {
      if (dist[fac[v[0]][0]][fac[v[0]][1]] > d + C) {
        dist[fac[v[0]][0]][fac[v[0]][1]] = d + C;
        pq.push({d + C, {fac[v[0]][0], fac[v[0]][1]}});
      }
    }
  }
  cout << min(dist[0][0], dist[0][1]) << '\n';
}
0