Source Code
#include <bits/stdc++.h>
using namespace std;
const int N = 8e6 + 9;
const int mod = 1e9 + 7;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    string s;
    cin >> s;
    int n = s.size();
    int pre[4][n + 1];
    memset(pre, 0 , sizeof pre);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < 4; j++){
            pre[j][i + 1] = pre[j][i];
        }

        if(s[i] == '1') pre[i % 4][i + 1]++;
    }
    int q;
    cin >> q;
    while(q--){
        int l , r;
        cin >> l >> r;
        int ans = 0;
        if(s[r - 1] == '1') ans++;
        ans += (pre[0][r - 1] - pre[0][l - 1]) * 2;
        ans %= 10;
        ans += (pre[1][r - 1] - pre[1][l - 1]) * 4;
        ans %= 10;
        ans += (pre[2][r - 1] - pre[2][l - 1]) * 8;
        ans %= 10;
        ans += (pre[3][r - 1] - pre[3][l - 1]) * 6;
        ans %= 10;
        cout << ans << "\n";  
    }
    return 0;
}  
Copy
Binarithm AbduSaber
GNU G++17
1 ms
184 KB
Wrong Answer