結果

問題 No.774 tatyamと素数大富豪
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-25 03:08:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,075 ms / 2,000 ms
コード長 1,842 bytes
コンパイル時間 3,745 ms
コンパイル使用メモリ 78,772 KB
実行使用メモリ 67,680 KB
最終ジャッジ日時 2023-10-14 09:47:07
合計ジャッジ時間 11,908 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 887 ms
66,884 KB
testcase_01 AC 149 ms
56,040 KB
testcase_02 AC 125 ms
55,952 KB
testcase_03 AC 230 ms
56,788 KB
testcase_04 AC 194 ms
56,556 KB
testcase_05 AC 144 ms
56,168 KB
testcase_06 AC 122 ms
55,948 KB
testcase_07 AC 122 ms
55,816 KB
testcase_08 AC 121 ms
56,096 KB
testcase_09 AC 378 ms
64,092 KB
testcase_10 AC 195 ms
57,004 KB
testcase_11 AC 338 ms
60,420 KB
testcase_12 AC 1,075 ms
67,680 KB
testcase_13 AC 605 ms
66,360 KB
testcase_14 AC 246 ms
59,120 KB
testcase_15 AC 449 ms
64,964 KB
testcase_16 AC 266 ms
60,136 KB
testcase_17 AC 194 ms
57,020 KB
testcase_18 AC 344 ms
62,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.math.*;
import java.util.*;
import java.io.*;

public class y774 {
  public static void main(String[] args) { new y774().run(); }
  Scanner in = new Scanner(System.in);
  void _out(Object...os) { System.out.println(deepToString(os)); }
  void _err(Object...os) { System.err.println(deepToString(os)); }

  int N;
  int[] A;
  
  int[] work;
  
  void run() {
    for (; in.hasNext(); ) {
      N = in.nextInt();
      A = new int[N];
      for (int i = 0; i < N; ++i) {
        A[i] = in.nextInt();
      }
      sort(A);
      work = new int[N];
      BigInteger ans = valueOf(-1);
      do {
        if (N >= 2 && (A[N - 1] % 2 == 0 || A[N - 1] % 5 == 0)) {
          continue;
        }
        String str = "";
        for (int i = 0; i < N; ++i) {
          str += Integer.toString(A[i]);
        }
        BigInteger x = new BigInteger(str);
        if (ans.compareTo(x) < 0) {
          if (x.isProbablePrime(100)) {
            ans = x;
          }
        }
      } while (nextPermutation());
      System.out.println(ans);
    }
  }
  
  boolean nextPermutation() {
    for (int i = N - 2; i >= 0; --i) {
      if (A[i] < A[i + 1]) {
        for (int j = N - 1; j > i; --j) {
          if (A[i] < A[j]) {
            int workLen = 0;
            work[workLen++] = A[j];
            for (int k = N - 1; k > j; --k) {
              work[workLen++] = A[k];
            }
            work[workLen++] = A[i];
            for (int k = j - 1; k > i; --k) {
              work[workLen++] = A[k];
            }
            for (int k = i; k < N; ++k) {
              A[k] = work[k - i];
            }
            return true;
          }
        }
      }
    }
    return false;
  }
}
0