You are viewing a single comment's thread. Return to all comments →
O(n)
long summingPieces(vector<long> A) { long mod = 1000000007; vector<long> twoPower = { 1 }; for (int i = 1; i <= A.size(); i++) twoPower.push_back((2 * twoPower.back()) % mod); vector<vector<long>> storage = {{A[0], A[0], A[0]}}; long L = A[0], N, P, Q; for (int i = 1; i < A.size(); i++) { P = (storage[i-1][1] + twoPower[i] * A[i]) % mod; Q = (storage[i-1][2] + storage[i-1][1] + (twoPower[i+1] - 1) * A[i]) % mod; N = (L + Q) % mod; L = (L + N) % mod; storage.push_back({N, P, Q}); } return storage.back()[0]; }
Seems like cookies are disabled on this browser, please enable them to open this website
Summing Pieces
You are viewing a single comment's thread. Return to all comments →
O(n)