#include <iostream> #include <algorithm> #include <unordered_map> #define N 5002 #define M 5001 #define P 1000000007 using namespace std; int n; int sweets[N]; long long dp[N][M]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> sweets[i]; } sort(sweets + 1, sweets + n + 1); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j < M; j++) { dp[i][j] = dp[i - 1][j]; } for (int j = 0; j < M; j++) { if (j >= sweets[i] - 1) { dp[i][min(j + sweets[i], M - 1)] = (dp[i][min(j + sweets[i], M - 1)] + dp[i - 1][j]) % P; } } } long long finalSum = 0; for (int i = 1; i < M; i++) { finalSum = (finalSum + dp[n][i]) % P; } cout << finalSum << endl; return 0; }
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 | #include <iostream> #include <algorithm> #include <unordered_map> #define N 5002 #define M 5001 #define P 1000000007 using namespace std; int n; int sweets[N]; long long dp[N][M]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> sweets[i]; } sort(sweets + 1, sweets + n + 1); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j < M; j++) { dp[i][j] = dp[i - 1][j]; } for (int j = 0; j < M; j++) { if (j >= sweets[i] - 1) { dp[i][min(j + sweets[i], M - 1)] = (dp[i][min(j + sweets[i], M - 1)] + dp[i - 1][j]) % P; } } } long long finalSum = 0; for (int i = 1; i < M; i++) { finalSum = (finalSum + dp[n][i]) % P; } cout << finalSum << endl; return 0; } |