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. 洛谷U14200 Changing 题解 【杨辉三角】

    题目描述 有nnn盏灯环形排列,顺时针依次标号为1⋯n1\cdots n1⋯n.初始时刻为000,初始时刻第iii盏灯的亮灭aia_iai​给定,000表示灭,111表示亮.下一时刻每盏灯的亮灭取决于 ...

  2. NOI2013 矩阵游戏 【数论】

    题目描述 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的元素,则F[i ...

  3. NOI2014魔法森林题解报告

    题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2,3,-,n,边标号为 1,2,3,-, ...

  4. 70路小报:用PV和UV作为网站衡量指标已经过时

    方法]投资人呼吁:PV和UV不应该再作为产品衡量指标 风险投资机构Andreessen Horowitz近日一直反对再用传统的网站衡量指标去评价互联网产品,比如PV和UV,甚至包括应用的下载量. 他们 ...

  5. linux系统启动自动激活网卡的解决方法

    linux每次启动的时候网卡都需要激活才能上网,实在是很麻烦. 上网找了找资料,最后是这样解决的: #   vi   /etc/sysconfig/network-scripts/ifcfg-eth0 ...

  6. [CodeVs3196]黄金宝藏(DP/极大极小搜索)

    题目大意:给出n(≤500)个数,两个人轮流取数,每次可以从数列左边或者右边取一个数,直到所有的数被取完,两个人都以最优策略取数,求最后两人所得分数. 显然这种类型的博弈题,第一眼就是极大极小搜索+记 ...

  7. 专题训练之数位DP

    推荐以下一篇博客:https://blog.csdn.net/wust_zzwh/article/details/52100392 1.(HDOJ2089)http://acm.hdu.edu.cn/ ...

  8. python学习(十四)正则表达式

    原文链接 ## 什么是正则表达式`正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑 ...

  9. [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: YES)

    在配置了zabbix服务端后,发现:“zabbix server is running”的Value值是“no”, 用:netstat -atnlp|grep 10051 发现没有出现zabbix_s ...

  10. time_t与GMT格式互转

    time_t Time::timeFromGMT(string gmt) { char week[4]; memset(week,0,4); char month[4]; memset(month,0 ...