結果

問題 No.2744 Power! or +1
ユーザー cho435cho435
提出日時 2024-04-20 03:50:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,234 bytes
コンパイル時間 2,177 ms
コンパイル使用メモリ 204,616 KB
実行使用メモリ 39,452 KB
最終ジャッジ日時 2024-04-20 03:51:06
合計ジャッジ時間 7,357 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 5 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 1,060 ms
38,712 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 7 ms
6,528 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 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++)

ll mpow(ll x,ll n,ll m){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%m;
		x=x*x%m;
		n>>=1;
	}
	return res;
}

int main(){
	ll n,a,b,c;
	cin>>n>>a>>b>>c;
	vector<ll> fac(n+1,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;
		}		
	}
	ll mxc=n*a;
	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==n) break;
		if(v==0){
			pq.push({sc+c,n});
			continue;
		}
		if(cs.at(v+1)==1e18){
			pq.push({sc+a,v+1});
		}
		{
			int k=1;
			ll bf=-1;
			while(1){
				int nx=mpow(v,k,n);
				ll pw=mpow(b,k,mxc);
				if(pw<bf) break;
				if(sc+pw>=mxc) break;
				bf=pw;
				if(cs.at(nx)==1e18){
					pq.push({sc+pw,nx});
				}
				k++;
			}
		}
		{
			int nx=fac.at(v);
			if(nx==0) nx=n;
			if(cs.at(nx)==1e18){
				pq.push({sc+c,nx});
			}
			if(ud<=v&&cs.at(0)==1e18){
				pq.push({sc+c,0});
			}
		}
	}
	cout<<cs.at(n)<<endl;
}
0