結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 267 ms
19,448 KB
testcase_01 AC 7 ms
10,204 KB
testcase_02 AC 4 ms
9,952 KB
testcase_03 AC 4 ms
7,648 KB
testcase_04 AC 37 ms
8,548 KB
testcase_05 AC 423 ms
19,480 KB
testcase_06 AC 70 ms
10,956 KB
testcase_07 AC 3 ms
7,520 KB
testcase_08 AC 9 ms
11,208 KB
testcase_09 AC 2 ms
7,648 KB
testcase_10 AC 3 ms
9,824 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;

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

        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} });

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

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





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


*/
0