結果

問題 No.3024 等式
ユーザー 37zigen37zigen
提出日時 2017-03-31 23:43:34
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,226 bytes
コンパイル時間 3,029 ms
コンパイル使用メモリ 83,816 KB
実行使用メモリ 81,280 KB
最終ジャッジ日時 2023-09-22 13:58:34
合計ジャッジ時間 19,725 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,524 KB
testcase_01 AC 121 ms
55,348 KB
testcase_02 AC 130 ms
55,864 KB
testcase_03 AC 130 ms
55,712 KB
testcase_04 AC 122 ms
55,556 KB
testcase_05 AC 123 ms
55,696 KB
testcase_06 AC 194 ms
61,700 KB
testcase_07 AC 193 ms
61,672 KB
testcase_08 AC 195 ms
61,828 KB
testcase_09 AC 127 ms
55,140 KB
testcase_10 AC 135 ms
55,372 KB
testcase_11 AC 131 ms
55,436 KB
testcase_12 AC 1,692 ms
80,872 KB
testcase_13 AC 1,897 ms
81,280 KB
testcase_14 AC 1,690 ms
80,232 KB
testcase_15 AC 144 ms
57,872 KB
testcase_16 AC 123 ms
55,604 KB
testcase_17 AC 341 ms
73,904 KB
testcase_18 AC 127 ms
55,416 KB
testcase_19 AC 130 ms
55,376 KB
testcase_20 AC 174 ms
64,760 KB
testcase_21 AC 134 ms
57,564 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<>();
				}
			}
			ArrayList<Double> tmp = rec(0, N - 1, A, ord);
			if (Math.abs(tmp.get(0)) < 1e-6) {
				System.out.println("YES");
				return;
			}
		} 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