結果

問題 No.2744 Power! or +1
ユーザー mealymealy
提出日時 2024-04-21 17:43:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,068 ms / 3,000 ms
コード長 2,205 bytes
コンパイル時間 5,997 ms
コンパイル使用メモリ 336,656 KB
実行使用メモリ 40,864 KB
最終ジャッジ日時 2024-04-21 17:43:23
合計ジャッジ時間 10,224 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,068 ms
40,864 KB
testcase_01 AC 50 ms
10,700 KB
testcase_02 AC 10 ms
5,376 KB
testcase_03 AC 14 ms
5,420 KB
testcase_04 AC 63 ms
8,020 KB
testcase_05 AC 647 ms
39,908 KB
testcase_06 AC 218 ms
13,272 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 116 ms
16,412 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using mint = atcoder::modint998244353;
//using mint = atcoder::modint1000000007;
using namespace atcoder;
using namespace std;

using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vm = vector<mint>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vpi = vector<pair<int,int>>;
using vpl = vector<pair<ll,ll>>;
#define rep(i,n) for (ll i = 0; i < n; i++)
#define replr(i,l,r) for (ll i = l; i < r; i++)
const int inf =1e9;
const ll infl = 4e18;

template <typename T>
bool chmax(T &a, const T& b) {if (a < b) {a = b;  return true;}return false;}
template <typename T>
bool chmin(T &a, const T& b) {if (a > b) {a = b;  return true;}return false;}
template <typename T>
T max(vector<T> &v) {T m = 0;for (auto x : v) chmax(m, x);return m;}
template <typename T>
T min(vector<T> &v) {T m = inf;for (auto x : v) chmin(m, x);return m;}
template <typename T>
void print(vector<T> &v){for(auto x : v){cout<<x<<' ';}cout<<'\n';}
/*memo

*/


int main(){
    ll N,A,B,C; cin >> N >> A >> B >> C;
    vl dp(2*N,infl);
    dp[2]=A;
    ll p=1;
    replr(i,2,N-1){
        ll n=i;
        ll cost=B;
        while (cost*B<=N*A){
            n*=i;
            if (n>=2*N){
                n%=N;
                n+=N;
            }
            cost*=B;
            chmin(dp[n],dp[i]+cost);
        }
        p*=i;
        if (p>=2*N){
            p%=N;
            p+=N;
        }
        chmin(dp[p],dp[i]+C);
        chmin(dp[i+1],dp[i]+A);
    }
    vl dist(N,infl);
    priority_queue<pl,vector<pl>,greater<pl>> pq;
    rep(i,N){
        pq.push({dp[i+N],i});
    }
    while (!pq.empty()){
        auto [d,n]=pq.top();
        pq.pop();
        if (dist[n]<=d) continue;
        dist[n]=d;
        if (dist[(n+1)%N]>d+A){
            pq.push({d+A,(n+1)%N});
        }
        ll cost=B;
        ll nn=n;
        while (cost*B<=N*A){
            nn*=n;
            nn%=N;
            cost*=B;
            if (dist[nn]>d+cost){
                pq.push({d+cost,nn});
            }
        }
        if (dist[0]>d+C){
            pq.push({d+C,0});
        }
    }
    cout << dist[0] << '\n';
}
0