結果

問題 No.774 tatyamと素数大富豪
ユーザー 37zigen37zigen
提出日時 2020-04-20 03:48:36
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,058 bytes
コンパイル時間 2,519 ms
コンパイル使用メモリ 79,772 KB
実行使用メモリ 60,040 KB
最終ジャッジ日時 2024-04-16 00:30:14
合計ジャッジ時間 16,653 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 136 ms
41,584 KB
testcase_02 TLE -
testcase_03 AC 271 ms
45,816 KB
testcase_04 AC 195 ms
42,564 KB
testcase_05 AC 271 ms
47,684 KB
testcase_06 AC 128 ms
41,120 KB
testcase_07 AC 130 ms
41,208 KB
testcase_08 AC 115 ms
40,292 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

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 (new BigInteger(v.toString()).isProbablePrime(20)) 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