#include <algorithm> #include <cstdio> using namespace std; const int MAX_N = 5001; const long long mod = 1000000007; long long cnt[MAX_N * MAX_N], n, a[MAX_N], hi = 0; int main() { scanf("%lld", &n); for(int i = 0; i < n; i++) scanf("%lld", &a[i]); sort(a, a + n); cnt[0] = 1; for(int i = 0; i < n; i++) { for(int j = hi; j + 1 >= a[i]; j--) { if(cnt[j] != 0) { cnt[j + a[i]] = (cnt[j + a[i]] + cnt[j]) % mod; hi = max(hi, j + a[i]); } } } long long result = 0; for(int i = 1; i <= hi; i++) result = (result + cnt[i]) % mod; printf("%lld\n", result); 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 | #include <algorithm> #include <cstdio> using namespace std; const int MAX_N = 5001; const long long mod = 1000000007; long long cnt[MAX_N * MAX_N], n, a[MAX_N], hi = 0; int main() { scanf("%lld", &n); for(int i = 0; i < n; i++) scanf("%lld", &a[i]); sort(a, a + n); cnt[0] = 1; for(int i = 0; i < n; i++) { for(int j = hi; j + 1 >= a[i]; j--) { if(cnt[j] != 0) { cnt[j + a[i]] = (cnt[j + a[i]] + cnt[j]) % mod; hi = max(hi, j + a[i]); } } } long long result = 0; for(int i = 1; i <= hi; i++) result = (result + cnt[i]) % mod; printf("%lld\n", result); return 0; } |