結果

問題 No.2744 Power! or +1
ユーザー はまやんはまやん
提出日時 2024-04-21 20:48:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 363 ms / 3,000 ms
コード長 3,179 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 174,552 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-10-13 19:12:25
合計ジャッジ時間 3,038 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

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} });
}
}
}
/*
##
##
*/
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0