結果

問題 No.2744 Power! or +1
ユーザー GOTKAKOGOTKAKO
提出日時 2024-04-20 13:46:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 3,000 ms
コード長 2,449 bytes
コンパイル時間 3,071 ms
コンパイル使用メモリ 210,360 KB
実行使用メモリ 20,564 KB
最終ジャッジ日時 2024-04-20 13:46:06
合計ジャッジ時間 4,455 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 151 ms
15,872 KB
testcase_01 AC 7 ms
7,552 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 25 ms
5,488 KB
testcase_05 AC 235 ms
20,564 KB
testcase_06 AC 41 ms
7,084 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 13 ms
9,600 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    long long N,A,B,C; cin >> N >> A >> B >> C;

    long long Limit = N;
    vector<pair<long long,bool>> fac(Limit+1,{1,false});
    for(long long i=1; i<=Limit; i++){
        fac.at(i) = fac.at(i-1);
        fac.at(i).first *= i;
        if(fac.at(i).first >= N) fac.at(i).first %= N,fac.at(i).second = true;
    }

    long long inf = 1e18;
    vector<bool> al1(N),al2(N); 
    vector<long long> M1(N,inf),M2(N,inf);
    priority_queue<tuple<long long,bool,long long>,vector<tuple<long long,bool,long long>>,greater<>> Q;
    M1.at(1) = 0; Q.push({0,false,1});
    while(Q.size()){
        auto[nowc,Over,v] = Q.top(); Q.pop();
        if(v == 0){cout << nowc << endl; break;}
        if(!Over){
            if(al1.at(v)) continue;
            al1.at(v) = true;
            if(M1.at((v+1)%N) > nowc+A) M1.at((v+1)%N) = nowc+A,Q.push({nowc+A,Over,(v+1)%N});
            long long cost = B,next = v,over = 0;
            while(cost*B <= 1e11){
                next *= v; cost *= B;
                if(next >= N) over = 1;
                next %= N;
                if(over){if(M2.at(next) > nowc+cost) M2.at(next) = nowc+cost,Q.push({nowc+cost,true,next});}
                else{if(M1.at(next) > nowc+cost) M1.at(next) = nowc+cost,Q.push({nowc+cost,false,next});}
            }
            {
                auto[next,over] = fac.at(v);
                cost = C;
                if(over){if(M2.at(next) > nowc+cost) M2.at(next) = nowc+cost,Q.push({nowc+cost,true,next});}
                else{if(M1.at(next) > nowc+cost) M1.at(next) = nowc+cost,Q.push({nowc+cost,false,next});}
            }
        }
        else{
            if(al2.at(v)) continue;
            al2.at(v) = true;
            if(M2.at((v+1)%N) > nowc+A) M2.at((v+1)%N) = nowc+A,Q.push({nowc+A,Over,(v+1)%N});
            long long cost = B,next = v,over = 1;
            while(cost*B <= 1e11){
                next *= v; cost *= B;
                if(next >= N) over = 1;
                next %= N;
                if(over){if(M2.at(next) > nowc+cost) M2.at(next) = nowc+cost,Q.push({nowc+cost,true,next});}
                else{if(M1.at(next) > nowc+cost) M1.at(next) = nowc+cost,Q.push({nowc+cost,false,next});}
            }
            {
                if(M2.at(0) > nowc+C) M2.at(0) = nowc+C,Q.push({nowc+C,true,0});
            }
        }
    }
}
0