fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int N, K;
  6. cin >> N >> K;
  7. const int P = N+1, Q = K+1, mod = 1e9+7, null = -1;
  8. using ivector = vector<int>;
  9. ivector a(P);
  10. for (int i = 1; i < P; ++i)
  11. cin >> a[i];
  12. vector<ivector> dp(P,ivector(Q,null));
  13. const function<int(int,int)> solve = [&](int n, int k) {
  14. // returns the number of ways to share (k) candies among the first
  15. // (n) children such that the i-th child, 1 <= i <= n, gets v[i] candies,
  16. // where 0 <= v[i] <= a[i] and v[1]+v[2]+....+v[n] = k.
  17. if (n == 0 or k == 0) // base case
  18. return int(k == 0);
  19. if (dp[n][k] == null) { // recursive case
  20. dp[n][k] = 0;
  21. for (int p = n-1, q = k, v = 0; q >= 0 and v <= a[n]; --q, ++v)
  22. dp[n][k] += solve(p,q), dp[n][k] %= mod; }
  23. return dp[n][k]; };
  24. cout << solve(N,K);
  25. return 0; }
  26.  
Success #stdin #stdout 0.01s 5368KB
stdin
3 4
1 2 3
stdout
5