Source Code
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <stdio.h>
#define ll long long
#define mem(a,n) memset((a),n,sizeof (a))
void fast();
ll gcd(ll x, ll y) { return(!y ? x : gcd(y, x % y)); }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
int dx[]{ 1, -1, 0, 0, 1, 1, -1, -1 };
int dy[]{ 0, 0, 1, -1, 1, -1, 1, -1 };
using namespace std;
ll fpow(ll x, ll n, int mod) {
    if (n == 0)return 1 % mod;
    if (n == 1)return x % mod;
    ll ans = fpow(x, n / 2, mod);
    ans = ans * ans % mod;
    if (n & 1)ans = ans * (x % mod) % mod;
    return ans;
}
int main()
{
    //freopen("input.txt" ,"r" ,stdin);
   //freopen("output.txt" ,"w" ,stdout);
    fast();
    string s; cin >> s;
    vector<int>temp(s.size()+1);
    int temp2 = 0;
    for (int i = 0; i<s.size(); i++) {
        temp2 *= 2;
        if (s[i] == '1')temp2++;
        temp2 %= 10;
        temp[i+1] = temp2;
    }
    int q; cin >> q;
    int l, r;
    while (q--) {
        cin >> l >> r;
        int ans = temp[r];
        if (l - 1 > 0) {
            temp2 = temp[l - 1];
            temp2 *= fpow(2, r - l + 1, 10);
            temp2 %= 10;
            ans -= temp2;
            ans = ((ans % 10) + 10) % 10;
        }
        cout << ans << "\n";
    }

    return 0;
}
void fast() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
Copy
Binarithm Nourhan Hanna
GNU G++17
216 ms
1.9 MB
Accepted