結果

問題 No.2744 Power! or +1
ユーザー Nzt3Nzt3
提出日時 2024-04-13 15:32:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 322 ms / 3,000 ms
コード長 1,482 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 203,664 KB
実行使用メモリ 17,432 KB
最終ジャッジ日時 2024-04-17 18:32:59
合計ジャッジ時間 3,379 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 322 ms
17,432 KB
testcase_01 AC 26 ms
7,040 KB
testcase_02 AC 5 ms
6,944 KB
testcase_03 AC 8 ms
6,948 KB
testcase_04 AC 24 ms
6,944 KB
testcase_05 AC 241 ms
12,140 KB
testcase_06 AC 94 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 65 ms
10,140 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
  ll N,A,B,C;
  cin>>N>>A>>B>>C;
  vector fac(N,1ll),bigN(N,0ll);
  for(int i=1;i<N;i++){
    bigN[i]=bigN[i-1];
    fac[i]=fac[i-1]*i%N;
    if(fac[i-1]*i>=N)bigN[i]=1;
  }
  vector dist(N+1,(ll)1e18);
  priority_queue<array<ll,2>,vector<array<ll,2>>,greater<array<ll,2>>>pq;
  dist[1]=0;
  pq.push({0,1});
  while(pq.size()){
    auto [d,v]=pq.top();
    pq.pop();
    if(d>dist[v])continue;
    if(v==N){
      // 操作3
      if(dist[0]>d+C){
        dist[0]=d+C;
        pq.push({d+C,0});
      }
    }else{
      // 操作1
      if(v+1>=N&&dist[N]>d+A){
        dist[N]=d+A;
        pq.push({d+A,N});
      }
      if(dist[(v+1)%N]>d+A){
        dist[(v+1)%N]=d+A;
        pq.push({d+A,(v+1)%N});
      }

      // 操作2
      {
        ll b=B,v2=v;
        bool f=0;
        while(b<A*(N-1)){
          if(v2>=N){
            f=1;
            v2%=N;
          }
          if(f){
            if(dist[N]>d+b){
              dist[N]=d+b;
              pq.push({d+b,N});
            }
            v2%=N;
          }
          if(dist[v2]>d+b){
            dist[v2]=d+b;
            pq.push({d+b,v2});
          }
          v2*=v;
          b*=B;
        }
      }
      // 操作3
      if(dist[fac[v]]>d+C){
        dist[fac[v]]=d+C;
        pq.push({d+C,fac[v]});
      }
      if(bigN[v]&&dist[N]>d+C){
        dist[N]=d+C;
        pq.push({d+C,N});
      }
    }
  }
  cout<<dist[0]<<'\n';
}
0