dice combinations cses code example

Example 1: cses money sums solution

#include 
using namespace std;

int main() {
  int n;
  cin >> n;
  int max_sum = n*1000;
  vector x(n);
  for (int&v : x) cin >> v;
  vector> dp(n+1,vector(max_sum+1,false));
  dp[0][0] = true;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= max_sum; j++) {
      dp[i][j] = dp[i-1][j];
      int left = j-x[i-1];
      if (left >= 0 && dp[i-1][left]) {
	dp[i][j] = true;
      }
    }
  }

  vector possible;
  for (int j = 1; j <= max_sum; j++) {
    if (dp[n][j]) {
      possible.push_back(j);
    }
  }
  cout << possible.size() << endl;
  for (int v : possible) {
    cout << v << ' ';
  }
  cout << endl;
}

Example 2: dice combinations cses

#include 
using namespace std;

int main() {
  int mod = 1e9+7;
  int n;
  cin >> n;
  vector dp(n+1,0);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 6 && i-j >= 0; j++) {
      (dp[i] += dp[i-j]) %= mod;
    }
  }
  cout << dp[n] << endl;
}

Tags:

Misc Example