【Codeforces Round #520 C.Banh-mi】字首和+
阿新 • • 發佈:2018-12-21
C. Banh-mi
題意
做法
坑點
程式碼
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
const int maxn = 1e5+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
ll pow_(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=(ans*a)%Mod;
b>>=1;
a=(a*a)%Mod;
}
return ans;
}
ll inv(ll x)
{
return pow_(x,Mod-2);
}
ll sum[maxn][2];
char str[maxn];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,q;
scanf("%d%d",&n,&q);
scanf("%s",str+1);
for(int i=1;i<=n;i++)
{
sum[i][0]=sum[i-1][0];
sum[i][1]=sum[i-1][1];
if(str[i]=='0') sum[i][0]=(sum[i][0]+1)%Mod;
else sum[i][1]=(sum[i][1]+1)%Mod;;
}
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
ll sum0=(sum[r][0]-sum[l-1][0]+Mod)%Mod;
ll sum1=(sum[r][1]-sum[l-1][1]+Mod)%Mod;
ll ans=0;
ans=(pow_(2,sum1)-1+Mod)%Mod;
ll st=(pow_(2,sum1)-1+Mod)%Mod;
ll tmp=(pow_(2,sum0)-1+Mod)%Mod;
tmp=(tmp*st)%Mod;
ans=(ans+tmp)%Mod;
printf("%lld\n",ans);
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}