結果

問題 No.2744 Power! or +1
ユーザー cho435cho435
提出日時 2024-04-20 05:08:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 3,000 ms
コード長 1,242 bytes
コンパイル時間 2,272 ms
コンパイル使用メモリ 204,428 KB
実行使用メモリ 40,248 KB
最終ジャッジ日時 2024-04-20 05:08:09
合計ジャッジ時間 3,760 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 391 ms
40,248 KB
testcase_01 AC 5 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 41 ms
8,660 KB
testcase_05 AC 357 ms
39,812 KB
testcase_06 AC 56 ms
8,748 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 7 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)

bool mck(ll x,ll y,ll m){
	double dx=x,dy=y;
	dx*=dy;
	return dx>(double)m;
}

int main(){
	ll n,a,b,c;
	cin>>n>>a>>b>>c;
	vector<ll> fac(n,1);
	ll ud=-1;
	for(ll i=2;i<n;i++){
		if(ud==-1){
			fac.at(i)=fac.at(i-1)*i;
			if(fac.at(i)>=n){
				ud=i;
				fac.at(i)%=n;
			}
		}else{
			fac.at(i)=fac.at(i-1)*i%n;
		}		
	}
	vector<ll> cs(n+1,1e18);
	priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
	pq.push({0,1});
	while(!pq.empty()){
		auto[sc,v]=pq.top();
		pq.pop();
		if(cs.at(v)!=1e18) continue;
		cs.at(v)=sc;
		if(v==0) break;
		if(v==n){
			pq.push({sc+c,0});
			continue;
		}
		if(cs.at((v+1)%n)==1e18){
			pq.push({sc+a,(v+1)%n});
		}
		{
			ll nx=v;
			ll pw=b;
			int flg=0;
			for(int k=2;1;k++){
				nx=(nx*v);
				if(nx>=n){
					nx%=n;
					flg=1;
				}
				if(mck(pw,b,a*n)) break;
				pw*=b;
				if(cs.at(nx)==1e18){
					pq.push({sc+pw,nx});
				}
				if(flg&&cs.at(n)==1e18){
					pq.push({sc+pw,n});
				}
			}
		}
		{
			int nx=fac.at(v);
			if(cs.at(nx)==1e18){
				pq.push({sc+c,nx});
			}
			if(ud<=v&&cs.at(n)==1e18){
				pq.push({sc+c,n});
			}
		}
	}
	cout<<cs.at(0)<<endl;
}
0