#include <bits/stdc++.h> using namespace std; const int MAX = 3e5 + 7; const int MOD = 1e9 + 7; typedef long long ll; ll dp[MAX], pref[MAX]; int solve_1(int n){ ll sum = 1; for(int i = 1; i <= n; i++){ if(!(pref[i]&1)){ dp[i] = sum; sum <<= 1; sum %= MOD; } } return dp[n]; } int solve_2(int n){ dp[0] = 1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= i; j++){ if(((pref[i] - pref[j-1]+MOD)%MOD)%2 == 0) { dp[i] += dp[j-1]; dp[i] %= MOD; } } } return dp[n]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1, a; i <= n; i++){ cin >> a; pref[i] = (pref[i-1] + a)%MOD; } if(n <= 3e4) cout << solve_2(n); else cout << solve_1(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 | #include <bits/stdc++.h> using namespace std; const int MAX = 3e5 + 7; const int MOD = 1e9 + 7; typedef long long ll; ll dp[MAX], pref[MAX]; int solve_1(int n){ ll sum = 1; for(int i = 1; i <= n; i++){ if(!(pref[i]&1)){ dp[i] = sum; sum <<= 1; sum %= MOD; } } return dp[n]; } int solve_2(int n){ dp[0] = 1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= i; j++){ if(((pref[i] - pref[j-1]+MOD)%MOD)%2 == 0) { dp[i] += dp[j-1]; dp[i] %= MOD; } } } return dp[n]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1, a; i <= n; i++){ cin >> a; pref[i] = (pref[i-1] + a)%MOD; } if(n <= 3e4) cout << solve_2(n); else cout << solve_1(n); return 0; } |