Submission #861534


Source Code Expand

Copy
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A = sc.nextInt();
        int[] x = new int[N];
        for (int i = 0; i < N; i++) {
            x[i] = sc.nextInt();
        }
        System.out.println(solve3(N, A, x));
    }

    private static long solve(int n, int a, int[] x) {
        int[] y = Arrays.stream(x).map(i -> i - a).toArray();
        return brute(n - 1, 0, y);
    }

    private static long brute(final int i, final int n, final int[] y) {
        if (i < 0) return 0;
        return (n + y[i] == 0 ? 1 : 0) + brute(i - 1, n, y) + brute(i - 1, n + y[i], y);
    }

    private static long solve2(int n, int a, int[] x) {
        // dp[i][j][k] := {x_1 ... x_i} から j 個選んで,和が k となる組み合わせの数 O(n^3X)
        int X = Math.max(a, Arrays.stream(x).max().getAsInt());
        long[][][] dp = new long[n + 1][n + 1][n * X + 1];
        dp[0][0][0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                for (int k = 0; k <= n * X; k++) {
                    if (k - x[i - 1] >= 0 && j - 1 >= 0) {
                        dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i - 1]];
                    } else {
                        dp[i][j][k] = dp[i - 1][j][k];
                    }
                }
            }
        }
        long ans = 0;
        for (int i = 1; i <= n; i++) {
            ans += dp[n][i][i * a];
        }
        return ans;
    }

    private static long solve3(int n, int a, int[] x) {
        // 平均 0 を求めるようにすると,O(n^2X)
        int[] y = Arrays.stream(x).map(v -> v - a).toArray();
        int X = Math.max(a, Arrays.stream(x).max().getAsInt());
        long[][] dp = new long[n + 1][2 * n * X + 1];
        dp[0][n * X] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= 2 * n * X; j++) {
                if (0 <= j - y[i - 1] && j - y[i - 1] <= 2 * n * X ) {
                    dp[i][j] = dp[i - 1][j] + dp[i - 1][j - y[i - 1]];
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[n][n * X] - 1;
    }

}

Submission Info

Submission Time
Task C - Tak and Cards
User nes_in_it
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 2487 Byte
Status AC
Exec Time 365 ms
Memory 16976 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 200 / 200 100 / 100
Status
AC × 4
AC × 12
AC × 24
Set Name Test Cases
Sample example_01.txt, example_02.txt, example_03.txt, example_04.txt
Subtask1 example_01.txt, example_02.txt, example_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt
All example_01.txt, example_02.txt, example_03.txt, example_04.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt
Case Name Status Exec Time Memory
example_01.txt AC 365 ms 15424 KB
example_02.txt AC 313 ms 14796 KB
example_03.txt AC 346 ms 14796 KB
example_04.txt AC 321 ms 14796 KB
subtask1_01.txt AC 325 ms 15564 KB
subtask1_02.txt AC 338 ms 15048 KB
subtask1_03.txt AC 339 ms 14920 KB
subtask1_04.txt AC 327 ms 15300 KB
subtask1_05.txt AC 320 ms 15316 KB
subtask1_06.txt AC 324 ms 14656 KB
subtask1_07.txt AC 324 ms 14672 KB
subtask1_08.txt AC 328 ms 14920 KB
subtask1_09.txt AC 336 ms 14920 KB
subtask2_01.txt AC 353 ms 16824 KB
subtask2_02.txt AC 358 ms 16848 KB
subtask2_03.txt AC 339 ms 16720 KB
subtask2_04.txt AC 351 ms 16976 KB
subtask2_05.txt AC 353 ms 16840 KB
subtask2_06.txt AC 345 ms 16832 KB
subtask2_07.txt AC 342 ms 16844 KB
subtask2_08.txt AC 354 ms 15692 KB
subtask2_09.txt AC 334 ms 15692 KB
subtask2_10.txt AC 342 ms 16196 KB
subtask2_11.txt AC 331 ms 16332 KB