結果

問題 No.2744 Power! or +1
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2024-04-20 23:22:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 850 ms / 3,000 ms
コード長 3,443 bytes
コンパイル時間 1,608 ms
コンパイル使用メモリ 172,368 KB
実行使用メモリ 19,448 KB
最終ジャッジ日時 2024-04-20 23:23:00
合計ジャッジ時間 6,115 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 850 ms
19,448 KB
testcase_01 AC 444 ms
10,812 KB
testcase_02 AC 74 ms
6,944 KB
testcase_03 AC 96 ms
5,376 KB
testcase_04 AC 62 ms
5,376 KB
testcase_05 AC 643 ms
17,640 KB
testcase_06 AC 248 ms
8,160 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 710 ms
15,224 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 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(isUpper, 0, 2) {
        dp[mo][isUpper] = infl;
        vis[mo][isUpper] = 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 isUpper = q.second.second;

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

        //printf("%d %d\n", mo, isUpper);

        int mo2, isUpper2;

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

        //printf("%d %d - 1\n", mo, isUpper);

        // Op.2
        mo2 = mo;
        isUpper2 = isUpper;
        ll b2 = B;
        rep(k, 2, 64) {
            //printf("%d %d - 2 - %d\n", mo, isUpper, k);
            if (isUpper2 == 0 && N <= mul(mo2, mo)) isUpper2 = 1;
            mo2 = (1LL * mo2 * mo) % N;
            //printf("%d %d - 2 - %d - A\n", mo, isUpper, k);
            //printf("%d %d - 2 - %d - B\n", mo, isUpper, k);
            b2 = mul(b2, B);
            //printf("%d %d - 2 - %d - C\n", mo, isUpper, k);
            if (chmin(dp[mo2][isUpper2], cst + b2)) que.push({ dp[mo2][isUpper2], {mo2, isUpper2} });
        }

        //printf("%d %d - 2\n", mo, isUpper);

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

    ll ans = dp[0][1];
    rep(mo, 1, N) chmin(ans, dp[mo][1] + C);
    cout << ans << endl;
}





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


*/
0