#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n; cin >> n; vector<long long> sweets; vector<long long> bag; for(int i=0; i<n; i++){ long long s; cin >> s; sweets.push_back(s); } sort(sweets.begin(), sweets.end()); for(int i=0; i<sweets.back(); i++){ bag.push_back(0); } bag[0] = 1; bag.push_back(0); for(long long i=0; i<sweets.size(); i++){ if(sweets.back()>0){ bag.back() += bag.back()%(1000000007); } for(long long j=bag.size()-2; j>=0; j--){ if(bag[j]>0){ if(sweets[i]-1 <= j){ if(j+sweets[i]>bag.size()-2){ bag.back() = (bag.back() + bag[j])%(1000000007); }else{ bag[j+sweets[i]] = (bag[j+sweets[i]] + bag[j])%(1000000007); } } } } } long long sum = 0; for(int i=1; i<bag.size(); i++){ sum = (sum+bag[i])%(1000000007);; } cout << sum; }
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<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n; cin >> n; vector<long long> sweets; vector<long long> bag; for(int i=0; i<n; i++){ long long s; cin >> s; sweets.push_back(s); } sort(sweets.begin(), sweets.end()); for(int i=0; i<sweets.back(); i++){ bag.push_back(0); } bag[0] = 1; bag.push_back(0); for(long long i=0; i<sweets.size(); i++){ if(sweets.back()>0){ bag.back() += bag.back()%(1000000007); } for(long long j=bag.size()-2; j>=0; j--){ if(bag[j]>0){ if(sweets[i]-1 <= j){ if(j+sweets[i]>bag.size()-2){ bag.back() = (bag.back() + bag[j])%(1000000007); }else{ bag[j+sweets[i]] = (bag[j+sweets[i]] + bag[j])%(1000000007); } } } } } long long sum = 0; for(int i=1; i<bag.size(); i++){ sum = (sum+bag[i])%(1000000007);; } cout << sum; } |