Codeforces1062C. Banh-mi(贪心+快速幂)
题目链接:传送门
题目:
C. Banh-mi
time limit per test
second
memory limit per test
megabytes
input
standard input
output
standard output 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 n
parts, places them on a row and numbers them from through n. For each part i, he defines the deliciousness of the part as xi∈{,}. 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 i-th part then his enjoyment of the Banh-mi will increase by xi and the deliciousness of all the remaining parts will also increase by xi. The initial enjoyment of JATC is equal to 0 . For example, suppose the deliciousness of
parts are [,,]. If JATC eats the second part then his enjoyment will become and the deliciousness of remaining parts will become [,_,]. Next, if he eats the first part then his enjoyment will become and the remaining parts will become [_,_,]. After eating the last part, JATC's enjoyment will become 4 . However, JATC doesn't want to eat all the parts but to save some for later. He gives you q
queries, each of them consisting of two integers li and ri. 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] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo + .
Input The first line contains two integers n
and q (≤n,q≤ ). The second line contains a string of n
characters, each character is either '' or ''. The i-th character defines the deliciousness of the i -th part. Each of the following q
lines contains two integers li and ri (≤li≤ri≤n ) — the segment of the corresponding query.
Output Print q
lines, where i-th of them contains a single integer — the answer to the i-th query modulo + .
Examples
Input
Copy Output
Copy Input
Copy Output
Copy Note In the first example: For query : One of the best ways for JATC to eats those parts is in this order: , , ,
.
For query
: Both , and , ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
题目大意:
给出一个长度为n的二进制串,q次询问。
每次询问l到r区间内吃Banh-mi得到的美味值的最大值。
吃掉一个位置,会得到这个位置的美味值(初始值为0、1),其他位置的美味值也会加上这个位置的美味值。
思路:
一个位置被吃掉后的贡献是和剩下的位置的数量正相关的,所以要贪心地吃美味值大的位置。
然后举两个栗子模拟一下发现一只吃1的话,得到的美味值是1、2、4、8、16。。。
然后开始吃0的话就是31、62、124、31*8、31*16。。。
意会一下,答案大概就是(2cnt1-1)*2cnt0。
预处理前缀和就可以O(1)求出cnt1和cnt0了,然后快速幂跑一跑,时间复杂度为O(nlogn)。
代码:
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
const int MAX_N = 1e5 + ;
const int MOD = 1e9 + ; string a;
ll sum[MAX_N]; ll fpow(ll a, ll p)
{
ll ans = ;
for (; p; p >>= ) {
if (p&)
ans = (ans*a)%MOD;
a = (a*a)%MOD;
}
return ans%MOD;;
} int main()
{
ios::sync_with_stdio(false);cin.tie();
int N, Q;
cin >> N >> Q;
cin >> a;
sum[] = ;
for (int i = ; i <= N; i++) {
sum[i] = sum[i-];
if (a[i-] == '')
sum[i]++;
}
while (Q--) {
ll l, r;
cin >> l >> r;
ll len = r-l+;
ll cnt1 = sum[r] - sum[l-];
ll cnt0 = len - cnt1;
ll ans = fpow(, cnt1) - ;
if (ans < )
ans += MOD;
ans = (ans * fpow(, cnt0)) % MOD;
cout << ans << endl;
}
return ;
}
Codeforces1062C. Banh-mi(贪心+快速幂)的更多相关文章
- Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂
A. Table time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 小总结:快速幂+贪心————Bit Mask____UVA 10718 多多去理解去温习哦!
传送门:https://vjudge.net/problem/UVA-10718 Preview: bitstream:a flow of data in binary form. in bit-wi ...
- 贪心,打表(或者快速幂), UVA - 11636
题目链接: https://cn.vjudge.net/problem/34398/origin 题目比较简单,就是水题,基础贪心,大于所需的即可: AC代码: 打表: #include <cm ...
- “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】
黑白图像直方图 发布时间: 2017年7月9日 18:30 最后更新: 2017年7月10日 21:08 时间限制: 1000ms 内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...
- codevs3500 快速幂入门题解
codevs3500 快速幂入门题解 //我也是抄的题解 题目描述 Description 输入3个数a,b,c,求a^b mod c=? 输入描述 Input Description 三个数a,b, ...
- UVALive 7040 Color (容斥原理+逆元+组合数+费马小定理+快速幂)
题目:传送门. 题意:t组数据,每组给定n,m,k.有n个格子,m种颜色,要求把每个格子涂上颜色且正好适用k种颜色且相邻的格子颜色不同,求一共有多少种方案,结果对1e9+7取余. 题解: 首先可以将m ...
- HDU 2817 A sequence of numbers 整数快速幂
A sequence of numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- kettle 连接 Oracle 异常
场景重现 新安装的 kettle(pdi-ce-7.0.0.0-25) 连接 Oracle 11G R2 报错如下: 解决办法 到 Oracle 官网 JDBC Downloads 下载对应的 ojd ...
- 安装win10 和win中的一些杂项问题
reg中的 policies是政策/策略, 主要是指 控制面板中的一些控制项目, 其中 explorer是 控制面板的 显示项目... DWORD值是一个32位(4个字节:即双字)长度的数值, 是数字 ...
- malloc/free 和 new/delete
(本文参考于网上) 首先两者都可用于申请动态内存和释放内存。 对于非内部数据类型的对象而言,只用malloc/free无法满足动态对象的要求.对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执 ...
- pycharm 倒入request包方法(新手)
1.先安装request模块,在pycharm import,但是怎么也倒不进去,咨询了开发,原来需要把包倒入到pycharm 编译器里面才可以import 成功,具体操作步骤如下 首先确认下自己电脑 ...
- C#集合类型大揭秘
集合是.NET FCL(Framework Class Library)的重要组成部分,我们平常撸C#代码时免不了和集合打交道,FCL提供了丰富易用的集合类型,给我们撸码提供了极大的便利.正是因为这种 ...
- C# 关键字base用法
1.调用基类的方法 public class A { public virtual void Hello() { Console.WiriteLine("Hello"); } } ...
- SAP 多语言文本翻译
SAP自己的东西都是有语言包的,针对很多语言有是有对应文本的翻译,巴特,比较不是专业的翻译,多以很多时候还是有这样那样的文本描述需要调整. 语言包怎么打就不说了,也不知道,知道也没打过... 标准界面 ...
- 算法-最通俗易懂的KMP算法详解
有些算法,适合从它产生的动机,如何设计与解决问题这样正向地去介绍.但KMP算法真的不适合这样去学.最好的办法是先搞清楚它所用的数据结构是什么,再搞清楚怎么用,最后为什么的问题就会有恍然大悟的感觉.我试 ...
- linux下配置mysql 与错误解决
1.下载mysql wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-el7-x86_64.tar.gz 2.检查linux是否 ...
- Jmeter对HTTP请求压力测试、并发测试的简单使用方法
对于服务器性能测试这块的经验更是少得可以忽略.迫使不得不让我们去尝试了解测试的知识. 首先我们的需求场景如下: 服务器硬件:(只有一台) 系统:Windows 2003 WebServer:Tomca ...