結果

問題 No.774 tatyamと素数大富豪
ユーザー chocorusk
提出日時 2018-12-22 09:11:22
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1,240 ms / 2,000 ms
コード長 1,352 bytes
コンパイル時間 1,229 ms
コンパイル使用メモリ 98,612 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-01 13:23:21
合計ジャッジ時間 5,155 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll powmod(__int128_t a, ll k, ll m){
	__int128_t ap=a, ans=1;
	while(k){
		if(k&1){
			ans*=ap;
			ans%=m;
		}
		k>>=1;
		ap=ap*ap%m;
	}
	return ans;
}
random_device rnd;
mt19937_64 mt(rnd());
bool is_prime(ll n){
    if(n<=1) return false;
	if(n==2) return true;
	if(!(n&1)) return false;
	ll d=n-1;
	int k=0;
	while(!(d&1)){
		d>>=1;
		k++;
	}
	uniform_int_distribution<ll> rndn(2, n-1);
	for(int i=0; i<10; i++){
		ll a=rndn(mt);
		bool comp=1;
		ll ap=powmod(a, d, n);
		if(ap==1 || ap==n-1) continue;
		for(int r=1; r<k; r++){
			ap=powmod(ap, 2, n);
			if(ap==n-1){
				comp=0;
				break;
			}
		}
		if(comp) return false;
	}
	return true;
}
int main()
{
	int n; cin>>n;
	ll a[9];
	for(int i=0; i<n; i++) cin>>a[i];
	ll ans=-1;
	while(1){
		ll p10=1, p=0;
		for(int i=0; i<n; i++){
			p+=(p10*a[i]);
			if(a[i]>=10) p10*=100;
			else p10*=10;
		}
		if(is_prime(p)) ans=max(ans, p);
		if(!next_permutation(a, a+n)) break;
	}
	cout<<ans<<endl;
	return 0;
}
0