#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <numeric>
using namespace std;
using ull = unsigned long long;
#ifdef GGDEBUG
#define dbg printf
#else
#define dbg //dbg
#endif
int p = 1000000000 + 7;
// logarithmic power
ull power_of_two(int pwr) {
if (pwr == 0) return 1;
if (pwr % 2 == 0) {
ull tmp = power_of_two(pwr / 2);
tmp *= tmp;
tmp %= p;
return tmp;
}
ull tmp = 2 * power_of_two(pwr - 1);
tmp %= p;
return tmp;
}
int main() {
int n;
scanf("%d", &n);
vector<int> in;
ull sum_of_all = 0;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
in.push_back(x);
sum_of_all += x;
}
if (sum_of_all < p) {
// find number of separate groups
ull sum = 0;
int groups = 0;
for (int i = 0; i < n; i++) {
dbg("%d ", in[i]);
}
dbg("\n");
for (int i = 0; i < n; i++) {
sum += in[i];
if (sum % 2 == 0) {
groups++;
sum = 0;
}
}
if (sum != 0) {
printf("0\n");
return 0;
}
dbg("groups: %d\n", groups);
cout << power_of_two(groups-1) << endl;
return 0;
} else {
vector<int> poss;
poss.resize(n+1, 0);
poss[n] = 1;
for (int i = n-1; i >= 0; i--) {
// dbg("%d ", in[i]);
ull sum = 0;
for (int j = i; j < n; j++) {
sum += in[j];
sum %= p;
if ((sum % p) % 2 == 0) {
poss[i] += poss[j+1];
poss[i] %= p;
}
}
}
for (int i = 0; i < n; i++) {
dbg("%d ", poss[i]);
}
dbg("\n");
cout << poss[0] << endl;
}
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 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 | #include <iostream> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <map> #include <numeric> using namespace std; using ull = unsigned long long; #ifdef GGDEBUG #define dbg printf #else #define dbg //dbg #endif int p = 1000000000 + 7; // logarithmic power ull power_of_two(int pwr) { if (pwr == 0) return 1; if (pwr % 2 == 0) { ull tmp = power_of_two(pwr / 2); tmp *= tmp; tmp %= p; return tmp; } ull tmp = 2 * power_of_two(pwr - 1); tmp %= p; return tmp; } int main() { int n; scanf("%d", &n); vector<int> in; ull sum_of_all = 0; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); in.push_back(x); sum_of_all += x; } if (sum_of_all < p) { // find number of separate groups ull sum = 0; int groups = 0; for (int i = 0; i < n; i++) { dbg("%d ", in[i]); } dbg("\n"); for (int i = 0; i < n; i++) { sum += in[i]; if (sum % 2 == 0) { groups++; sum = 0; } } if (sum != 0) { printf("0\n"); return 0; } dbg("groups: %d\n", groups); cout << power_of_two(groups-1) << endl; return 0; } else { vector<int> poss; poss.resize(n+1, 0); poss[n] = 1; for (int i = n-1; i >= 0; i--) { // dbg("%d ", in[i]); ull sum = 0; for (int j = i; j < n; j++) { sum += in[j]; sum %= p; if ((sum % p) % 2 == 0) { poss[i] += poss[j+1]; poss[i] %= p; } } } for (int i = 0; i < n; i++) { dbg("%d ", poss[i]); } dbg("\n"); cout << poss[0] << endl; } return 0; } |
English