1784: Internet of Lights and Switches
Time Limit: 2 Sec
Memory Limit: 128 MB
[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
【分析】
题目里有个单词..叫做consecutive...连续的...真是...尴尬...如果不知道这个单词..样例是绝对算不出来的....
题意:有N盏灯和M个开关,每一个开关控制多盏灯(比如N=4,"1010"就代表这个开关控制第1和第3盏灯),初始所有的灯都是开着的。问你有多少种按开关的方法使得所有的灯都熄灭?(只能选择一段连续的开关区间,区间长度在[a,b]范围内。)
又是一题异或...那么显然我们可以考虑先把这个最长长度有50的字符串转化成整数,那样的话就方便比较了
(这里有一个大佬级别的二进制转化 ...嗯...学到了..)
然后考虑前缀异或和[1,j]^[1,i](i>j)的结果如果是全1的话,那么就可以说明[i,j]的异或和就是全1
因为相同的数异或两次就是0,所以有了这一点,代码就好写了,对于当前的前缀异或和,如果满足长度就进入区间,然后每次把超出区间的数删掉就可以了,我的程序是按照上面的区间异或和来的,所以对当前i,i表示的是当前判断区间的最后一位,具体的看代码里的解释吧
【代码】
#include <iostream>
#include <cstdio>
#include <map>
#define LL long long
using namespace std;
char s[100];
LL arr[301000];
map<LL,int>f;
int main()
{
int n,m,a,b;
int pp=1;
while (~scanf("%d%d%d%d",&n,&m,&a,&b))
{
f.clear();
arr[0]=0;
LL find=((LL)1<<n)-1;
LL ans=0;
for (int i=1;i<=m;i++)
{
LL res=0;
scanf("%s",s);
for (int j=0;j<n;j++) res<<=1,res+=s[j]-48; //二进制转化整数
arr[i]=arr[i-1]^res;//前缀异或和
if (i>=a) f[arr[i-a]]++; //如果当前长度到了a,那么就可以把这个区间里第一个加入区间
if (i>b) f[arr[i-b-1]]--; //去掉超出区间的那个数
ans+=f[arr[i]^find]; //结果可以直接找到,A^B=C=>B=A^C 稍微理解一下就好了
}
printf("Case %d: %lld\n",pp++,ans);
}
return 0;
}