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;
- ll up(ll a , ll b ){
- return (( a % mod ) * ( b % mod) ) %mod;
- }
- ll Binex(ll a , ll b ){
- ll ret=1;
- while(b){
- if(b & 1 ) ret = up( ret, a);
- a= up(a , a );
- b>>=1;
- }
- return ret;
- }
- 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] *Binex(10 , n-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 = Binex(10 ,l) ;
- ll ans2 = (((suff[n-r-1] - temp)% mod + mod) % mod )/ pw;
- ans2 %= mod;
- ans = ans * Binex(10 , l +1) + ans2 ;
- ans %= mod;
- if(r != n-1){
- temp = pref[r] , pw = Binex(10 ,n-r-1);
- ans2 = (pref[n-1] - (temp * pw)%mod);
- ans2 += mod;
- ans2 %= mod;
- ans = (ans * Binex(10 , n-r-1 )) % mod + ans2 ;
- ans %= mod;
- }
- cout << ans <<nl;
- }
- // 13245
- // 54321
- // 972446260
- }
- 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