Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fixed(n) cout << fixed << setprecision(n);
#define mod 1000000007
#define cin(v) for (auto&i:v) cin >> i;
#define cout(v) for (auto&i:v) cout << i << " ";
//#define ceil(n,m) (n / m + (n%m != 0))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void lil_codi_vert(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif
}

//int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
//int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

ll fastpow(ll b, ll e, ll mo){
    ll res = 1;
    while(e){
        if(e&1) res = ((res%mo) * (b%mo)) % mo;
        e /= 2;
        b = ((b%mo) * (b%mo)) % mo;
    }
    return res % mo;
}


int main(){
    lil_codi_vert();
    string s; cin >> s;
    ll n = s.size();
    vector<ll> pre(n+1);
    for(int i = 1; i <= n; i++){
        pre[i] = pre[i-1] * 2;
        if(s[i-1] == '1')
            pre[i]++;
        pre[i] %= 10;
        
    }

    cout (pre);
    cout << "\n";
    
    
    
    ll q; cin >> q;
    while(q--){
        ll l, r; cin >> l >> r;
        ll sm = (pre[l-1] * fastpow(2ll, (r-l), 10)) % 10;
        cout << (pre[r] - sm + 10) % 10 << "\n";   
    }
    
     
    return 0;
}
Copy
Binarithm aboelsoudJr
GNU G++17
1 ms
244 KB
Wrong Answer