Source Code
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <map>
#define ll long long
#define endl '\n'
#define Pi 3.1415926535898
const int N = 2e6 + 5;
#define watch(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(),(v).end()
using namespace std;
// cout << fixed << setprecision(9);
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
}
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; // dir array
int dy[] = { 1, -1, 0, 0, 1, -1, -1, 1 };

int main() {
	fast();
	file();
	// cout << (1 << 5); 
	//bitset<32> bits("01110101");
	// cout << bits.to_ulong() << endl;
	string s; 
	cin >> s; 
	int sz = s.size(); 
	vector <int> v(sz + 1); 
	v[0] = 0;
	for (int i = 0;i < sz;i++) {
		if (s[i] == '0') {
			v[i+1] = (v[i] * 2) % 10 ; 
		}
		else {
			v[i+1] = ((v[i] * 2) % 10) +1 ; 
		}
	}
	//for (auto it : v) cout << it << " "; 
	int q; 
	cin >> q; 
	while (q--) {
		int l, r; 
		cin >> l >> r; 
		if (l==1)
	    	cout << v[r] << endl;
		else {
			ll dis = r - l + 1; 
			ll x = v[l - 1] * (1 << dis); 
			cout << abs(x % 10 - v[r]) << endl;
		}
	}
	return 0;
}
Copy
Binarithm Marah Mohamed
GNU G++17
52 ms
1.7 MB
Wrong Answer