#include <bits/stdc++.h> using namespace std; int P = 1000000007; void solve1(int n, const vector<int> &a, int sa) { if (sa%2 != 0) { cout << 0 << endl; return; } int dp=0,dps=0; int s = 0; for (int i=1;i<n+1;i++) { s += a[i]; if (s%2==0) { dp = (dps + 1)%P; dps = (dps + dp)%P; //cout << dp << " " << dps << endl; } } cout << dp << endl; } void solve2(int n, const vector<int> &a) { vector<int> dp(n+1); dp[0]=1; for (int i=1;i<n+1;i++) { int s=0,s2=0; //cout << "i="<<i<<endl; for (int j=i;j>0;j--) { s = (s+a[j])%P; //cout << " s="<< s << endl; if (s%2==0) s2 = (s2+dp[j-1])%P; } dp[i] = s2; } //for (int i=1;i<n+1;i++) // cout << dp[i] << " "; cout << dp[n] << endl; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n+1); long long sa = 0; for (int i=1;i<n+1;i++) { cin >> a[i]; sa += a[i]; } if (sa<P) solve1(n,a,sa); else solve2(n,a); 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 | #include <bits/stdc++.h> using namespace std; int P = 1000000007; void solve1(int n, const vector<int> &a, int sa) { if (sa%2 != 0) { cout << 0 << endl; return; } int dp=0,dps=0; int s = 0; for (int i=1;i<n+1;i++) { s += a[i]; if (s%2==0) { dp = (dps + 1)%P; dps = (dps + dp)%P; //cout << dp << " " << dps << endl; } } cout << dp << endl; } void solve2(int n, const vector<int> &a) { vector<int> dp(n+1); dp[0]=1; for (int i=1;i<n+1;i++) { int s=0,s2=0; //cout << "i="<<i<<endl; for (int j=i;j>0;j--) { s = (s+a[j])%P; //cout << " s="<< s << endl; if (s%2==0) s2 = (s2+dp[j-1])%P; } dp[i] = s2; } //for (int i=1;i<n+1;i++) // cout << dp[i] << " "; cout << dp[n] << endl; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n+1); long long sa = 0; for (int i=1;i<n+1;i++) { cin >> a[i]; sa += a[i]; } if (sa<P) solve1(n,a,sa); else solve2(n,a); return 0; } |