Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class DPM_1 {
  5.  
  6. //memo[i][j] i children and j candies store the numbers of ways
  7. //memo[i][0] 1
  8. //memo[i][j] = memo[i-1][j]+memo[i-1][j-1]+memo[i-1][j-2]+...+memo[i-1][j-ai];
  9.  
  10. public static long memo[][] = new long[2][1000005];
  11. public static long prefixSum[][] = new long[2][1000005];
  12. public static int a[] = new int[100005];
  13. public static final long MOD = 1000000007;
  14.  
  15. public static void main(String[] args) throws IOException
  16. {
  17. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  18. String[] tmp = in.readLine().split(" ");
  19. int N = Integer.parseInt(tmp[0]);
  20. int K = Integer.parseInt(tmp[1]);
  21.  
  22. tmp = in.readLine().split(" ");
  23. for (int i = 1; i <= N; i++)
  24. a[i] = Integer.parseInt(tmp[i-1]);
  25.  
  26. memo[0][0] = 1;
  27. int cur = 1, prev = 0;
  28. for (int i = 1; i <= N; i++)
  29. {
  30. for (int j = 0; j <= K; j++)
  31. {
  32. //Using prefixSum to calculate current value
  33. memo[cur][j] = prefixSum[prev][j];
  34. if (j-a[i] > 0)
  35. memo[cur][j] = prefixSum[prev][j]-prefixSum[prev][j-a[i]-1];
  36. memo[cur][j] %= MOD;
  37. memo[cur][j] += MOD;
  38. memo[cur][j] %= MOD;
  39. }
  40. //recalculate prefix sum
  41. for (int j = 1; j <= K; j++)
  42. prefixSum[cur][j] = (memo[cur][j] + prefixSum[cur][j-1]) % MOD;
  43. cur ^= 1;
  44. prev ^= 1;
  45. }
  46. System.out.println(memo[prev][K]);
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement