Lamps Problem Solving Report
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
N lights, numbered 1~n. There are 4 switches, and C times the chance to change the status of a switch, how can the end of all the lights out of the situation may be?
One switch: Change the status of all lights.
Switch Second: Change the status of all odd number lights.
Switch Third: Change the status of all even number lights.
Switch No. Fourth: Change the status of all 3k+1 lights (k=0,1,2,... )。
At the beginning, all lights are lit.
Input
The first line is a number, N.
The second line is a number, C.
The following two lines, each line has a space divided by a number of, ending with-1, gives the question "additional constraints":
The number that appears in the third row indicates that the lights that are numbered should end up in a "bright" state, i.e. the number of these positions should be 1 at the end of the output;
The number shown in line four indicates that the lights that are numbered should end up in the "off" state, i.e. the number of these positions should be 0 at the end of the output.
Output
All states that meet the requirements of the topic, each line of a state, with n-bit binary number to indicate the light off state of all lights, from the highest to the lowest digit is the number 1th to n lights.
Note that the output is in dictionary order.
"Data Range"
n=10~100
c=0~10000
"Input Sample"
10
1
-1
7-1
"Output Example"
0000000000
0101010101
0110110110
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
If the status of all lights is recorded directly, then the number of States is 2 of the 100 species, significantly hyperspace.
A simple observation, the light of this problem can be divided into 6 groups, divided by 6 mode 0~5 the six groups. Then, a switch is the 6 group of all changes, II switch is to change the odd and even groups, fourth switch is only to change the corresponding two groups of 3k+1. In this way, all lights have a total of only 64 states.
Continue analysis, four switches, an initial state, then obviously we can toggle the switch 0~4 times, respectively, a switch is unchanged until four switches are changed, only the five cases. That is, the value of C only 0~4 is meaningful, and once more than 4, it is necessary to show that the lamp is two times, then you can let C minus 2, so that the lamp is not so meaningless "play", the result is obviously the same.
At this point, we turn N into 6,c and become 0~4, and this model is obviously much simpler. So, let's just take a look at the final restrictions on the subject.
is still mapped to the 1~6 lights on behalf of the 6 sets of lights can be, so here may directly lead to the problem is not solved ~
This is the idea of the subject, the rest is the implementation of programming details, not to repeat.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
The fourth time AC, look back on their own this less than one hours, I am really "lazy heart" Ah!
Two days did not do the problem, the brain began to lazy, quiet heart to think carefully, do very slow errors are many. A very simple question, making a few details mistakes in the process:
1. First Commit, forget the third impossible in the main function of the code.
2. Second commit, OK function if statement & operation parentheses are not added, resulting in operator precedence error
3. The third commit, the last loop in the main function does not use KK to guarantee dictionary-ordered output
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:lamps4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One //p[101100]: 1,2,5 closed, 3,4,6 openned A BOOLp[ -];//P[k]: can walk to state K - BOOLd[ -];//D[k]: State D was right - intr[6],w[4]; the intmark[6]; - - intn,c; - + voidGointKintC//Walk C steps from State K - { + if(!C)//get a final state A { atp[k]=true; - return; - } - - intT; -t=k^ the; Go (t,c-1); int=k^ +; Go (t,c-1); -t=k^ the; Go (t,c-1); tot=k^9; Go (t,c-1); + } - the BOOLOkintK//return if the state was right * { $ for(intI=0; i!=6; ++i)//The lamps in Group IPanax Notoginseng if(mark[i]!=-1&& (k& (1<<i))! = (mark[i]<<i))//The state of lamps in Group I was right to Mark[i] - return false; the return true; + } A the voidPrint (Ostream & out,intk) + { - BOOLt[6]; $ for(intI=0; i!=6; ++i) t[i]=k& (1<<i); $ for(intI=0; i!=n;++i) out<<t[i%6]; - out<<Endl; - } the - intMain ()Wuyi { theIfstreaminch("lamps.in"); -Ofstream out("Lamps.out"); Wu - inch>>N>>C; About $ while(c>=5) c-=2; - -p[ the]=true; -Go the, C); A intx, y; + for(intI=0; i!=6; ++i) mark[i]=-1; the for(inch>>x;x!=-1;inch>>x) - { $y= (x1)%6; the if(mark[y]==-1) mark[y]=1; the Else if(mark[y]==0) the { the out<<"Impossible"<<Endl; - inch. Close (); out. Close (); in return 0; the } the } About for(inch>>x;x!=-1;inch>>x) the { they= (x1)%6; the if(mark[y]==-1) mark[y]=0; + Else if(mark[y]==1) - { the out<<"Impossible"<<Endl;Bayi inch. Close (); out. Close (); the return 0; the } - } - //for (int i=0;i!=6;++i) cout<< "mark[" <<i<< "]=" <<mark[i]<<endl; the the BOOLt=false; the for(intkk=0, k=0; k!= -; ++k,kk=0) the { - for(intI=0; i!=6; ++i) kk+= ((BOOL) (k& (1<<i))) * (1<< (6-i-1)); the //cout<< "k="; Print (cout,k); the //cout<< "kk="; print (COUT,KK); the //cout<<endl;94 if(P[kk] && ok (KK)) {Print ( out, KK); t=true; } the } the if(!t) out<<"Impossible"<<Endl; the 98 inch. Close (); out. Close (); About return 0; -}
Usaco Section2.2 Party Lamps Problem Solving report "Icedream61"