#include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const int mod = 1e9 + 7; const int maxVal = 5000; int dp[maxVal+1][maxVal+1]; void solve() { int n; cin >> n; Vi arr(n+1); for (int i = 1; i <= n; i++) { cin >> arr[i]; } sort(all(arr)); dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= maxVal; j++) { dp[i][j] = dp[i-1][j]; } for (int j = arr[i] - 1; j <= maxVal; j++) { int k = min(maxVal, j + arr[i]); dp[i][k] += dp[i-1][j]; if(dp[i][k] >= mod) { dp[i][k] -= mod; } } } int res = 0; for (int i = 1; i <= maxVal; i++) { res += dp[n][i]; if (res >= mod) { res -= mod; } } cout << res << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const int mod = 1e9 + 7; const int maxVal = 5000; int dp[maxVal+1][maxVal+1]; void solve() { int n; cin >> n; Vi arr(n+1); for (int i = 1; i <= n; i++) { cin >> arr[i]; } sort(all(arr)); dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= maxVal; j++) { dp[i][j] = dp[i-1][j]; } for (int j = arr[i] - 1; j <= maxVal; j++) { int k = min(maxVal, j + arr[i]); dp[i][k] += dp[i-1][j]; if(dp[i][k] >= mod) { dp[i][k] -= mod; } } } int res = 0; for (int i = 1; i <= maxVal; i++) { res += dp[n][i]; if (res >= mod) { res -= mod; } } cout << res << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } } |