Submission #858797


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(solve2(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)
        // 平均 0 を求めるようにすると,O(n^2X)
        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;
    }

}

Submission Info

Submission Time
Task C - Tak and Cards
User nes_in_it
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 1824 Byte
Status AC
Exec Time 545 ms
Memory 77332 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 383 ms 16332 KB
example_02.txt AC 303 ms 15188 KB
example_03.txt AC 332 ms 15424 KB
example_04.txt AC 325 ms 16208 KB
subtask1_01.txt AC 337 ms 17100 KB
subtask1_02.txt AC 356 ms 16452 KB
subtask1_03.txt AC 345 ms 16716 KB
subtask1_04.txt AC 360 ms 16848 KB
subtask1_05.txt AC 328 ms 16580 KB
subtask1_06.txt AC 311 ms 14924 KB
subtask1_07.txt AC 324 ms 15052 KB
subtask1_08.txt AC 347 ms 16460 KB
subtask1_09.txt AC 327 ms 16464 KB
subtask2_01.txt AC 525 ms 76748 KB
subtask2_02.txt AC 517 ms 77308 KB
subtask2_03.txt AC 482 ms 61436 KB
subtask2_04.txt AC 527 ms 77192 KB
subtask2_05.txt AC 513 ms 77332 KB
subtask2_06.txt AC 545 ms 76764 KB
subtask2_07.txt AC 519 ms 76624 KB
subtask2_08.txt AC 389 ms 35596 KB
subtask2_09.txt AC 408 ms 35984 KB
subtask2_10.txt AC 439 ms 43784 KB
subtask2_11.txt AC 463 ms 46792 KB