Codeforces Round #520 (Div. 2) C. Banh-mi
C. Banh-mi
题目链接:https://codeforc.es/contest/1062/problem/C
Description:
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,1] . 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.
Input:
The first line contains two integers nn and qq (1≤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≤n) — the segment of the corresponding query.
Output:
Print q lines, where ii-th of them contains a single integer — the answer to the ii-th query modulo 109+7.
Sample Input:
4 2
1011
1 4
3 4
Sample Output:
14
3
题意:
给出一个01串,每次可以选一个出来,然后其它的都会加上选出来的这个数。问怎么选可以让选出来的和最大。
题解:
首先肯定会选大的,然后会选小的。但是询问次数那么多,如果每次都通过模拟操作来实现,肯定会超时。
我们考虑每一个数给最终答案的贡献,比如有k个1,总长度为n。
选出第一个1,然后对答案的直接贡献是1,但是后面所有的都会加1,第二个1选出来的时候对答案的贡献会加1,然后这个1又会加到后面的数上,选后面的一个1时,对答案的贡献就是2了,然后以此类推...
最后会发现第一个1对最终答案的贡献就是2^(n-1),然后1的总贡献就时(2^(n-1)+2^(n-2)+...+2^(n-k)),最后再推下就可以得出最终答案了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define MOD 1000000007
using namespace std; const int N = 1e5+;
int n,q;
char s[N];
int len[N]; long long quick_pow(long long a,int b){
long long ans=;
while(b){
if(b&) ans=(ans*a)%MOD;
a=(a*a)%MOD;
b>>=;
}
return ans ;
} int main(){
scanf("%d%d",&n,&q);
scanf("%s",s+);
for(int i=;i<=n;i++) if(s[i]=='') len[i]=len[i-]+;else len[i]=len[i-];
for(int i=,l,r;i<=q;i++){
scanf("%d%d",&l,&r);
int len1 = len[r]-len[l-],k=r-l+;
long long ans;
ans=((quick_pow(,len1)-)%MOD*quick_pow(,k-len1)%MOD)%MOD;
printf("%lld\n",ans);
}
return ;
}
Codeforces Round #520 (Div. 2) C. Banh-mi的更多相关文章
- Codeforces Round #520 (Div. 2)
Codeforces Round #520 (Div. 2) https://codeforces.com/contest/1062 A #include<bits/stdc++.h> u ...
- Codeforces Round #520 (Div. 2) E. Company(dfs序判断v是否在u的子树里+lca+线段树)
https://codeforces.com/contest/1062/problem/E 题意 给一颗树n,然后q个询问,询问编号l~r的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
- Codeforces Round #520 (Div. 2) B. Math 唯一分解定理+贪心
题意:给出一个x 可以做两种操作 ①sqrt(x) 注意必须是完全平方数 ② x*=k (k为任意数) 问能达到的最小的x是多少 思路: 由题意以及 操作 应该联想到唯一分解定理 经过 ...
- Codeforces Round #520 (Div. 2)B(贪心,数学)
#include<bits/stdc++.h>using namespace std;int mi[100007];int main(){ int cnt=0; int flag=0; i ...
- CF每日一练 Codeforces Round #520 (Div. 2)
比赛过程总结:过程中有事就玩手机了,后面打的状态不是很好,A题理解错题意,表明了内心不在状态,B题想法和思路都是完全正确的,但是并没有写出来,因为自己代码能力不强,思路不是特别清晰,把代码后面写乱了, ...
- Codeforces Round #520 (Div. 2) Solution
A. A Prank Solved. 题意: 给出一串数字,每个数字的范围是$[1, 1000]$,并且这个序列是递增的,求最多擦除掉多少个数字,使得别人一看就知道缺的数字是什么. 思路: 显然,如果 ...
- Codeforces Round #520 (Div. 2) D. Fun with Integers
D. Fun with Integers 题目链接:https://codeforc.es/contest/1062/problem/D 题意: 给定一个n,对于任意2<=|a|,|b|< ...
- Codeforces Round #520 (Div. 2) B. Math
B. Math time limit per test:1 second memory limit per test:256 megabytes Description: JATC's math te ...
- Codeforces Round #520 (Div. 2) A. A Prank
A. A Prank time limit per test 1 second memory limit per test 256 megabytes 题目链接:https://codefo ...
随机推荐
- ecshop 全系列版本网站漏洞 远程代码执行sql注入漏洞
ecshop漏洞于2018年9月12日被某安全组织披露爆出,该漏洞受影响范围较广,ecshop2.73版本以及目前最新的3.0.3.6.4.0版本都受此次ecshop漏洞的影响,主要漏洞是利用远程代码 ...
- tail命令使用
1.tail命令 命令的主要用途是将指定的文件的最后部分输出到终端,如果该文件有更新,tail会自己主动刷新. 2.tail语法 tail [ -f ] [ -c Number | -n Number ...
- 链栈的c++实现
2013-08-30 20:58 1876人阅读 评论(0) 收藏 举报 链栈是借用单链表实现的栈.其不同于顺序栈之处在于: 1.链栈的空间是程序运行期间根据需要动态分配的,机器内存是它的上限.而顺序 ...
- itchat和matplotlib的结合使用爬取微信信息
前几天无意中看到了一片文章,<一件有趣的事:我用 Python 爬了爬自己的微信朋友>,这篇文章写的是使用python中的itchat爬取微信中朋友的信息,其中信息包括,昵称.性别.地理位 ...
- RedHat安装Oracle后中文乱码
radhat7.1 Oracle11gr2 安装Oracle时忘记设置字符集,导致安装后中文乱码,中文变成"???????????" 分析原因是Oracle服务器端和客户端的字符集 ...
- Uber CEO博鳌论坛采访:看好中国市场共享经济的发展模式
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...
- 史上最全的PHP正则表达式
首先看下正则表达式思维导图: 一.校验数字的表达式 1 数字:^[0-9]*$2 n位的数字:^\d{n}$3 至少n位的数字:^\d{n,}$4 m-n位的数字:^\d{m,n}$5 零和非零开头 ...
- 涉及到大小变化,类似QScrollArea判断大小是否显示滚动条
涉及到大小变化,类似QScrollArea判断大小是否显示滚动条的情况要注意 这两个属性的设置:
- Android TextView 单行文本的坑
这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...