IOI 98
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.
/*
ID: shiznet1
LANG: JAVA
TASK: lamps
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
public class lamps {
private static int[] even = {6,12,5,15,9,3,10,0}; //{0110,1100,0101,1111,1001,0011,1010,0000};
private static int[] odd = {1,11,2,8,14,4,13,7};//{0001,1011,0010,1000,1110,0100,1101,0111};
private static int[] odd_1 = {1,2,8,4};//{0001,1011,0010,1000,1110,0100,1101,0111};
private static int[] even_2 = {6,12,5,9,3,10,0}; //{0110,1100,0101,1111,1001,0011,1010,0000};
public static void main(String[] args) throws IOException,FileNotFoundException {
BufferedReader br = new BufferedReader(new FileReader("lamps.in"));
FileWriter fout = new FileWriter("lamps.out");
int N = Integer.parseInt(br.readLine());
int C = Integer.parseInt(br.readLine());
LinkedList<int[]> list = new LinkedList<int[]>();
String on = br.readLine();
String off = br.readLine();
if(!on.equals("-1")) {
String[] ons = on.substring(0, on.length()-3).split(" ");
for(String str:ons) {
int[] tmp = new int[2];
tmp[0]=Integer.parseInt(str)-1;
tmp[1]=1;
list.add(tmp);
}
}
if(!off.equals("-1")) {
String[] offs = off.substring(0, off.length()-3).split(" ");
for(String str:offs) {
int[] tmp = new int[2];
tmp[0]=Integer.parseInt(str)-1;
tmp[1]=0;
list.add(tmp);
}
}
int[] checklist = null;
if(C>0&&(C&1)==0) {
if(C==2)
checklist = even_2;
else
checklist = even;
}
else if(C>0){
if(C==1)
checklist = odd_1;
else
checklist = odd;
}else {
checklist = new int[1];
}
LinkedList<Integer> result = new LinkedList<Integer>();
for(int buttom:checklist) {
if(check(buttom,list)) {
result.add(buttom);
System.out.println(buttom);
}
}
doPrint(result,fout,N);
fout.flush();
fout.close();
br.close();
System.exit(0);
}
private static void doPrint(LinkedList<Integer> result, FileWriter fout,
int n) {
if(result == null||result.size()==0) {
try {
fout.write("IMPOSSIBLE\n");
} catch (IOException e) {
e.printStackTrace();
}
return ;
}
Iterator<Integer> iterator = result.iterator();
while(iterator.hasNext()) {
int buttoms = iterator.next();
try {
for(int i = 0;i<n;i++) {
fout.write(check(buttoms,i)+'0');
}
fout.write("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static boolean check(int buttom,
LinkedList<int[]> list) {
if(list==null||list.size()==0)
return true;
Iterator<int[]> iterator = list.iterator();
while(iterator.hasNext()) {
int[] pair = iterator.next();
if(check(buttom,pair[0])!=pair[1])
return false;
}
return true;
}
private static int check(int buttom, int position) {
return 1^(buttom&1)^
(((buttom>>1)&1)&((position&1)^1))^
(((buttom>>2)&1)&(position&1))^
(((buttom>>3)&1)&((position%3)==0?1:0));
}
}