結果

問題 No.2744 Power! or +1
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2024-04-21 20:55:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 463 ms / 3,000 ms
コード長 3,206 bytes
コンパイル時間 2,184 ms
コンパイル使用メモリ 170,536 KB
実行使用メモリ 19,316 KB
最終ジャッジ日時 2024-04-21 20:55:55
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 311 ms
19,316 KB
testcase_01 AC 9 ms
8,704 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 38 ms
5,376 KB
testcase_05 AC 463 ms
17,636 KB
testcase_06 AC 71 ms
7,136 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 15 ms
11,520 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
ll mul(ll a, ll b) { if(a==0) return 0; if(infl/a<b) return infl; return min(infl, a*b); }
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/



int N; ll A, B, C;

ll dp[201010][2];
int vis[201010][2];

ll P[201010];
ll PM[201010];

void _main() {
    cin >> N >> A >> B >> C;

    rep(mo, 0, N) rep(isLarger, 0, 2) {
        dp[mo][isLarger] = infl;
        vis[mo][isLarger] = 0;
    }

    P[1] = PM[1] = 1;
    rep(i, 2, N) {
        P[i] = mul(P[i - 1], i);
        PM[i] = (1LL * PM[i - 1] * i) % N;
    }

    min_priority_queue<pair<ll, pair<int,int>>> que;

    dp[1][0] = 0;
    que.push({0, {1, 0}});

    while(!que.empty()) {
        auto q = que.top(); que.pop();

        ll cst = q.first;
        int mo = q.second.first;
        int isLarger = q.second.second;

        if (vis[mo][isLarger]) continue;
        vis[mo][isLarger] = 1;

        if (mo == 0) {
            cout << cst << endl;
            return;
        }

        int mo2, isLarger2;

        // Op.1
        mo2 = (mo + 1) % N;
        isLarger2 = isLarger;
        if (mo + 1 == N) isLarger2 = 1;
        if (chmin(dp[mo2][isLarger2], cst + A)) que.push({ dp[mo2][isLarger2], {mo2, isLarger2} });

        // Op.2
        mo2 = mo;
        isLarger2 = isLarger;
        ll b2 = B;
        rep(k, 2, 64) {
            if (isLarger2 == 0 && N <= mul(mo2, mo)) isLarger2 = 1;
            mo2 = (1LL * mo2 * mo) % N;
            b2 = mul(b2, B);
            if (chmin(dp[mo2][isLarger2], cst + b2)) que.push({ dp[mo2][isLarger2], {mo2, isLarger2} });
        }

        // Op.3
        mo2 = PM[mo];
        if (isLarger == 0) {
            isLarger2 = isLarger;
            if (N <= P[mo]) isLarger2 = 1;
            if (chmin(dp[mo2][isLarger2], cst + C)) que.push({ dp[mo2][isLarger2], {mo2, isLarger2} });
        } else {
            if (chmin(dp[0][1], cst + C)) que.push({ dp[0][1], {0, 1} });
        }
    }
}





/*
## 前提知識
## 解法


*/
0