• + 0 comments

    O(n)

    unsigned long hackerrankCity(vector<int> A) {
        int mod = 1000000007;
        unsigned long P = 1, L = 0, D = 0, H = 0;
        for (int i=1; i <= A.size(); i++) {
            H = (4*H + 12*P*D + 16*((P*P) % mod)*A[i-1] + 8*D + 12*P*A[i-1] + A[i-1]) % mod;
            D = (3*P*L + 8*P*A[i-1] + 4*D + 2*L + 3*A[i-1]) % mod;
            P = (4*P + 2) % mod;
            L = (2*L + 3*A[i-1]) % mod;
        }
        return H;
    }