#include <bits/stdc++.h> using namespace std; const int q=1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<int>V(n+1,0); for(int i=1;i<=n;i++) cin>>V[i]; sort(V.begin(),V.end()); vector<vector<long long>>Dp(2,vector<long long>(5002,0)); Dp[0][0]=1; Dp[1][0]=1; for(int i=0;i<n;i++) { int a=V[i+1]; for(int j=5001;j>=0;j--) { Dp[(i+1)%2][j]=Dp[i%2][j]; if(j>=a-1) Dp[(i+1)%2][min(j+a,5001)]+=Dp[i%2][j]; if(Dp[(i+1)%2][j]>=q) Dp[(i+1)%2][j]%=q; } } long long wynik=0; for(int j=1;j<=5001;j++) wynik=(wynik+Dp[n%2][j])%q; cout<<wynik<<"\n"; 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 | #include <bits/stdc++.h> using namespace std; const int q=1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<int>V(n+1,0); for(int i=1;i<=n;i++) cin>>V[i]; sort(V.begin(),V.end()); vector<vector<long long>>Dp(2,vector<long long>(5002,0)); Dp[0][0]=1; Dp[1][0]=1; for(int i=0;i<n;i++) { int a=V[i+1]; for(int j=5001;j>=0;j--) { Dp[(i+1)%2][j]=Dp[i%2][j]; if(j>=a-1) Dp[(i+1)%2][min(j+a,5001)]+=Dp[i%2][j]; if(Dp[(i+1)%2][j]>=q) Dp[(i+1)%2][j]%=q; } } long long wynik=0; for(int j=1;j<=5001;j++) wynik=(wynik+Dp[n%2][j])%q; cout<<wynik<<"\n"; return 0; } |