Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define EPS 1e-6
#define Ceil(n, m) ((n / m) + ( n % m ? 1 : 0))
#define mod 1000000007
#define over 1e18
#define cin(v) for(auto &i: v)  cin>>i
#define cout(v) for(auto &i: v) cout<<i<<" ";
#define all(v) v.begin(), v.end()
#define LSB(n) (n & -n)
#define OO 2'000'000'000
void fcode(){
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
   /* #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout) ,  freopen("error.txt", "w", stderr);
    #endif*/
}
ll power(ll x, ll y){ 
    ll temp; 
    if(y == 0) 
        return 1; 
    temp = (power(x, y / 2) % 10); 
    if (y % 2 == 0) 
        return ((temp % 10) * (temp % 10) % 10); 
    else{ 
        if(y > 0) 
            return ((x % 10)* temp % 10 * temp % 10) % 10; 
        else
            return (((temp % 10) * (temp % 10)) / x) % 10; 
    } 
} 
int main(){
    fcode();
    string s;    cin>>s;
    vector<ll>pre(s.size()+1);
    for(int i = 0; i<s.size(); i++){
      if(s[i] == '1')
        pre[i+1] = (((pre[i] % 10)* 2)+ 1) % 10;
      else 
        pre[i+1] = ((pre[i] % 10) * 2) % 10;
      pre[i] %= 10;
      pre[i+1] %= 10;
    }
    ll q;    cin>>q;
    while(q--){
      ll l, r;    cin>>l>>r;
      ll num = (ll)(pre[r] - (pre[l - 1] * power(2, r - l + 1)))% 10;
      if(num < 0)
       num += 10;
     cout<<num<<"\n";
    }
  // cout(pre);
}
Copy
Binarithm Hodakamal
GNU G++17
157 ms
2.7 MB
Accepted