some dp problems

This commit is contained in:
2024-06-14 19:49:10 +02:00
parent 66d059f230
commit b67bf7bbea
21 changed files with 124 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,26 @@
// Coin Combinations I
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
ll dp[(int)1e6 + 1];
int main() {
int n; int x; cin >> n >> x;
dp[0] = 1;
vector<int> in(n);
for (int i = 0; i < n; i++) cin >> in[i];
for (int i = 0; i <= x; ++i) {
for (int j = 0; j < n; j++) {
if (in[j] <= i) {
dp[i] += dp[i - in[j]];
dp[i] %= MOD;
}
}
}
cout << dp[x] << endl;
return 0;
}

View File

@@ -0,0 +1,24 @@
// Coin Combinations II
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
ll dp[(int)1e6 + 1];
int main() {
dp[0] = 1;
int n, x;
cin >> n >> x;
for (int i{}; i < n; i++) {
int c; cin >> c;
for (int j = c; j <= x; j++) {
dp[j] += dp[j-c];
dp[j] %= MOD;
}
}
cout << dp[x] << endl;
return 0;
}

View File

@@ -0,0 +1,22 @@
// Dice Combinations
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e6;
const ll MOD = 1e9 + 7;
ll ways[N + 1];
int main() {
ll n; cin >> n;
ways[0] = 1;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= 6 && j <= i; j++) {
ways[i] = (ways[i] + (ways[i - j])) % MOD;
}
}
cout << ways[n];
}

View File

@@ -0,0 +1,23 @@
// Minimizing Coins
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> dp(x + 1, INT_MAX - 5);
dp[0] = 0;
for (int i{}; i < n; i++) {
int c; cin >> c;
for (int j = c; j <= x; j++) {
if (j >= c) {
dp[j] = min(dp[j - c] + 1, dp[j]);
}
}
}
if (dp[x] != INT_MAX - 5) cout << dp[x] << endl;
else cout << -1 << endl;
return 0;
}

View File

@@ -0,0 +1,28 @@
// Removing Digits
#include<bits/stdc++.h>
using namespace std;
int dp[(int)1e6 + 1];
int solve(int n) {
if (dp[n]) return dp[n];
int count = INT_MAX;
if (n == 0) return 0;
for (int i = 1; i <= 10 * n; i*= 10) {
int remove = (n / i) - (n / (10 * i)) * 10;
if (!remove) continue;
count = min(count, 1 + solve(n - remove));
}
dp[n] = count;
return count;
}
int main() {
int n; cin >> n;
cout << solve(n) << endl;
return 0;
}