結果

問題 No.774 tatyamと素数大富豪
ユーザー chocoruskchocorusk
提出日時 2018-12-22 10:06:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,492 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 104,508 KB
実行使用メモリ 7,500 KB
最終ジャッジ日時 2024-04-08 22:29:27
合計ジャッジ時間 1,838 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
7,500 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 1 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 16 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 21 ms
6,548 KB
testcase_13 AC 21 ms
6,548 KB
testcase_14 AC 4 ms
6,548 KB
testcase_15 AC 2 ms
6,548 KB
testcase_16 AC 5 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 1 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

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];
	vector<ll> v;
	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;
		}
		v.push_back(p);
		if(!next_permutation(a, a+n)) break;
	}
	sort(v.begin(), v.end(), greater<ll>());
	for(int i=0; i<v.size(); i++){
	    if(is_prime(v[i])){
	        cout<<v[i]<<endl;
	        return 0;
	    }
	}
	cout<<-1<<endl;
	return 0;
}
0