結果

問題 No.2744 Power! or +1
ユーザー 👑 NachiaNachia
提出日時 2024-04-20 20:50:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,607 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 88,136 KB
実行使用メモリ 16,308 KB
最終ジャッジ日時 2024-04-20 20:50:24
合計ジャッジ時間 3,496 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 620 ms
16,308 KB
testcase_01 AC 70 ms
8,672 KB
testcase_02 AC 11 ms
5,376 KB
testcase_03 AC 16 ms
5,376 KB
testcase_04 AC 49 ms
5,376 KB
testcase_05 AC 471 ms
15,096 KB
testcase_06 WA -
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 154 ms
12,084 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <array>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
#define repr(i,n) for(int i=int(n)-1; i>=0; i--)
const i64 INF = 1001001001001001001;
const char* yn(bool x){ return x ? "Yes" : "No"; }
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
template<typename A> using nega_queue = std::priority_queue<A,std::vector<A>,std::greater<A>>;
using namespace std;

void testcase(){
    i64 N, A, B, C; cin >> N >> A >> B >> C;
    i64 K = 0; {
        i64 f = 1;
        while(INF / B >= f){
            K++;
            f *= B;
        }
    }
    i64 Nfactor = 0; {
        i64 n = N;
        for(i64 p=2; p*p<=n; p++) if(n%p == 0){
            i64 cnt = 0;
            while(n%p == 0){ n /= p; cnt++; }
            i64 targetq = 0;
            for(i64 q=1; q<=cnt; q++){
                i64 qq = q;
                i64 qc = 0;
                while(qq){ qc += qq; qq /= p; }
                if(qc >= cnt){ targetq = q; break; }
            }
            chmax(Nfactor, p * targetq);
        }
        chmax(Nfactor, n);
    }

    nega_queue<pair<i64,i64>> que;
    vector<i64> dist(N*2, INF);
    i64 ans = A * (N-1);
    auto upd = [&](i64 v, i64 d){
        if(dist[v] > d){
            dist[v] = d;
            que.push({ d, v });
        }
    };
    vector<i64> fact(N);
    fact[0] = 1;
    auto xmod = [&](i64 a){
        if(a >= N) return (a-N) % N + N;
        return a;
    };
    for(i64 i=1; i<N; i++){
        fact[i] = xmod(fact[i-1] * i);
    }
    upd(1, 0);
    while(que.size()){
        auto [d, v] = que.top(); que.pop();
        if(dist[v] != d) continue;
        {
            if(v % N == N-1){
                chmin(ans, d + A);
            } else {
                upd(v+1, d + A);
            }
        }
        if(v < N){
            if(fact[v] % N == 0){
                chmin(ans, d + C);
            } else {
                upd(fact[v], d + C);
            }
        } else {
            chmin(ans, d + C);
        }
        {
            i64 w = v, dd = B;
            for(i64 k=2; k<=K; k++){
                dd *= B; w = xmod(w*v);
                upd(w, d + dd);
            }
        }
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    #ifdef NACHIA
    int T; cin >> T; for(int t=0; t<T; T!=++t?(cout<<'\n'),0:0)
    #endif
    testcase();
    return 0;
}
0