#include <cassert> #include <cstdio> typedef long long LL; const int MAXN = 300000 + 1; const int MOD = 1000000007; int n; int a[MAXN]; LL sum[MAXN]; LL dp[MAXN]; LL do_real() { for (int i = 0; i <= n; i++) { sum[i] = 0; dp[i] = 0; } dp[0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) { sum[j] = (sum[j] + a[i]) % MOD; } for (int j = 0; j < i; j++) { if (sum[j] % 2 == 0) { dp[i] = (dp[i] + dp[j]) % MOD; } } } return dp[n]; } LL dp2[MAXN][2]; LL do_small() { dp2[1][0] = 1 - (a[1] % 2); dp2[1][1] = a[1] % 2; for (int i = 2; i <= n; i++) { if (a[i] % 2 == 1) { dp2[i][0] = dp2[i - 1][1]; dp2[i][1] = 2 * dp2[i - 1][0]; } else { dp2[i][0] = 2 * dp2[i - 1][0]; dp2[i][1] = dp2[i - 1][1]; } } return dp2[n][0]; } int main() { scanf("%d", &n); LL sum = 0; bool small = true; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; if (sum >= MOD) small = false; } printf("%lld\n", small ? do_small() : do_real()); }
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 66 67 68 | #include <cassert> #include <cstdio> typedef long long LL; const int MAXN = 300000 + 1; const int MOD = 1000000007; int n; int a[MAXN]; LL sum[MAXN]; LL dp[MAXN]; LL do_real() { for (int i = 0; i <= n; i++) { sum[i] = 0; dp[i] = 0; } dp[0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) { sum[j] = (sum[j] + a[i]) % MOD; } for (int j = 0; j < i; j++) { if (sum[j] % 2 == 0) { dp[i] = (dp[i] + dp[j]) % MOD; } } } return dp[n]; } LL dp2[MAXN][2]; LL do_small() { dp2[1][0] = 1 - (a[1] % 2); dp2[1][1] = a[1] % 2; for (int i = 2; i <= n; i++) { if (a[i] % 2 == 1) { dp2[i][0] = dp2[i - 1][1]; dp2[i][1] = 2 * dp2[i - 1][0]; } else { dp2[i][0] = 2 * dp2[i - 1][0]; dp2[i][1] = dp2[i - 1][1]; } } return dp2[n][0]; } int main() { scanf("%d", &n); LL sum = 0; bool small = true; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; if (sum >= MOD) small = false; } printf("%lld\n", small ? do_small() : do_real()); } |