1062C - Banh-mi - CodeForces Solution


greedy implementation math *1600

Please click on ads to support us..

Python Code:

N = 10 ** 5 + 2; MOD = 10 ** 9 + 7
n, q = map (int, input ().split ())
x = input (); pre = [0] * (n + 5)
for i in range (1, n + 1) : pre[i] = pre[i - 1] + (x[i - 1] == '1')
POW2 = [1] * (N); pre2 = [1] * (N)
for i in range (1, N) :
  POW2[i] = (POW2[i - 1] * 2) % MOD
  pre2[i] = (pre2[i - 1] + POW2[i]) % MOD
for i in range (q) :
  l, r = map (int, input ().split ())
  cnt0, cnt1 = r - l + 1 - (pre[r] - pre[l - 1]), pre[r] - pre[l - 1]
  if cnt1 <= 0 : print (0); continue
  ans = pre2[cnt1 - 1]
  if cnt0 > 0 : ans += pre2[cnt0 - 1] * (POW2[cnt1] - 1)
  ans = ((ans % MOD) + MOD) % MOD
  print (ans)
  

C++ Code:

#include <bits/stdc++.h>
#define INF 1e18
#define MINSINF -1000000007
#define eps 1e-7
#define ff first
#define ss second
#define NN 10000000
#define pb(x) push_back(x)
#define pp() pop_back()
#define sq(a) a*a
#define MP(x,y) make_pair(x,y)
const int N=5e5+5;
const int mod=1e9+7;
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair <int,int> PII;
//memset(a, 0, sizeof(a));
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//bool comp(pair<int,int>& a,pair<int,int>& b){
    //return a.ss<b.ss;
//}
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ll power(ll a,ll b){
	ll res=1;
	while(b>0){
		if(b&1)  res=(res*a)%mod;
		a=(a*a)%mod;
		b/=2;
	}
	return res;
}
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//ll inverse(ll a, ll p=mod){ return power(a,p-2); }
ll abso(ll a, ll b){    return ((a%mod)*(b%mod))%mod; }
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//ll nCr(ll n, ll r){
    //vector <ll> fact(n+1,0); fact[0]=1;
    //for(ll i=1;i<=n;i++) fact[i]=abso(fact[i-1],i);
    //return abso(fact[n],abso(inverse(fact[n-r]),inverse(fact[r])));
//}
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
void solve(){
    int n,q;
    cin>>n>>q;
    string s;
    cin>>s;
    vector <int> pref(n+1,0);
    for(int i=0;i<n;i++){
        pref[i+1]=pref[i]+(s[i]-'0');
    }
    while(q--){
        int l,r;
        cin>>l>>r;
        if(pref[r]-pref[l-1]){
            ll sum=0;
            ll one=pref[r]-pref[l-1];
            ll zero=r-l+1-one;
            ll part_one=power(2,one)-1;
            sum+=abso(part_one,power(2,zero));
            cout<<sum<<endl;
        }
        else{
            cout<<0<<endl;
        }
    } 
}
int main(){	
    //freopen("file.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //clock_t tStart = clock();
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    //printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
        
    return 0;
}
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//a<<b means (a * pow(2,b))-> a * 2^b //left-sift operator <<
// checking set (i>>j)&1 or i&(1<<j)
//a>>b means (a / pow(2,b))-> a / 2^b //right-sift operator >>  
// __builtin_clz -> count the leading zeros of the integer from 32 bit
// __builtin_ctz -> count the trailing zeros of the given integer


Comments

Submit
0 Comments
More Questions

550C - Divisibility by Eight
5A - Chat Servers Outgoing Traffic
615A - Bulbs
5B - Center Alignment
549A - Face Detection
535B - Tavas and SaDDas
722C - Destroying Array
366A - Dima and Guards
716B - Complete the Word
1461C - Random Events
1627A - Not Shading
141B - Hopscotch
47B - Coins
1466C - Canine poetry
74A - Room Leader
1333D - Challenges in school №41
1475B - New Year's Number
461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)
948A - Protect Sheep
387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep