結果

問題 No.774 tatyamと素数大富豪
ユーザー kriiikriii
提出日時 2018-12-25 16:19:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,382 bytes
コンパイル時間 578 ms
コンパイル使用メモリ 58,672 KB
実行使用メモリ 30,120 KB
最終ジャッジ日時 2024-04-08 22:32:33
合計ジャッジ時間 6,976 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
using lint = long long;

lint gcd(lint a, lint b)
{
	return b ? gcd(b,a%b) : a;
}

namespace miller_rabin{
	lint mul(lint x, lint y, lint mod){ return (__int128) x * y % mod; }
	lint ipow(lint x, lint y, lint p){
		lint ret = 1, piv = x % p;
		while(y){
			if(y&1) ret = mul(ret, piv, p);
			piv = mul(piv, piv, p);
			y >>= 1;
		}
		return ret;
	}
	bool miller_rabin(lint x, lint a){
		if(x % a == 0) return 0;
		lint d = x - 1;
		while(1){
			lint tmp = ipow(a, d, x);
			if(d&1) return (tmp != 1 && tmp != x-1);
			else if(tmp == x-1) return 0;
			d >>= 1;
		}
	}
	bool isprime(lint x){
		for(auto &i : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}){
			if(x == i) return 1;
			if(x > 40 && miller_rabin(x, i)) return 0;
		}
		if(x <= 40) return 0;
		return 1;
	}
}

set<long long> dup;
bool chk[10]; int N,A[10];
long long ans = -1;
void go(int a, long long v)
{
	if (a < N){
		for (int i=0;i<N;i++) if (!chk[i]){
			chk[i] = 1;
			long long nv = v;
			if (A[i] < 10) nv = nv * 10 + A[i];
			else nv = nv * 100 + A[i];
			go(a+1,nv);
			chk[i] = 0;
		}
	}
	else{
		if (!dup.count(v)){
			dup.insert(v);
			if (miller_rabin::isprime(v)) ans = max(ans,v);
		}
	}
}

int main()
{
	scanf ("%d",&N);
	for (int i=0;i<N;i++) scanf ("%d",&A[i]);
	go(0,0);

	printf ("%lld\n",ans);
	return 0;
}
0