#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX = 300005; const LL P = 1e9+7; LL a[MAX]; LL dp[MAX]; int main() { ios_base::sync_with_stdio(false); int n = 0; cin >> n; LL totalSum = 0; for(int i = 0; i < n; ++i) { cin >> a[i]; totalSum += a[i]; } if(totalSum < P && n > 5000) { LL p = 0, np = 0, res = 0; totalSum = 0; for(int i = 0; i < n; ++i) { if(a[i]%2 == 0) { res = p; p = (p+p)%P; } else { res = np; LL tmp = p; p = np; np = tmp; p = (p+p)%P; } totalSum = (totalSum+a[i])%P; if(totalSum%2 == 0) { p = (p+1)%P; ++res; } } cout << res << endl; } else { for(int i = 0; i < n; ++i) { LL s = 0; LL w = 0; for(int k = i; k >= 0; --k) { s += a[k]; s %= P; if(s % 2 == 0) { if(k == 0) w = (w+1)%P; else w = (w + dp[k-1])%P; } } dp[i] = w; } cout << dp[n-1] << endl; } }
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 <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX = 300005; const LL P = 1e9+7; LL a[MAX]; LL dp[MAX]; int main() { ios_base::sync_with_stdio(false); int n = 0; cin >> n; LL totalSum = 0; for(int i = 0; i < n; ++i) { cin >> a[i]; totalSum += a[i]; } if(totalSum < P && n > 5000) { LL p = 0, np = 0, res = 0; totalSum = 0; for(int i = 0; i < n; ++i) { if(a[i]%2 == 0) { res = p; p = (p+p)%P; } else { res = np; LL tmp = p; p = np; np = tmp; p = (p+p)%P; } totalSum = (totalSum+a[i])%P; if(totalSum%2 == 0) { p = (p+1)%P; ++res; } } cout << res << endl; } else { for(int i = 0; i < n; ++i) { LL s = 0; LL w = 0; for(int k = i; k >= 0; --k) { s += a[k]; s %= P; if(s % 2 == 0) { if(k == 0) w = (w+1)%P; else w = (w + dp[k-1])%P; } } dp[i] = w; } cout << dp[n-1] << endl; } } |