【CodeForces - 1062C】Banh-mi (贪心,数学,找规律,快速幂)

98 篇文章 1 订阅
56 篇文章 0 订阅

题干:

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.

中文题意:

Glory喜欢吃冰激凌,有一天,他买了一箱冰激凌

首先, 他把这些冰激凌分成 n 份, 把它们从1 到n排成一排 . 对每一部分都有一个 美味系数 xi 属于 {0, 1}. Glory打算一个个吃掉它们. 每一次,Glory可以随意选一部分吃掉,假设这部分是第i部分,然后 Glory的开心值 将会增加xi剩下来的每一份冰激凌的美味系数将会增加xi. Glory最初的开心值为 0.

举个例子说明这个过程,假设冰激凌被分成有 3份,美味系数为 [0, 1, 0]. 如果Glory先吃第二块,那么他的开心值将变为 1 ,然后三份的美味系数变为 [1, _, 1]. 然后Glory再吃第三块,那么他的开心值将变为2 ,然后三份的美味系数变为[_, _, 2].在吃最后一块后, Glory的开心值变为 4.

然而, Glory想利益最大化. 他给你 q 询问,询问包含 l_i and r_i. 对每次询问,你需要告诉他,他吃完[l_i, r_i]之间的所有冰激凌最多能获得多少开心值(不要求输出吃的顺序,只要求输出最大收益).

答案可能会非常大,请mod 10^9+7输出.

解题报告:

  首先看第一步,肯定有1选1,如果同时有多个1,那选哪个1都不影响答案,一次类推我们不需要关注位置,只需要关注在当前查询的 ( l , r )区间内有多少1,有多少0。通过贪心,我们知道我们每次需要选择最大的进行贪心。

找几个例子就画出结论了。这里给一个别人的题解:

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const int MAX = 2e5 + 5;
char s[MAX];
int sum0[MAX];
int bit[MAX];
int main()
{	
	int n,q;
	cin>>n>>q;
	cin>>(s+1);
	for(int i = 1; i<=n; i++) {
		sum0[i] = sum0[i-1];
		if(s[i] == '0') sum0[i]++;
	}
	int l,r;
	bit[0]=1;
	for(int i = 1; i<=n; i++) bit[i] = (bit[i-1]*2)%mod;
	while(q--) {
		scanf("%d%d",&l,&r);
		int len = r-l+1;
		ll ans = bit[len] - bit[sum0[r] - sum0[l-1] ];
		if(ans<=0) ans += mod;
		ans %=mod;
		printf("%lld\n",ans);
	}
	return 0 ;
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值