Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
165
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. //https://pastebin.com/uSuh7zuG
  4. public class DPM_1 {
  5. //memo[i][j] i children and j candies store the numbers of ways
  6. //memo[i][0] 1
  7. //memo[i][j] = memo[i-1][j]+memo[i-1][j-1]+memo[i-1][j-2]+...+memo[i-1][j-ai];
  8. public static long memo[][] = new long[2][1000005];
  9. public static long prefixSum[][] = new long[2][1000005];
  10. public static int a[] = new int[100005];
  11. public static final long MOD = 1000000007;
  12.  
  13. public static void main(String[] args) throws IOException
  14. {
  15. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  16. String[] tmp = in.readLine().split(" ");
  17. int N = Integer.parseInt(tmp[0]);
  18. int K = Integer.parseInt(tmp[1]);
  19.  
  20. tmp = in.readLine().split(" ");
  21. for (int i = 1; i <= N; i++)
  22. a[i] = Integer.parseInt(tmp[i-1]);
  23.  
  24. memo[0][0] = 1;
  25. int cur = 1, prev = 0;
  26. for (int i = 1; i <= N; i++)
  27. {
  28. for (int j = 0; j <= K; j++)
  29. {
  30. //Using prefixSum to calculate current value
  31. memo[cur][j] = memo[prev][j];
  32. if (j-a[i] > 0)
  33. memo[cur][j] = memo[prev][j]-memo[prev][j-a[i]-1];
  34. memo[cur][j] %= MOD;
  35. memo[cur][j] += MOD;
  36. memo[cur][j] %= MOD;
  37. }
  38. //recalculate prefix sum
  39. for (int j = 1; j <= K; j++)
  40. memo[cur][j] = (memo[cur][j] + memo[cur][j-1]) % MOD;
  41. cur ^= 1;
  42. prev ^= 1;
  43. }
  44. System.out.println(memo[prev][K]);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement