結果

問題 No.774 tatyamと素数大富豪
ユーザー htensaihtensai
提出日時 2019-12-30 23:45:03
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,488 bytes
コンパイル時間 1,766 ms
コンパイル使用メモリ 77,320 KB
実行使用メモリ 118,016 KB
最終ジャッジ日時 2024-04-27 17:48:44
合計ジャッジ時間 8,288 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int[] arr;
    static int n;
    static boolean[] used;
    static long max = -1;
    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);
        System.out.println(max);
    }
    
    static void make(int count, long value, boolean[] used) {
        if (count > n) {
            if (isPrime(value)) {
                max = Math.max(max, value);
            }
            return;
        }
        int prev = 0;
        for (int i = 0; i < n; 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