#include <iostream> const int maxN = 300 * 1000 + 20; const long long mod = 1000 * 1000 * 1000 + 7; const long long unsolved = -1; int n; long long a[maxN]; long long solution[maxN]; //number of solution to truncated problem void start(){ for(int i = 0; i < maxN; i++) solution[i] = unsolved; } long long solve(int ind){ if(solution[ind] != unsolved) return solution[ind]; long long answer = 0; long long sum = 0; for(int i = ind; i < n; i++){ sum += a[i]; sum %= mod; if(sum % 2 == 1) continue; if(i == n - 1) answer += 1; else answer += solve(i + 1); answer %= mod; } solution[ind] = answer; return answer; } int main(){ std::cin >> n; for(int i = 0; i < n; i++) std::cin >> a[i]; start(); std::cout << solve(0) << "\n"; 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 | #include <iostream> const int maxN = 300 * 1000 + 20; const long long mod = 1000 * 1000 * 1000 + 7; const long long unsolved = -1; int n; long long a[maxN]; long long solution[maxN]; //number of solution to truncated problem void start(){ for(int i = 0; i < maxN; i++) solution[i] = unsolved; } long long solve(int ind){ if(solution[ind] != unsolved) return solution[ind]; long long answer = 0; long long sum = 0; for(int i = ind; i < n; i++){ sum += a[i]; sum %= mod; if(sum % 2 == 1) continue; if(i == n - 1) answer += 1; else answer += solve(i + 1); answer %= mod; } solution[ind] = answer; return answer; } int main(){ std::cin >> n; for(int i = 0; i < n; i++) std::cin >> a[i]; start(); std::cout << solve(0) << "\n"; return 0; } |