結果

問題 No.3024 等式
ユーザー 37zigen37zigen
提出日時 2017-03-31 23:44:13
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,116 bytes
コンパイル時間 2,938 ms
コンパイル使用メモリ 82,908 KB
実行使用メモリ 80,040 KB
最終ジャッジ日時 2023-09-22 14:05:18
合計ジャッジ時間 18,088 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
56,156 KB
testcase_01 AC 117 ms
55,572 KB
testcase_02 AC 127 ms
55,644 KB
testcase_03 AC 127 ms
55,932 KB
testcase_04 AC 118 ms
55,772 KB
testcase_05 AC 118 ms
55,444 KB
testcase_06 AC 187 ms
61,816 KB
testcase_07 AC 188 ms
61,640 KB
testcase_08 AC 186 ms
61,828 KB
testcase_09 AC 123 ms
55,692 KB
testcase_10 AC 130 ms
53,744 KB
testcase_11 AC 127 ms
55,868 KB
testcase_12 AC 1,624 ms
80,040 KB
testcase_13 AC 1,652 ms
79,992 KB
testcase_14 AC 1,651 ms
79,776 KB
testcase_15 AC 143 ms
57,576 KB
testcase_16 AC 123 ms
55,772 KB
testcase_17 AC 310 ms
73,968 KB
testcase_18 AC 123 ms
55,428 KB
testcase_19 AC 125 ms
55,420 KB
testcase_20 AC 171 ms
64,984 KB
testcase_21 AC 132 ms
57,188 KB
testcase_22 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	ArrayList<Double>[][] dp;

	// [left,right]
	ArrayList<Double> rec(int left, int right, int[] A, int[] ord) {
		if (dp[left][right].size() != 0) {
			return dp[left][right];
		}
		if (right - left == 0) {
			dp[left][right].add((double) A[ord[left]]);
			return dp[left][right];
		}
		ArrayList<Double> ret = new ArrayList<>();
		for (int i = left; i < right; ++i) {
			ArrayList<Double> lv = rec(left, i, A, ord);
			ArrayList<Double> rv = rec(i + 1, right, A, ord);
			for (double v1 : lv) {
				for (double v2 : rv) {
					ret.add(v1 + v2);
					ret.add(v1 - v2);
					if (Math.abs(v1 + v2) < 1e-6 || Math.abs(v1 - v2) < 1e-6) {
						System.out.println("YES");
						System.exit(0);
					}
					if (v2 != 0) {
						ret.add(v1 / v2);
					}
					ret.add(v1 * v2);
				}
			}
		}
		dp[left][right] = ret;
		return ret;
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; ++i) {
			A[i] = sc.nextInt();
		}
		int[] ord = new int[N];
		for (int i = 0; i < ord.length; ++i) {
			ord[i] = i;
		}
		// permutationの状態数は5040通り。
		do {
			dp = new ArrayList[N][N];
			for (int i = 0; i < N; ++i) {
				for (int j = 0; j < N; ++j) {
					dp[i][j] = new ArrayList<>();
				}
			}
			rec(0, N - 1, A, ord);
		} while (nextPermutation(ord));
		System.out.println("NO");
	}

	boolean nextPermutation(int[] ord) {
		int n = ord.length;
		int i = n - 1;
		while (i - 1 >= 0 && ord[i - 1] > ord[i]) {
			--i;
		}

		if (i == 0)
			return false;
		int j = i;

		while (j + 1 < n && ord[i - 1] < ord[j + 1]) {
			++j;
		}
		int tmp = ord[i - 1];
		ord[i - 1] = ord[j];
		ord[j] = tmp;
		int s = i;
		int t = n - 1;
		while (t - s > 0) {
			tmp = ord[t];
			ord[t] = ord[s];
			ord[s] = tmp;
			++s;
			--t;
		}

		return true;

	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0