Internet of Lights and Switches

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 3  Solved: 3
[Submit][Status][Web Board]

Description

You are a fan of "Internet of Things"(IoT, 物联网), so you build a nice Internet of Lights and Switches in your huge mansion. Formally, there are n lights and m switches,
each switch controls one or more lights, i.e. pressing that switch
flips the status of those lights (on->off, off->on).   Initially, all the lights are on. Your task is to count the number of ways to turn off all the lights by pressing some consecutive switches.
Each switch should not be pressed more than once. There is only one
restriction: the number of switches you pressed should be between a and b
(inclusive).  

Input

There
will be at most 20 test cases. Each test case begins with a line
containing four integers n, m, a, b(2<=n<=50,
1<=a<=b<=m<=300000). Each of the following m lines contains a
01 string of length n.The i-th character is 1 if and only if that
switch controls the i-th light. The size of the whole input file
does not exceed 8MB.  

Output

For each test case, print the case number, and the number of ways to turn off all the lights.  

Sample Input

2 4 1 4
01
10
11
00
2 4 3 3
01
10
11
00
6 3 1 3
101001
010110
101001

Sample Output

Case 1: 3
Case 2: 0
Case 3: 2 题意:现在有m盏灯,有n个开关(m<=50,n<=300000) 现在开关用01串表示,为1表示开关可以操作当前的灯,现在必须操纵连续k盏灯使得这些这些灯全部关闭,A<=k<=B,问现在有多少这样连续的开关满足条件?
题解:去年省赛竟然有几支队A了,ORZ,映射好难想到啊~.这题的话我们保存一个异或前缀和,如果某段的异或和为全1,那么这段就是可取的.但是怎么找到呢?我们假设xorsum[1,i]^xorsum[1,j] 为全1.那么xor[j+1,i]是符合条件的,关键是怎么找到这个区间?这时候就要用到hash了,用map映射满足当前异或前缀和的所有下标。用一个vector记录,然后输入的时候,只要查询 i 前面有多少个下标的异或值为 1111111...^xor[1,x], 然后可以二分查询当前的下标距离 i 必须为 A~B 之间,在这个范围内满足的下标的个数就是当前与 i 组合成为区间满足条件的个数,还有一种情况就是[1,i]这个区间满足条件,这种情况直接++即可.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
char str[];
const int INF = ;
map<LL,vector<int> > mp;
int n,m,A,B;
LL charToLL(char *str){
LL ans = ;
int p = ,len = strlen(str);
for(int i=len-;i>=;i--){
if(str[i]==''){
ans+=p;
}
p*=;
}
return ans;
}
int binary(LL val,int id){
vector <int> vec = mp[val];
int down = ,up = vec.size()-,r=-,l=INF;
while(down<=up){
int mid = (down+up)>>;
if(id-vec[mid]>=A){
r = mid;
down = mid+;
}else up = mid-;
}
down = ,up = vec.size()-;
while(down<=up){
int mid = (down+up)>>;
if(id-vec[mid]<=B){
l = mid;
up = mid-;
}else down = mid+;
}
//printf("%d %d\n",l,r);
if(l>r) return ;
return r-l+;
}
int main(){
int t = ;
while(scanf("%d%d%d%d",&m,&n,&A,&B)!=EOF){
mp.clear();
for(int i=;i<m;i++){
str[i] = '';
}
str[m]='\0';
LL k = charToLL(str),xorsum=,ans = ;
for(int i=;i<=n;i++){
scanf("%s",str);
LL x = charToLL(str);
xorsum^=x;
if(xorsum==k&&i>=A&&i<=B) ans++;
ans+=binary(k^xorsum,i);
mp[xorsum].push_back(i);
}
printf("Case %d: %lld\n",t++,ans);
}
}
												

湖南省第十一届大学生程序设计竞赛:Internet of Lights and Switches(HASH+二分+异或前缀和)的更多相关文章

  1. 2018 ACM 国际大学生程序设计竞赛上海大都会赛

    传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...

  2. 河南省第十一届ACM大学生程序设计竞赛

    nyoj-1365-山区修路 内存限制:128MB 时间限制:3000ms 特判: No通过数:4 提交数:4 难度:3 题目描述: SNJ位于HB省西部一片群峰耸立的高大山地,横亘于A江.B水之间, ...

  3. 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

    Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...

  4. 2016年中国大学生程序设计竞赛(合肥)-重现赛1001 HDU 5961

    传递 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  5. 2016年中国大学生程序设计竞赛(合肥)-重现赛1008 HDU 5968

    异或密码 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  6. Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...

  7. 2016年中国大学生程序设计竞赛(合肥)-重现赛1009 HDU 5969

    最大的位或 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. 2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server

    点击打开链接 2226: Contest Print Server Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 53  Solved: 18 [Su ...

  9. 山东省第四届ACM大学生程序设计竞赛解题报告(部分)

    2013年"浪潮杯"山东省第四届ACM大学生程序设计竞赛排名:http://acm.upc.edu.cn/ranklist/ 一.第J题坑爹大水题,模拟一下就行了 J:Contes ...

随机推荐

  1. Google Map API使用详解(一)——Google Map开发背景知识

    一.谷歌地图主页 谷歌地图对应不同的地区都会有一些专门的主页,首次登陆时会显示这些地区.比如,香港的:http://maps.google.com.hk,台湾的:http://maps.google. ...

  2. Python学习笔记(二十五)操作文件和目录

    摘抄:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319253241 ...

  3. JVM调优总结(2):基本垃圾回收算法

    可以从不同的的角度去划分垃圾回收算法: 按照基本回收策略分 引用计数(Reference Counting): 比较古老的回收算法.原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数. ...

  4. PHP扩展--taint检测隐藏漏洞

    简介 Taint 可以用来检测隐藏的XSS code, SQL注入, Shell注入等漏洞, 并且这些漏洞如果要用静态分析工具去排查, 将会非常困难, 比如对于如下的例子: <?php echo ...

  5. MySQL VS PostgreSQL:该选择哪个开源数据库?

    Naresh Kumar 是一位软件工程师与热情的博主,对编程与新事物充满了激情和兴趣.近日,Naresh撰写了一篇博文,对开源世界最常见的两种数据库 MySQL 与 PostgreSQL 的特点进行 ...

  6. Codeforces Round #419 (Div. 2) A-E

    上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...

  7. 【BZOJ】2693: jzptab 莫比乌斯反演

    [题意]2154: Crash的数字表格 莫比乌斯反演,多组询问,T<=10000. [算法]数论(莫比乌斯反演) [题解]由上一题, $ans=\sum_{g\leq min(n,m)}g\s ...

  8. 【CodeForces】947 D. Picking Strings

    [题目]D. Picking Strings [题意]给定只含'A','B','C'的字符串,支持以下变换:1.A - BC   2.B - AC   3.C - AB   4.AAA - empty ...

  9. 【BZOJ】1798: [Ahoi2009]Seq 维护序列seq 线段树多标记(区间加+区间乘)

    [题意]给定序列,支持区间加和区间乘,查询区间和取模.n<=10^5. [算法]线段树 [题解]线段树多重标记要考虑标记与标记之间的相互影响. 对于sum*b+a,+c直接加上即可. *c后就是 ...

  10. webpack4 未设置mode会自动压缩

    最近想用LayaBox做个小游戏,然而Laya本身不自带构建工具.然后觉得写模块化的东西还是用webpack好使,用es6的语法也比较清晰. 于是就装了webpack,只用babel-loader来编 ...