結果

問題 No.774 tatyamと素数大富豪
ユーザー ahe100ahe100
提出日時 2019-03-19 19:32:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,347 ms / 2,000 ms
コード長 1,242 bytes
コンパイル時間 1,796 ms
コンパイル使用メモリ 167,908 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 05:23:59
合計ジャッジ時間 6,937 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1,347 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 788 ms
4,352 KB
testcase_10 AC 313 ms
4,348 KB
testcase_11 AC 16 ms
4,348 KB
testcase_12 AC 8 ms
4,348 KB
testcase_13 AC 9 ms
4,352 KB
testcase_14 AC 24 ms
4,352 KB
testcase_15 AC 24 ms
4,352 KB
testcase_16 AC 942 ms
4,348 KB
testcase_17 AC 138 ms
4,352 KB
testcase_18 AC 23 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef pair<ll, ll> mp;

/*

*/

// ミラーラビン
ll modpow(__int128_t a, ll n, ll mo) {
	__int128_t r=1;
	a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}
bool MillerRabin(ll v,int loop=10) {
	ll d=v-1;
	int s=0,t;
	if(v<=1) return false;
	if(v==2) return true;
	if(v%2==0) return false;
	while(d%2==0) d/=2,s++;
	
	rep(i,loop) {
		ll a=abs(1LL*rand()*rand()+rand())%(v-2)+1;
		ll r=modpow(a,d,v);
		if(r==1 || r==v-1) continue;
		t=s;
		rep(j,s) {
			r=modpow(r,2,v);
			if(r==v-1){
				t=j;
				break;
			}
		}
		if(t==s) return false;
	}
	return true;
}

ll n,a[10],ans=-1;

int main(void){
	cin>>n;
	rep(i,n)cin>>a[i];
	vector<int> v(n);
	iota(v.begin(), v.end(), 0);
	do{
		ll dig=1,t=0;
		for(auto x : v){
			t+=dig*a[x];
			if(a[x]>=10){
				dig*=100;
			}else{
				dig*=10;
			}
		};
		if(ans<t && MillerRabin(t,30)){
			ans=t;
		}
	}while( next_permutation(v.begin(), v.end()) );
	cout<<ans<<endl;
	return 0;
}
0