Usaco 2.2.4 party lamps solution report

Source: Internet
Author: User

Party lamps
IOI 98

To brighten up the gala dinner of the IOI '98 we have a setN(10 <= n <= 100) colored lamps numbered from 1N.

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., 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 executions of the N lamps that are consistent with the given information, without repetitions.

Program name: lampsinput 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)
101-17 -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 invocations (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 distributions, output a single line with the single word 'impossible'

Sample output (File lamps. out)
000000000001010101010110110110

In this case, there are three possible final invocations:

  • All lamps are off
  • Lamps 1, 4, 7, 10 are off and lamps 2, 3, 5, 6, 8, 9 areOn.
  • Lamps 1, 3, 5, 7, 9 are off and lamps 2, 4, 6, 8, 10 areOn.

The question is to give the number of lights, and the operating rules of the lights, and after the C-step operation, part of the lights are on and part of the lights are off, to find the final possibility of the lights, if 'impossible 'is not output'

  • Button 1: When you press this button, all the lights will be changed: the lights that were originally on will go off and the lights that were originally turned off will be lit.
  • Button 2: When you press this button, all the lights with odd numbers are changed.
  • Button 3: When you press this button, all lights with even numbers are changed.
  • Button 4: When you press this button, all the lights whose serial numbers are 3 * k + 1 (k> = 0) will be changed. Example:, 7...

Note that when each operation is performed twice, the effect is the same as that of no implementation, so c greater than 4 can be reduced to 3 or 4;

Then, you can start to search for any possible situations after one-step operations without a lamp. You can see that step C has been performed. If the problem conditions are met, save the data and search for the next group.

/* ID:shiryuw1 PROG:lamps LANG:C++ */ #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int op[105]; int cl[105]; int n; int c; int clk=0; int opk=0; bool found=false; struct prog{     int a1;     int a2;     int a3;     int a4;     bool str[105]; }; prog ans[10000]; int p=0; bool istrue(bool* lap) {     int i;     for(i=0;i<opk;i++)     {         if(lap[op[i]-1]!=true)             return false;     }     for(i=0;i<clk;i++)     {         if(lap[cl[i]-1]!=false)             return false;     }     return true; } void change(bool* lap,int k) {     int st=0;     int ad=k;     if(k==4)     {         st=1;         ad=2;     }     int i;     for(i=st;i<n;i+=ad)         lap[i]=!lap[i]; } void DFS(bool *lap,int k) {     int i,j;     if(k/2==c/2)     {         if(istrue(lap))         {                         for(j=0;j<n;j++)                 ans[p].str[j]=lap[j];             for(j=0;j<25;j++)             {                 ans[p].a1=ans[p].a1*2+ans[p].str[j];                            }            for(j=25;j<50;j++)             {                ans[p].a2=ans[p].a2*2+ans[p].str[j];                             }            for(j=50;j<75;j++)             {                 ans[p].a3=ans[p].a3*2+ans[p].str[j];                             }            for(j=75;j<100;j++)             {                 ans[p].a4=ans[p].a4*2+ans[p].str[j];             }             p++;             found=true;         }         return ;     }     for(i=1;i<=4;i++)     {             bool tmp[105];        for(j=0;j<n;j++)         {             tmp[j]=lap[j];         }         change(tmp,i);         DFS(tmp,k+1);     } } int cmp(const void *a,const void *b) {     if((*(prog *)a).a1!=(*(prog *)b).a1)         return (*(prog *)a).a1-(*(prog *)b).a1;     if((*(prog *)a).a2!=(*(prog *)b).a2)         return (*(prog *)a).a2-(*(prog *)b).a2;     if((*(prog *)a).a3!=(*(prog *)b).a3)         return (*(prog *)a).a3-(*(prog *)b).a3;     return (*(prog *)a).a4-(*(prog *)b).a4;} int main() {    memset(ans,0,sizeof(ans));     bool lap[105];     freopen("lamps.in","r",stdin);     freopen("lamps.out","w",stdout);     memset(lap,true,sizeof(lap));     cin>>n;     cin>>c;     while(1)     {         cin>>op[opk];                 if(op[opk]==-1)             break;         opk++;     }     while(1)     {         cin>>cl[clk];                 if(cl[clk]==-1)             break;         clk++;     }     if(c>4)     {         if(c%2)             c=3;         else             c=4;     }     DFS(lap,0);     int i,j;     if(!found)         cout<<"IMPOSSIBLE"<<endl;     else     {         qsort(ans,p,sizeof(ans[0]),cmp);         for(i=0;i<p;i++)         {             if(i&&ans[i].a1==ans[i-1].a1&&ans[i].a2==ans[i-1].a2&&ans[i].a3==ans[i-1].a3&&ans[i].a4==ans[i-1].a4)                 continue;             for(j=0;j<n;j++)             {                                 cout<<ans[i].str[j];             }             cout<<endl;         }     }     return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.