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

题干:

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 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440810.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【牛客 - 练习】约数个数的和(数论,数学)

题干&#xff1a; 给个n&#xff0c;求1到n的所有数的约数个数的和~ 输入描述: 第一行一个正整数n 输出描述: 输出一个整数&#xff0c;表示答案 示例1 输入 复制 3 输出 复制 5 说明 样例解释&#xff1a; 1有1个约数1 2有2个约数1,2 3有2个约数1,3 备注: n…

mysql json 创建索引_MySQL · 最佳实践 · 如何索引JSON字段

概述MySQL从5.7.8起开始支持JSON字段&#xff0c;这极大的丰富了MySQL的数据类型。也方便了广大开发人员。但MySQL并没有提供对JSON对象中的字段进行索引的功能&#xff0c;至少没有直接对其字段进行索引的方法。本文将介绍利用MySQL 5.7中的虚拟字段的功能来对JSON对象中的字段…

mysql链路跟踪工具_EasySwoole利用链路追踪组件制作甩锅工具

前言最近前端老是反馈API调用异常&#xff0c;说请求成功但是没有数据返回&#xff01;我写的代码怎么可能有bug&#xff0c;肯定是前端调用的方式不对&#xff01;经过一番套鼓&#xff0c;直接把请求参数和响应内容打印到控制台&#xff0c;果然不出我所料&#xff0c;请求缺…

java selector 源码_Java NIO核心组件-Selector和Channel

昨天我们介绍了一下SelectorProvider和IO multiplexing.特别是IO multiplexing中的epoll系统调用,是Linux版本的Java的NIO的核心实现.那今天我们就来介绍一下, Java NIO中的核心组件, Selector和Channel.这两个组件,对于熟悉Java OIO,而不熟悉Java NIO的朋友来说,理解其作用是极…

python 爬虫 博客园_Python爬虫爬取博客园作业

分析一下他们的代码&#xff0c;我在浏览器中对应位置右键&#xff0c;然后点击检查元素&#xff0c;可以找到对应部分的代码。但是&#xff0c;直接查看当前网页的源码发现&#xff0c;里面并没有对应的代码。我猜测这里是根据服务器上的数据动态生成的这部分代码&#xff0c;…

java 与 xml_xml与java对象转换

public static void main(String[] args) {//java bean 转 xmlDept d new Dept();List staffs new ArrayList<>();Staff s1 new Staff("wuyun", 20);Staff s2 new Staff("lilei", 22);staffs.add(s1);staffs.add(s2);d.setDeptName("开放平…

【牛客 - 370H】Rinne Loves Dynamic Graph(分层图最短路)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/370/H 来源&#xff1a;牛客网 Rinne 学到了一个新的奇妙的东西叫做动态图&#xff0c;这里的动态图的定义是边权可以随着操作而变动的图。 当我们在这个图上经过一条边的时候&#xff0c;这个图上所…

中位数及带权中位数问题(转)

先从一到简单的题看起&#xff1a; 士兵站队问题 在一个划分成网格的操场上&#xff0c;n个士兵散乱地站在网格点上。网格点由整数坐标(x,y)表示。士兵们可以沿网格边上、下、左、右移动一步&#xff0c;但在同一时刻任一网格点上只能有一名士兵。按照军官的命令&#xff0c;…

*【HDU - 4272 】LianLianKan (dfs 或 状压dp,贪心不行)

题干&#xff1a; I like playing game with my friend, although sometimes looks pretty naive. Today I invent a new game called LianLianKan. The game is about playing on a number stack. Now we have a number stack, and we should link and pop the same element…

java中的values函数_巧用valueat函数(快逸免费版)

在制作报表时&#xff0c;经常会遇到将数据库里一列数据按照条件取值后&#xff0c;分为多列显示的需求&#xff0c;例如&#xff1a;数据库中有一列名为type的数据&#xff0c;在报表中&#xff0c;第一列选择type为1的数据&#xff0c;第二列选择type为2的数据。由于受到扩展…

Java设计流程执行器_Java进阶面试精选系列:SpringMVC+SpringBoot+Hibernate+Mybatis+设计模式...

小编精心收集&#xff1a;为金三银四准备&#xff0c;以下面试题先过一遍&#xff0c;为即将到了的面试做好准备&#xff0c;也过一遍基础知识点。一、Spring/Spring MVC1.为什么要使用 spring&#xff1f;2.解释一下什么是 aop&#xff1f;3.解释一下什么是 ioc&#xff1f;3.…

【牛客 - 370E】Rinne Loves Gift(Bellman_Ford判负环,二分,分数规划)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/370/E 来源&#xff1a;牛客网 Rinne 喜欢礼物&#xff0c;也喜欢送礼物 圣诞节快到了&#xff0c;Rinne 要去给给住在城市里的人送礼物 城市的交通可以抽象成一个 n 个点 m 条边的有向图 每条边上有…

【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)

