#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int N;
cin >> N;
vector<int> c;
c.reserve(N);
for (int i = 0; i < N; i++) {
int a;
cin >> a;
c.push_back(a);
}
sort(c.begin(), c.end());
unsigned long long cnt = 1;
int last = *c.rbegin();
vector<unsigned long long> v(last, 0);
v[0] = 1;
const int MOD = 1000000007;
unsigned long long total = 0;
for (int i = 0; i < N; i++) {
int candies = c[i];
for (int j = 0; j < last && j < candies-1; j++) {
cnt = (cnt + MOD - v[j]) % MOD;
v[j] = 0;
}
for (int j = last-1; j >= candies; j--)
v[j] = (v[j] + v[j-candies]) % MOD;
total = (total + cnt) % MOD;
cnt = (cnt*2) % MOD;
}
cout << total << 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 44 45 46 47 48 49 50 51 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); int N; cin >> N; vector<int> c; c.reserve(N); for (int i = 0; i < N; i++) { int a; cin >> a; c.push_back(a); } sort(c.begin(), c.end()); unsigned long long cnt = 1; int last = *c.rbegin(); vector<unsigned long long> v(last, 0); v[0] = 1; const int MOD = 1000000007; unsigned long long total = 0; for (int i = 0; i < N; i++) { int candies = c[i]; for (int j = 0; j < last && j < candies-1; j++) { cnt = (cnt + MOD - v[j]) % MOD; v[j] = 0; } for (int j = last-1; j >= candies; j--) v[j] = (v[j] + v[j-candies]) % MOD; total = (total + cnt) % MOD; cnt = (cnt*2) % MOD; } cout << total << endl; return 0; } |
English