CodeForces - 1062C

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 nn parts, places them on a row and numbers them from 11 through nn. For each part ii, he defines the deliciousness of the part as xi∈{0,1}xi∈{0,1}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the ii-th part then his enjoyment of the Banh-mi will increase by xixi and the deliciousness of all the remaining parts will also increase by xixi. The initial enjoyment of JATC is equal to 00.

For example, suppose the deliciousness of 33 parts are [0,1,0][0,1,0]. If JATC eats the second part then his enjoyment will become 11 and the deliciousness of remaining parts will become [1,_,1][1,_,1]. Next, if he eats the first part then his enjoyment will become 22 and the remaining parts will become [_,_,2][_,_,2]. After eating the last part, JATC's enjoyment will become 44.

However, JATC doesn't want to eat all the parts but to save some for later. He gives you qq queries, each of them consisting of two integers lili and riri. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [li,ri][li,ri] in some order.

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

Input

The first line contains two integers nn and qq (1≤n,q≤1000001≤n,q≤100000).

The second line contains a string of nn characters, each character is either '0' or '1'. The ii-th character defines the deliciousness of the ii-th part.

Each of the following qq lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the segment of the corresponding query.

Output

Print qq lines, where ii-th of them contains a single integer — the answer to the ii-th query modulo 109+7109+7.

Examples

Input

4 2
1011
1 4
3 4

Output

14
3

Input

3 2
111
1 2
3 3

Output

3
1

Note

In the first example:

  • For query 11: One of the best ways for JATC to eats those parts is in this order: 11, 44, 33, 22.
  • For query 22: Both 33, 44 and 44, 33 ordering give the same answer.

In the second example, any order of eating parts leads to the same answer.

题意:给你第一行字符串长度和几次询问

下面是lr区间让你选出顺序使区间之和最大,每获取一个数其他的数会加这个数的大小找出区间最大字符只有01两种情况

输出每次询问区间最大的值

思路:先找1再找0因为先找的最大的其他加的多,推出来的规律为2^1的个数-1乘2^0的个数

代码如下:

#include<iostream>
#include<cstdio> 
#include<cstring>
using namespace std;
long long book[1000009];char a[10000009];long long gg[1000009];
long long pows(long long a,long long b,long mode)
{
	long long sum=1;
	a = a%mode;
	while(b>0)
	{
		if(b%2== 1)
			sum = (sum*a)%mode;
			b/=2;
			a = (a*a)%mode;
	}
	return sum;
}
int main()
{
	long long n,m;
	scanf("%lld%lld",&n,&m);
	scanf("%s",&a);
	long long r,l;long long g=0;
	for(long long i=0;i<n;++i)
	{
		if(a[i] == '1')
		{
			g++;book[i] = g;
		}
		else{
			book[i] = g;
		}
	}
	long long y;int zz =0;
	for(int i=0;i<=g;++i)
	{
		gg[i] = zz;
		zz= (zz + pows(2,i,1000000007))%1000000007;	//前缀和存答案防超时1的数量套公式出来的最大值
	}
	for(long long i=0;i<m;++i)
	{
		scanf("%lld%lld",&r,&l);
		y = book[l-1]-book[r-1];
		if(a[r-1] == '1')
		{
			y++;
		}
		long long oo = l-r+1-y;
		long long sum=gg[y];
		long long uu=gg[y];
		long long ans1 = uu*(pows(2,oo,1000000007))%1000000007-uu;//1的最大加0的最大
		sum  = (sum%1000000007+ans1)%1000000007;
		printf("%lld\n",sum);
	} 
	return 0;
}
/*
6 1
101000
1 6
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值