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
Sample Output
【分析】
#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;
}