#include<bits/stdc++.h> using namespace std; const long long mod = (long long)1e9 + 7; vector<long long> V,P,DP; long long sum(int a, int b) { return (P[b] - P[a-1] + mod)%mod; } int main() { bool duzasuma=0; int n; cin>>n; V.resize(n+1); P.resize(n+1); DP.resize(n+1,0); P[0]=0; long long suma=0; for(int i=1;i<=n;i++) { cin>>V[i]; suma += V[i]; if(suma >= mod) duzasuma=1; P[i] = (P[i-1]+V[i])%mod; } if(duzasuma) { DP[0] = 1; for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { if((sum(j,i)%mod)%2 == 0) { DP[i] += DP[j-1]; DP[i] %= mod; } } } } else { long long dpparz=0, dpnieparz=0; DP[0] = 1; for(int i=1;i<=n;i++) { if(V[i]%2==0) { dpparz += DP[i-1]; dpparz %= mod; DP[i] = dpparz; } else { swap(dpparz, dpnieparz); dpnieparz += DP[i-1]; dpnieparz %= mod; DP[i] = dpparz; } } } cout<<DP[n]<<"\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 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 | #include<bits/stdc++.h> using namespace std; const long long mod = (long long)1e9 + 7; vector<long long> V,P,DP; long long sum(int a, int b) { return (P[b] - P[a-1] + mod)%mod; } int main() { bool duzasuma=0; int n; cin>>n; V.resize(n+1); P.resize(n+1); DP.resize(n+1,0); P[0]=0; long long suma=0; for(int i=1;i<=n;i++) { cin>>V[i]; suma += V[i]; if(suma >= mod) duzasuma=1; P[i] = (P[i-1]+V[i])%mod; } if(duzasuma) { DP[0] = 1; for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { if((sum(j,i)%mod)%2 == 0) { DP[i] += DP[j-1]; DP[i] %= mod; } } } } else { long long dpparz=0, dpnieparz=0; DP[0] = 1; for(int i=1;i<=n;i++) { if(V[i]%2==0) { dpparz += DP[i-1]; dpparz %= mod; DP[i] = dpparz; } else { swap(dpparz, dpnieparz); dpnieparz += DP[i-1]; dpnieparz %= mod; DP[i] = dpparz; } } } cout<<DP[n]<<"\n"; return 0; } |