結果

問題 No.774 tatyamと素数大富豪
ユーザー htensaihtensai
提出日時 2019-12-31 08:43:03
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,630 bytes
コンパイル時間 2,568 ms
コンパイル使用メモリ 78,196 KB
実行使用メモリ 71,308 KB
最終ジャッジ日時 2024-04-28 21:07:20
合計ジャッジ時間 15,033 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 387 ms
71,308 KB
testcase_01 AC 140 ms
41,708 KB
testcase_02 AC 138 ms
41,248 KB
testcase_03 AC 168 ms
41,784 KB
testcase_04 AC 134 ms
41,496 KB
testcase_05 AC 142 ms
41,500 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 135 ms
41,488 KB
testcase_09 AC 177 ms
42,568 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 361 ms
58,696 KB
testcase_13 AC 385 ms
58,612 KB
testcase_14 AC 167 ms
42,216 KB
testcase_15 TLE -
testcase_16 AC 165 ms
54,204 KB
testcase_17 AC 167 ms
54,272 KB
testcase_18 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int[] arr;
    static int n;
    static boolean[] used;
    static TreeSet<Long> numbers = new TreeSet<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        arr = new int[n];
        used = new boolean[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        make(1, 0, used);
        for (long x : numbers) {
            x *= -1;
            if (isPrime(x)) {
                System.out.println(x);
                return;
            }
        }
        System.out.println(-1);
    }
    
    static void make(int count, long value, boolean[] used) {
        if (count > n) {
            numbers.add(-value);
            return;
        }
        int prev = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (!used[i]) {
                if (prev == arr[i]) {
                    continue;
                }
                prev = arr[i];
                used[i] = true;
                long next;
                if (arr[i] >= 10) {
                    next = value * 100 + arr[i];
                } else {
                    next = value * 10 + arr[i];
                }
                make(count + 1, next, used);
                used[i] = false;
            }
        }
    }
    
    static boolean isPrime(long x) {
        if (x % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(x); i += 2) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }
 }
0