Advertisement
ogv

Untitled

ogv
Jan 13th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4. private static final int M = 1000000007;
  5.  
  6. private static long solve(int N, int K, int[] a) {
  7. int[] ways = new int[K+1];
  8. int[] diff = new int[K+1];
  9. ways[0] = 1;
  10.  
  11. for (int ai: a) {
  12. Arrays.fill(diff, 0);
  13.  
  14. int s = 0;
  15. for (int i = 1; i <= K; i++) {
  16. int add = ways[i-1];
  17. int remove = (i-ai-1 >= 0) ? ways[i-ai-1]: 0;
  18. s = (s + add - remove) % M;
  19.  
  20. diff[i] = (diff[i] + s) % M;
  21. }
  22.  
  23. for (int i = 0; i <= K; i++)
  24. ways[i] = (ways[i] + diff[i]) % M;
  25. }
  26.  
  27. return ways[K];
  28. }
  29.  
  30. public static long run(Scanner scanner) {
  31. int N = scanner.nextInt();
  32. int K = scanner.nextInt();
  33. int[] a = new int[N];
  34. for (int i=0; i < N; i++) a[i] = scanner.nextInt();
  35.  
  36. return solve(N, K, a);
  37. }
  38.  
  39. public static void main(String[] args) {
  40. try (Scanner scanner = new Scanner(System.in)) {
  41. System.out.println(run(scanner));
  42. }
  43. Tests.run();
  44. }
  45. }
  46.  
  47. class Tests {
  48. public static void run() {
  49. testCase("3 4\n" +
  50. "1 2 3", 5);
  51. testCase("1 10\n" +
  52. "9", 0);
  53. testCase("2 0\n" +
  54. "0 0", 1);
  55. testCase("4 100000\n" +
  56. "100000 100000 100000 100000", 665683269);
  57.  
  58. System.out.println("DONE");
  59. }
  60.  
  61. private static void testCase(String input, long expected) {
  62. try (Scanner scanner = new Scanner(input)) {
  63. long result = Main.run(scanner);
  64. if (result != expected) System.out.println("TEST FAILED: was " + result + " expected " + expected + " on input\n" + input);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement