#include <iostream> #include <vector> using namespace std; const int P = 1e9 + 7; void brut3000(const vector<int>& nums, vector<int>& dp, int start, int end) { for(int i = start; i < end; ++i) { int sum = 0; for(int j = i; j >= 0; --j) { sum = (sum + nums[j]) % P; if(sum % 2 == 0) { dp[i] = (dp[i] + (j > 0 ? dp[j - 1] : 1)) % P; } } } } void brut100(vector<int>& nums, vector<int>& dp, int end) { int lastOne = -1; dp[0] = 1 - (nums[0] % 2); if(dp[0] == 0) { lastOne = 0; } for(int i = 1; i < end; ++i) { if(nums[i] % 2 == 0) { dp[i] = (2 * dp[i - 1]) % P; } else { if(lastOne == 0) { dp[i] = 1; } else if(lastOne > 0) { dp[i] = (2 * dp[lastOne - 1]) % P; } lastOne = i; } } } pair<int, int> getLimit1(vector<int>& nums, int n) { pair<int, int> res(-1, 0); for(int i = 0; i < n; ++i) { res.second = (res.second + nums[i]) % P; if(res.second % 2 == 0) { res.first = i; break; } } return res; } pair<int, int> getLimit2(vector<int>& nums, int n) { pair<int, int> res(-1, 0); for(int i = n - 1; i >= 0; --i) { res.second = (res.second + nums[i]) % P; if(res.second % 2 == 0) { res.first = i; break; } } return res; } int getEnd(const vector<int>& nums, int n) { int sum = 0; for(int i = 0; i < n; ++i) { sum += nums[i]; if(sum >= P) { return i; } } return n; } int allGood(vector<int>& nums, int n) { int sum = 0; for(int i = 0; i < n; ++i) { sum = (sum + nums[i]) % P; } if(sum % 2 == 0) { return 1; } return -1; } vector<int> compress(vector<int>& nums, int n, int & status) { vector<int> res; pair<int, int> limit1 = getLimit1(nums, n), limit2 = getLimit2(nums, n); if(limit1.first == -1 || limit2.first == -1) { status = -1; return res; } if(limit1.first >= limit2.first) { status = allGood(nums, n); return res; } nums[limit1.first] = limit1.second, nums[limit2.first] = limit2.second; for(int i = limit1.first; i <= limit2.first; ++i) { res.push_back(nums[i]); } status = 0; return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> nums(n); for(int i = 0; i < n; ++i) { cin >> nums[i]; } int status; vector<int> compressed = compress(nums, n, status); if(status == -1) { cout << "0\n"; return 0; } if(status == 1) { cout << "1\n"; return 0; } n = compressed.size(); vector<int> dp(n), nums2(n); for(int i = 0; i < n; ++i) { nums2[i] = compressed[n - i - 1]; } int end1 = getEnd(compressed, n), end2 = getEnd(nums2, n); if(end1 >= end2) { brut100(compressed, dp, end1); brut3000(compressed, dp, end1, n); } else { brut100(nums2, dp, end2); brut3000(nums2, dp, end2, n); } cout << dp[n - 1] << "\n"; }
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include <iostream> #include <vector> using namespace std; const int P = 1e9 + 7; void brut3000(const vector<int>& nums, vector<int>& dp, int start, int end) { for(int i = start; i < end; ++i) { int sum = 0; for(int j = i; j >= 0; --j) { sum = (sum + nums[j]) % P; if(sum % 2 == 0) { dp[i] = (dp[i] + (j > 0 ? dp[j - 1] : 1)) % P; } } } } void brut100(vector<int>& nums, vector<int>& dp, int end) { int lastOne = -1; dp[0] = 1 - (nums[0] % 2); if(dp[0] == 0) { lastOne = 0; } for(int i = 1; i < end; ++i) { if(nums[i] % 2 == 0) { dp[i] = (2 * dp[i - 1]) % P; } else { if(lastOne == 0) { dp[i] = 1; } else if(lastOne > 0) { dp[i] = (2 * dp[lastOne - 1]) % P; } lastOne = i; } } } pair<int, int> getLimit1(vector<int>& nums, int n) { pair<int, int> res(-1, 0); for(int i = 0; i < n; ++i) { res.second = (res.second + nums[i]) % P; if(res.second % 2 == 0) { res.first = i; break; } } return res; } pair<int, int> getLimit2(vector<int>& nums, int n) { pair<int, int> res(-1, 0); for(int i = n - 1; i >= 0; --i) { res.second = (res.second + nums[i]) % P; if(res.second % 2 == 0) { res.first = i; break; } } return res; } int getEnd(const vector<int>& nums, int n) { int sum = 0; for(int i = 0; i < n; ++i) { sum += nums[i]; if(sum >= P) { return i; } } return n; } int allGood(vector<int>& nums, int n) { int sum = 0; for(int i = 0; i < n; ++i) { sum = (sum + nums[i]) % P; } if(sum % 2 == 0) { return 1; } return -1; } vector<int> compress(vector<int>& nums, int n, int & status) { vector<int> res; pair<int, int> limit1 = getLimit1(nums, n), limit2 = getLimit2(nums, n); if(limit1.first == -1 || limit2.first == -1) { status = -1; return res; } if(limit1.first >= limit2.first) { status = allGood(nums, n); return res; } nums[limit1.first] = limit1.second, nums[limit2.first] = limit2.second; for(int i = limit1.first; i <= limit2.first; ++i) { res.push_back(nums[i]); } status = 0; return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> nums(n); for(int i = 0; i < n; ++i) { cin >> nums[i]; } int status; vector<int> compressed = compress(nums, n, status); if(status == -1) { cout << "0\n"; return 0; } if(status == 1) { cout << "1\n"; return 0; } n = compressed.size(); vector<int> dp(n), nums2(n); for(int i = 0; i < n; ++i) { nums2[i] = compressed[n - i - 1]; } int end1 = getEnd(compressed, n), end2 = getEnd(nums2, n); if(end1 >= end2) { brut100(compressed, dp, end1); brut3000(compressed, dp, end1, n); } else { brut100(nums2, dp, end2); brut3000(nums2, dp, end2, n); } cout << dp[n - 1] << "\n"; } |