# Codeforces Round #520 (Div. 2) C. Banh-mi

C. Banh-mi

time limit per test：1 second
memory limit per test：256 megabytes

Description:

JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.

First, he splits the Banh-mi into

For example, suppose the deliciousness of

However, JATC doesn't want to eat all the parts but to save some for later. He gives you

All the queries are independent of each other. Since the answer to the query could be very large, print it modulo

The first line contains two integers

The second line contains a string of

Each of the following

Output:

Print

#include <cstdio>4 2
#include <cstring>
#include <algorithm>
#include <iostream>
#define MOD 1000000007
using namespace std;

const int N = 1e5+5;
int n,q;
char s[N];
int len[N];

long long quick_pow(long long a,int b){
long long ans=1;
while(b){
if(b&1) ans=(ans*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return ans ;
}

int main(){
scanf("%d%d",&n,&q);
scanf("%s",s+1);
for(int i=1;i<=n;i++) if(s[i]=='1') len[i]=len[i-1]+1;else len[i]=len[i-1];
for(int i=1,l,r;i<=q;i++){
scanf("%d%d",&l,&r);
int len1 = len[r]-len[l-1],k=r-l+1;
long long ans;
ans=((quick_pow(2,len1)-1)%MOD*quick_pow(2,k-len1)%MOD)%MOD;
printf("%lld\n",ans);
}
return 0;
}

posted @ 2018-11-19 22:57  heyuhhh  阅读(207)  评论(0编辑  收藏  举报