#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
constexpr int M=5007;
constexpr int mod=1e9+7;
int t[M];
bool legal[M];
int dp[M];
int dp2[M];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long res=0;
int n, a, b;
cin >> n;
for(int i=0; i<n; i++) cin >> t[i];
sort(t, t+n);
dp[0]=1;
legal[0]=1;
//cout << '\n';
//for(int j=1; j<=20; j++) cout << dp[j] << ' ';
//cout << '\n';
for(int i=0; i<n; i++){
a = t[i];
for(int j=5001; j>=a-1; j--){
b = j + a;
if(b>5001) b = 5001;
if(legal[j]) legal[b] = 1;
dp2[b] += dp[j];
if(dp2[b] >= mod) dp2[b]-=mod;
}
for(int j=0; j<=5001; j++){
dp[j] += dp2[j];
if(dp[j]>=mod) dp[j]-=mod;
dp2[j] = 0;
}
//cout << '\n';
//for(int j=1; j<=20; j++) cout << dp[j] << ' ';
//cout << '\n';
}
for(int i=1; i<=5001; i++){
res += dp[i];
if(res>=mod) res-=mod;
}
cout << res;
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; constexpr int M=5007; constexpr int mod=1e9+7; int t[M]; bool legal[M]; int dp[M]; int dp2[M]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long res=0; int n, a, b; cin >> n; for(int i=0; i<n; i++) cin >> t[i]; sort(t, t+n); dp[0]=1; legal[0]=1; //cout << '\n'; //for(int j=1; j<=20; j++) cout << dp[j] << ' '; //cout << '\n'; for(int i=0; i<n; i++){ a = t[i]; for(int j=5001; j>=a-1; j--){ b = j + a; if(b>5001) b = 5001; if(legal[j]) legal[b] = 1; dp2[b] += dp[j]; if(dp2[b] >= mod) dp2[b]-=mod; } for(int j=0; j<=5001; j++){ dp[j] += dp2[j]; if(dp[j]>=mod) dp[j]-=mod; dp2[j] = 0; } //cout << '\n'; //for(int j=1; j<=20; j++) cout << dp[j] << ' '; //cout << '\n'; } for(int i=1; i<=5001; i++){ res += dp[i]; if(res>=mod) res-=mod; } cout << res; return 0; } |
English