思路:
先前缀和记录1的个数x,对应求出0的个数y;
根据题意推出公式:
“口感” = (2x - 1) * 2y % (1e9 + 7);
对2取次幂的时候要用快速幂;
Code:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
#include <stack>
#include <map>
#include <cmath>
#include <numeric>
#define mes memset
#define mec memcpy
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N = 1e5 + 10;
const int null = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
int n,q;
int a[N];
//快速幂代码
ll qmi(ll k)
{
ll res = 1 % mod,t = 2;
while(k)
{
if(k & 1) res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}
int main()
{
cin >> n >> q;
string s;
cin >> s;
//前缀和处理
for(int i = 0;i < s.size();i ++) a[i + 1] = a[i] + s[i] - '0';
while(q --)
{
int l,r;
cin >> l >> r;
//算出1的个数,0的个数
int cnt1 = a[r] - a[l - 1],cnt0 = r - l + 1 - cnt1;
//取出全部1得到的“口感”总和
ll ans1 = (qmi(cnt1) - 1) % mod;
//取出剩余的0得到的“口感”总和
ll ans0 = (ans1 * qmi(cnt0)) % mod;
cout << ans0 << endl;
}
return 0;
}