1018. Binary Prefix Divisible By 5

1018. Binary Prefix Divisible By 5

Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.)

Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5

vector<bool> prefixesDivBy5(vector<int>& A) {
    int len=A.size();
    int n=A[0];
    vector<bool> res(len, false);
    if (n==0) res[0]=true;
    for(int i=1;i<len;++i) {
        n=(n<<1)|A[i];
        n%=5;	// 整除关注余数
        res[i]=!n;
    }
    return res;
}

#math