Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define cin(v) for(auto&x:v) cin >> x;
- #define cout(v) for(auto&x:v) cout << x << " ";
- void files(){
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- const ll mod = 1e9 + 7;
- void solve(){
- int n , q , l , r;
- string s ;
- cin >> n >> q >> s;
- vector < ll > pref(n) , suff(n);
- for(int i =0 ; i < n ; i++){
- pref[i] = (s[i] - '0');
- if(i){
- pref[i] = pref[i-1] * 10 + pref[i];
- pref[i] %= mod;
- }
- // cout << pref[i] << nl;
- }
- string t= s;
- reverse(all(t));
- for(int i = n-1 ; i >=0 ; i--){
- suff[i] = (t[i] - '0');
- if(i != n-1){
- suff[i] = suff[i] * pow(10 , sz(to_string(suff[i+1]))) + suff[i+1];
- suff[i] %= mod;
- }
- // cout << suff[i] << nl;
- }
- while(q--){
- cin >> l >> r;
- l-- , r--;
- ll ans= 0;
- ll pw = 1, temp =0 ;
- if(l) ans = pref[l-1] , temp = suff[n-l] , pw = pow(10 , sz(to_string(suff[n-l])) ) ;
- ll ans2 = (((suff[n-r-1] - temp)% mod + mod) % mod )/ pw;
- ans2 %= mod;
- // cout << ans2 << nl;
- ans = ans *(ll) pow(10 , sz(to_string(ans2))) %mod + ans2 ;
- ans %= mod;
- if(r != n-1){
- temp = pref[r] , pw = pow(10 , sz(to_string(pref[n-1])) - sz(to_string(pref[r])));
- ans2 = (pref[n-1] - temp * pw);
- ans2 %= mod;
- ans2 += mod;
- ans2 %= mod;
- // cout << ans2 << nl;
- ans = (ans * (ll)pow(10 , sz(to_string(ans2)) )) % mod + ans2 ;
- ans %= mod;
- }
- cout << ans <<nl;
- }
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // files();
- // freopen("fun.in", "r", stdin);
- int testCase=1;
- cin >> testCase ;
- for(int i=1 ; i <= testCase ; i++){
- // cout << "Case "<< i <<": " << nl;
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement