ACM: NBUT 1646 Internet of Lights and Switches - 二进制+map+vector

NBUT 1646 Internet of Lights and Switches

Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:

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
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
#include"map"
using namespace std;
typedef long long LL ;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define FK(x) cout<<"["<<x<<"]\n"
#define bigfor(T) for(int qq=1;qq<= T ;qq++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

LL light_now[300006];

int main() {
	char s[123];
	int n,m,a,b;
	int qq=1;
	LL light_change;
	while(~scanf("%d%d%d%d",&n,&m,&a,&b)) {
		map< LL , vector<int> > mp;  //  LL 保存开关的状态,vector 保存 能够按到这个状态的所有起点。
		mp.clear();
		light_now[0]=0;
		mp[0].push_back(0);
		for(int i=1; i<=m; i++) {
			scanf("%s",s);
//			FK(s);
			light_change=0;
			for(int j=0; j<n; j++) {
				if(s[j]==‘1‘)light_change+=(1ll<<j); //按照50位数来保存灯的状态
			}
//			FK(light_change);
			light_now[i]=light_change^light_now[i-1]; //取反,现在开关的状态。
//			FK(light_now[i]);
			mp[light_now[i]].push_back(i); //把每一个需要被按下的键,能够按到这个状态的所有起点保存。
		}
		int ans=0;
		for(int i=1; i<=m; i++) {
			LL t=((~light_now[i])&((1ll<<n)-1)); //需要被按下的键
			if(!mp[t].empty()) {
//				int x=mp[t].front();
//				mp[t].pop();
//				if(x>=a&&x<=b)ans++;
				int l=lower_bound(mp[t].begin(),mp[t].end(),i-b)-mp[t].begin();  //查找下边界
				int r=upper_bound(mp[t].begin(),mp[t].end(),i-a)-mp[t].begin();  //查找上边界
				ans+=r-l;  //记录所有的可能性。
			}
		}
		printf("Case %d: ",qq++);
		printf("%d\n",ans);
	}
	return 0;
}

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

/*/

  

时间: 2024-09-28 19:54:30

ACM: NBUT 1646 Internet of Lights and Switches - 二进制+map+vector的相关文章

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

Internet of Lights and Switches Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 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

2015湖南ACM省赛I题 Internet of Lights and Switches(思维)

https://ac.2333.moe/Problem/view.xhtml?id=1646 本来想用主席树来着,一看内存直接劝退. 貌似只能用vector或map搞一搞了. 1 #define bug(x) cout<<#x<<" is "<<x<<endl 2 #include<iostream> 3 #include<algorithm> 4 #include<iterator> 5 #inclu

uva Internet of Lights and Switches (异或运算)

题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=13&page=show_contest&contest=350 题意:有N盏灯和M个开关,每一个开关控制多盏灯(比如N=4,"0011"就代表这个开关控制第3和第4盏灯),现在问你有多少种按开关的方法使得所有的灯都熄灭(每个开关只能按一次,并且按的开关的编号是连续的),另外所按的编号连续的开关的长度在[a,b]范围内.初

ACM: NBUT 1105 多连块拼图 - 水题 - 模拟

NBUT 1105  多连块拼图 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format: Practice Appoint description:  System Crawler  (Aug 12, 2016 9:32:14 AM) Description 多连块是指由多个等大正方形边与边连接而成的平面连通图形. -- 维基百科 给一个大多连块和小多连块,你的任务是判断大多连块是否可以由两个这样的小多连块拼成.小多连块只能

ACM: NBUT 1107 盒子游戏 - 简单博弈

NBUT 1107  盒子游戏 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format: Practice Appoint description:  System Crawler  (Aug 13, 2016 10:35:29 PM) Description 有两个相同的盒子,其中一个装了n个球,另一个装了一个球.Alice和Bob发明了一个游戏,规则如下:Alice和Bob轮流操作,Alice先操作每次操作时,游戏者先看看

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

Description We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below: 2 : a, b, c    3 : d

ACM学习历程——UVA540 Team Queue(队列,map:Hash)

Description Team Queue   Team Queue  Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front

NBUT 2014 B Beautiful Walls

题目链接:http://acm.nbut.edu.cn/Contest/view/id/70/problem/B.xhtml 题意:给出n(n≤100000 )个正整数,考虑这个序列的连续的子序列的个数,将含有两个以上相同数字的子序列排除在外,将不同位置的相同序列算作两种,问这样的序列有多少个?为了便于描述,将这种序列称为W序列. 输入格式:每个样例首先输入正整数的个数n,然后是n个正整数,有多组样例 输出所求序列的个数 样例输入 5 3 4 5 5 2 3 1 2 3 样例输出 9 6 分析:

NBUT 2014 F Team of Slime

题目链接:http://acm.nbut.edu.cn/Problem/view.xhtml?id=1557 题意:给出n个不相同且分布在1-n之间的正整数组成的队列,每次可以将任一个数放到队首,问最少需要多少次可以将队列变为升序? 分析: (1)一种方法是对于每个有较大数在前面的数放到队首,然后将后面比它小的数再按照从大到小的顺序放到队首,这样就能将队列变为升序,实现可以用线段树维护每个数的逆序数,可惜这样做不是最优的结果: (2)考虑下界:考虑不需要移动的数,最大数n无论如何是不需要移的,n