#include <iostream> #include <vector> #include <algorithm> #include <string> #define MAX 1000009 #define MOD 1000000007 long long a[MAX], pref[MAX], result[MAX]; long long smallHeuristic(int n) { pref[0] = a[0]; for (int i=1; i<n; i++) { pref[i] = pref[i-1] + a[i]; } if (pref[n-1] % 2) { return 0; } int divisionPoints = 0; for (int i=0; i<n-1; i++) { if (pref[i] % 2 == 0) divisionPoints++; } long long res = 1; while (divisionPoints--) res *= 2; return res; } long long quadHeuristic(int n) { result[0] = 1; for (int i=0; i<n; i++) { long long sum = a[i]; for (int j=i; j>=0; j--) { if (sum % 2 == 0) { result[i+1] = (result[j] + result[i+1]) % MOD; } if (j) sum = (sum + a[j-1]) % MOD; } } return result[n]; } long long sol(int n) { return quadHeuristic(n); } int main() { int n; std::cin >> n; long long sum = 0; for (int i=0; i<n; i++) { std::cin >> a[i]; sum += a[i]; } long long result; if (n <= 3000) { result = quadHeuristic(n); } else if (sum < MOD) { result = smallHeuristic(n); } else { result = sol(n); } std::cout << result; }
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 69 70 71 72 | #include <iostream> #include <vector> #include <algorithm> #include <string> #define MAX 1000009 #define MOD 1000000007 long long a[MAX], pref[MAX], result[MAX]; long long smallHeuristic(int n) { pref[0] = a[0]; for (int i=1; i<n; i++) { pref[i] = pref[i-1] + a[i]; } if (pref[n-1] % 2) { return 0; } int divisionPoints = 0; for (int i=0; i<n-1; i++) { if (pref[i] % 2 == 0) divisionPoints++; } long long res = 1; while (divisionPoints--) res *= 2; return res; } long long quadHeuristic(int n) { result[0] = 1; for (int i=0; i<n; i++) { long long sum = a[i]; for (int j=i; j>=0; j--) { if (sum % 2 == 0) { result[i+1] = (result[j] + result[i+1]) % MOD; } if (j) sum = (sum + a[j-1]) % MOD; } } return result[n]; } long long sol(int n) { return quadHeuristic(n); } int main() { int n; std::cin >> n; long long sum = 0; for (int i=0; i<n; i++) { std::cin >> a[i]; sum += a[i]; } long long result; if (n <= 3000) { result = quadHeuristic(n); } else if (sum < MOD) { result = smallHeuristic(n); } else { result = sol(n); } std::cout << result; } |