Hello mates, I'm trying to solve this DP question Coin Combinations II from CSES: https://cses.fi/problemset/task/1636/↵
↵
Initially, I define an array dp[i][j]: meaning number of ways to form sum i, starting choosing coins from j-th index. And this solution using this DP array get me TLE.↵
↵
But if I switch the two dimension, array dp[i][j]: number of ways to form sum j, starting choosing coins from i-th index. This solution give me accepted. But why?↵
↵
Note:↵
↵
+ Two solution is 99% the same the only different is that I switch two dimensions.↵
↵
+ Sum i at most 10^6 and there are at most 10^2 coins.↵
↵
↵
Thank in advance.↵
↵
↵
↵
<spoiler summary="Accepted code">↵
```c++↵
#include<bits/stdc++.h>↵
using namespace std;↵
↵
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)↵
void IN_OUT() {↵
#ifndef ONLINE_JUDGE↵
freopen("Input.txt", "r", stdin);↵
freopen("Output.txt", "w", stdout);↵
#endif↵
}↵
/*--------------------------------------------------------------------------------------------------------------------------*/↵
↵
const int MOD = 1e9 + 7;↵
↵
const int mxn = 1e2 + 5;↵
const int mxx = 1e6 + 5;↵
int a[mxn];↵
int dp[mxn][mxx]; // dp[i][j]: number of ways to form sum j, starting choosing coins from i-th index↵
int n, x;↵
void solve() {↵
cin >> n >> x;↵
for (int i = 0; i < n; i++) {↵
cin >> a[i];↵
dp[i][0] = 1;↵
}↵
↵
for (int j = n - 1; j >= 0; j--) {↵
for (int i = 1; i <= x; i++) {↵
dp[j][i] = dp[j + 1][i];↵
if (i >= a[j]) {↵
dp[j][i] += dp[j][i - a[j]];↵
dp[j][i] %= MOD;↵
}↵
}↵
}↵
↵
cout << dp[0][x] << "\n";↵
}↵
↵
int main() {↵
fastio();↵
IN_OUT();↵
solve();↵
return 0;↵
}↵
```↵
</spoiler>↵
↵
<spoiler summary="TLE code">↵
```c++↵
#include<bits/stdc++.h>↵
using namespace std;↵
↵
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)↵
void IN_OUT() {↵
#ifndef ONLINE_JUDGE↵
freopen("Input.txt", "r", stdin);↵
freopen("Output.txt", "w", stdout);↵
#endif↵
}↵
/*--------------------------------------------------------------------------------------------------------------------------*/↵
↵
const int MOD = 1e9 + 7;↵
↵
const int mxn = 1e2 + 5;↵
const int mxx = 1e6 + 5;↵
int a[mxn];↵
int dp[mxx][mxn]; // dp[i][j]: number of ways to form sum i, starting choosing coins from j-th index↵
int n, x;↵
void solve() {↵
cin >> n >> x;↵
for (int i = 0; i < n; i++) {↵
cin >> a[i];↵
dp[0][i] = 1;↵
}↵
↵
for (int j = n - 1; j >= 0; j--) {↵
for (int i = 1; i <= x; i++) {↵
dp[i][j] = dp[i][j + 1];↵
if (i >= a[j]) {↵
dp[i][j] += dp[i - a[j]][j];↵
dp[i][j] %= MOD;↵
}↵
}↵
}↵
↵
cout << dp[x][0] << "\n";↵
}↵
↵
int main() {↵
fastio();↵
IN_OUT();↵
solve();↵
return 0;↵
}↵
```↵
</spoiler>↵
↵
↵
Initially, I define an array dp[i][j]: meaning number of ways to form sum i, starting choosing coins from j-th index. And this solution using this DP array get me TLE.↵
↵
But if I switch the two dimension, array dp[i][j]: number of ways to form sum j, starting choosing coins from i-th index. This solution give me accepted. But why?↵
↵
Note:↵
↵
+ Two solution is 99% the same the only different is that I switch two dimensions.↵
↵
+ Sum i at most 10^6 and there are at most 10^2 coins.↵
↵
↵
Thank in advance.↵
↵
↵
↵
<spoiler summary="Accepted code">↵
```c++↵
#include<bits/stdc++.h>↵
using namespace std;↵
↵
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)↵
void IN_OUT() {↵
#ifndef ONLINE_JUDGE↵
freopen("Input.txt", "r", stdin);↵
freopen("Output.txt", "w", stdout);↵
#endif↵
}↵
/*--------------------------------------------------------------------------------------------------------------------------*/↵
↵
const int MOD = 1e9 + 7;↵
↵
const int mxn = 1e2 + 5;↵
const int mxx = 1e6 + 5;↵
int a[mxn];↵
int dp[mxn][mxx]; // dp[i][j]: number of ways to form sum j, starting choosing coins from i-th index↵
int n, x;↵
void solve() {↵
cin >> n >> x;↵
for (int i = 0; i < n; i++) {↵
cin >> a[i];↵
dp[i][0] = 1;↵
}↵
↵
for (int j = n - 1; j >= 0; j--) {↵
for (int i = 1; i <= x; i++) {↵
dp[j][i] = dp[j + 1][i];↵
if (i >= a[j]) {↵
dp[j][i] += dp[j][i - a[j]];↵
dp[j][i] %= MOD;↵
}↵
}↵
}↵
↵
cout << dp[0][x] << "\n";↵
}↵
↵
int main() {↵
fastio();↵
IN_OUT();↵
solve();↵
return 0;↵
}↵
```↵
</spoiler>↵
↵
<spoiler summary="TLE code">↵
```c++↵
#include<bits/stdc++.h>↵
using namespace std;↵
↵
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)↵
void IN_OUT() {↵
#ifndef ONLINE_JUDGE↵
freopen("Input.txt", "r", stdin);↵
freopen("Output.txt", "w", stdout);↵
#endif↵
}↵
/*--------------------------------------------------------------------------------------------------------------------------*/↵
↵
const int MOD = 1e9 + 7;↵
↵
const int mxn = 1e2 + 5;↵
const int mxx = 1e6 + 5;↵
int a[mxn];↵
int dp[mxx][mxn]; // dp[i][j]: number of ways to form sum i, starting choosing coins from j-th index↵
int n, x;↵
void solve() {↵
cin >> n >> x;↵
for (int i = 0; i < n; i++) {↵
cin >> a[i];↵
dp[0][i] = 1;↵
}↵
↵
for (int j = n - 1; j >= 0; j--) {↵
for (int i = 1; i <= x; i++) {↵
dp[i][j] = dp[i][j + 1];↵
if (i >= a[j]) {↵
dp[i][j] += dp[i - a[j]][j];↵
dp[i][j] %= MOD;↵
}↵
}↵
}↵
↵
cout << dp[x][0] << "\n";↵
}↵
↵
int main() {↵
fastio();↵
IN_OUT();↵
solve();↵
return 0;↵
}↵
```↵
</spoiler>↵
↵