题干&#xff1a; In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be . Given your test scores and a positive integer k, determine how high you can make your cumulative aver…

重写过的url 怎么获取当前页面url java_网站URL重写(Java UrlRewrite 的使用)

现在大部分的网站和商城都会使用到URL重写&#xff0c;接触到这个&#xff0c;也是因为正在做的电子商务商城。URL重写&#xff0c;是将原有的URL采用另一种规则来显示&#xff0c;使得用户方便访问同时也屏蔽一些信息。在此说下它的好处&#xff0c;在开发过程中&#xff0c;经…

Java行业情景分析_Java 设计模式情景分析——单例模式

单例模式可以说是应用最广的模式之一&#xff0c;在应用单例模式时&#xff0c;单例对象的类必须保证只有一个实例存在&#xff0c;而且可以自行实例化并向整个系统提供这个实例。一般在不能自由构造对象的情况下&#xff0c;就会使用单例设计模式&#xff0c;例如创建一个对象…

php实现播放直播_PHP直播技术分享(一:实现直播)

推流服务器采用的是自搭的推流服务器 , 自己玩玩 做外包项目还是去搞七牛云/阿里这样的服务器吧,开始bb-----1:技术栈image.jpeg2:开发中业务(1)主播申请时创建个秘钥 , 这个秘钥随时字符串即可到时候根据字符串找到拉流的直播位置存数据库包括推流地址image.png3:配置nginx-rt…

php获取location,php获取header[‘location’]信息常见问题

15/01/31本文关键字: 302, header, location//初始化url信息$host “#8221;;$url$host.”l/rzTf7ap2viw/&iid222004556&resourceId0_04_05_99/v.swf”;//按照字段获取header响应信息$headers get_headers($url, TRUE);//获取这个土豆的302跳转地址$u302 $headers[“Lo…

php对联广告,html左右对联代码 cms网站对联广告html代码

实现网页左右两侧居中的对联广告代码你只记得自己有好多朋友却从没想过小编只有你一人。无标题文档 #left{ width:200px;height:450px; position:absolute; left:0px; background:red; border:1px #000 solid} #right{width:200px;height:450px; position:absolute; right:0px;…

php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...

原本的wordpress文章如果分页会成如下的结构&#xff1a;http://www.xyqzmt.cn/1.html/2通常固定链接都是这样的结构&#xff0c;设为/%postname%.html或者/%post_id%.html 以前我一直无法解决如上的问题&#xff0c;最后放弃挣扎&#xff0c;如果遇到很长的文章全都放在一个页…

【牛客 - 181F】子序列(容斥,组合数,费马小定理)

题干&#xff1a; 题目描述 给出一个长度为n的序列&#xff0c;你需要计算出所有长度为k的子序列中&#xff0c;除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T&#xff0c;表示数据组数。 对于每组数据&#xff0c;第一行两个整数N&#xff0c;k&#…