#include <cstdio> #include <cmath> const unsigned MAX = 300000; const unsigned PPP = 1000000007; int n; unsigned nums[MAX]; unsigned getSum(int i, int j) { if(i == 0) return nums[j]; --i; unsigned s = nums[j] < nums[i] ? nums[j] + PPP : nums[j]; unsigned diff = s - nums[i]; unsigned ret = s - diff; return ret; } void countRanges(int start, int len, unsigned *ret) { if (start + len == n) { unsigned sum = getSum(start, n) % PPP; if ((sum & 1) == 0) { ++(*ret); if (*ret > PPP) *ret -= PPP; } return; } unsigned sum = getSum(start, len + start) % PPP; if ((sum & 1) == 1) { return; } for (int i = 1; i < (n - start - len + 1); ++i) { countRanges(start+len, i, ret); } } int main() { scanf("%d", &n); unsigned sum = 0; for (int i = 0; i < n; ++i) { unsigned num; scanf("%u", &num); sum += num; if (sum >= PPP) sum -= PPP; nums[i] = sum; } unsigned ret = 0; for(int i = 1; i <= n; ++i) { countRanges(0, i, &ret); } printf("%d\n", ret); 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 | #include <cstdio> #include <cmath> const unsigned MAX = 300000; const unsigned PPP = 1000000007; int n; unsigned nums[MAX]; unsigned getSum(int i, int j) { if(i == 0) return nums[j]; --i; unsigned s = nums[j] < nums[i] ? nums[j] + PPP : nums[j]; unsigned diff = s - nums[i]; unsigned ret = s - diff; return ret; } void countRanges(int start, int len, unsigned *ret) { if (start + len == n) { unsigned sum = getSum(start, n) % PPP; if ((sum & 1) == 0) { ++(*ret); if (*ret > PPP) *ret -= PPP; } return; } unsigned sum = getSum(start, len + start) % PPP; if ((sum & 1) == 1) { return; } for (int i = 1; i < (n - start - len + 1); ++i) { countRanges(start+len, i, ret); } } int main() { scanf("%d", &n); unsigned sum = 0; for (int i = 0; i < n; ++i) { unsigned num; scanf("%u", &num); sum += num; if (sum >= PPP) sum -= PPP; nums[i] = sum; } unsigned ret = 0; for(int i = 1; i <= n; ++i) { countRanges(0, i, &ret); } printf("%d\n", ret); return 0; } |