結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 576 ms
51,000 KB
testcase_01 AC 136 ms
41,184 KB
testcase_02 AC 127 ms
41,112 KB
testcase_03 AC 213 ms
43,044 KB
testcase_04 AC 157 ms
41,576 KB
testcase_05 AC 225 ms
43,376 KB
testcase_06 AC 115 ms
39,996 KB
testcase_07 AC 128 ms
41,508 KB
testcase_08 AC 116 ms
40,280 KB
testcase_09 AC 130 ms
41,292 KB
testcase_10 TLE -
testcase_11 AC 1,110 ms
60,388 KB
testcase_12 AC 1,016 ms
59,132 KB
testcase_13 AC 586 ms
51,112 KB
testcase_14 AC 130 ms
41,212 KB
testcase_15 AC 1,357 ms
59,708 KB
testcase_16 TLE -
testcase_17 AC 1,905 ms
60,080 KB
testcase_18 AC 1,149 ms
60,380 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];
		long sum=0;
		for (int i=0;i<n;++i) {
			a[i]=sc.next();
			sum+=Integer.valueOf(a[i]);
		}
		int[] ord=new int[n];
		for (int i=0;i<n;++i) ord[i]=i;
		long ans=-1;
		if (sum%3!=0&&sum%9!=0) {
			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