Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- private static final int M = 1000000007;
- private static long solve(int N, int K, int[] a) {
- int[] ways = new int[K+1];
- int[] diff = new int[K+1];
- ways[0] = 1;
- for (int ai: a) {
- Arrays.fill(diff, 0);
- int s = 0;
- for (int i = 1; i <= K; i++) {
- int add = ways[i-1];
- int remove = (i-ai-1 >= 0) ? ways[i-ai-1]: 0;
- s = (s + add - remove) % M;
- diff[i] = (diff[i] + s) % M;
- }
- for (int i = 0; i <= K; i++)
- ways[i] = (ways[i] + diff[i]) % M;
- }
- return ways[K];
- }
- public static long run(Scanner scanner) {
- int N = scanner.nextInt();
- int K = scanner.nextInt();
- int[] a = new int[N];
- for (int i=0; i < N; i++) a[i] = scanner.nextInt();
- return solve(N, K, a);
- }
- public static void main(String[] args) {
- try (Scanner scanner = new Scanner(System.in)) {
- System.out.println(run(scanner));
- }
- Tests.run();
- }
- }
- class Tests {
- public static void run() {
- testCase("3 4\n" +
- "1 2 3", 5);
- testCase("1 10\n" +
- "9", 0);
- testCase("2 0\n" +
- "0 0", 1);
- testCase("4 100000\n" +
- "100000 100000 100000 100000", 665683269);
- System.out.println("DONE");
- }
- private static void testCase(String input, long expected) {
- try (Scanner scanner = new Scanner(input)) {
- long result = Main.run(scanner);
- if (result != expected) System.out.println("TEST FAILED: was " + result + " expected " + expected + " on input\n" + input);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement