結果

問題 No.774 tatyamと素数大富豪
ユーザー 37zigen37zigen
提出日時 2020-04-20 03:54:01
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,107 bytes
コンパイル時間 2,363 ms
コンパイル使用メモリ 78,764 KB
実行使用メモリ 61,240 KB
最終ジャッジ日時 2024-04-16 00:30:41
合計ジャッジ時間 22,371 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 558 ms
50,908 KB
testcase_01 AC 119 ms
40,184 KB
testcase_02 TLE -
testcase_03 AC 211 ms
43,136 KB
testcase_04 AC 142 ms
41,008 KB
testcase_05 AC 223 ms
43,648 KB
testcase_06 AC 130 ms
41,452 KB
testcase_07 AC 127 ms
41,288 KB
testcase_08 AC 130 ms
41,196 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 1,268 ms
60,072 KB
testcase_12 AC 1,039 ms
57,820 KB
testcase_13 AC 592 ms
51,528 KB
testcase_14 AC 461 ms
51,040 KB
testcase_15 AC 1,540 ms
60,292 KB
testcase_16 TLE -
testcase_17 AC 1,680 ms
59,828 KB
testcase_18 AC 1,161 ms
60,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	void run() {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		String[] a=new String[n];
		for (int i=0;i<n;++i) a[i]=sc.next();
		int[] ord=new int[n];
		for (int i=0;i<n;++i) ord[i]=i;
		long ans=-1;
		do {
			StringBuilder v=new StringBuilder();
			for (int i=0;i<n;++i) v.append(a[ord[i]]);
			if (ans>Long.valueOf(v.toString())) continue;
			if (new BigInteger(v.toString()).isProbablePrime(10)) ans=Math.max(ans, Long.valueOf(v.toString()));
		} while (nextPermutation(ord));
		System.out.println(ans);
	}
	
	boolean nextPermutation(int[] a) {
		int n=a.length;
		int p=n-1;
		while (p>0 && a[p-1]>a[p]) --p;
		if (p==0) return false;
		int q=p;
		while (q+1<n && a[p-1]<a[q+1]) ++q;
		a[p-1]^=a[q];a[q]^=a[p-1];a[p-1]^=a[q];
		int s=p,t=n-1;
		while (s<t) {
			a[s]^=a[t];a[t]^=a[s];a[s]^=a[t];
			++s;--t;
		}
		return true;
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
	
	public static void main(String[] args) {
    	new Main().run();
    }
}

0