//TArith.cc // ecna 2009 //problem A - Arithmetically Challenged #include using namespace std; struct node { int num; node * next; }; node * USED; //USED stores list of numbers in ascending order int Len, Low, High; void PrintList(){ node * ptr = USED; cout<<":"; while(ptr !=NULL){ cout<num<<' '; ptr = ptr->next; } cout<num = N; if (ptr == NULL){ X->next = NULL; USED = X; return; } if(ptr->num == N) return; if(ptr->num > N){ // add to front of list X->next = ptr; USED = X; return; } //must search list while(ptr->next != NULL && ptr->next->numnext; if(ptr->next == NULL){ // add X at end of list ptr->next = X; X->next = NULL; return; } if(ptr->next->num !=N){ // add X after ptr X->next = ptr->next; ptr->next = X; return; } // else N already in list, so do nothing } void Three(int A, int C){ Insert(A+C); Insert(A-C); Insert(C-A); Insert(A*C); if (C!=0 && A % C == 0) Insert(A/C); if(A !=0 && C % A == 0) Insert(C/A); //PrintList(); } void Two(int A, int B, int C){ Three(A+B,C); Three(A-B,C); Three(B-A,C); Three(A*B,C); if (B!=0 && A % B == 0) Three(A/B, C); if (A!=0 && B % A == 0) Three(B/A,C); } void PairOff(int A, int B, int C, int D){ Three(A+B, C+D); Three(A+B, C-D); Three(A+B, C*D); Three(A-B, C+D); Three(A-B, C-D); Three(A-B, C*D); Three(A*B, C+D); Three(A*B, C-D); Three(A*B, C*D); if(B!=0 && A%B==0){ Three(A/B, C+D); Three(A/B, C-D); Three(A/B, C*D); } if(D!=0 && C%D==0){ Three(A+B,C/D); Three(A-B,C/D); Three(A*B,C/D); if(B!=0 && A%B==0) Three(A/B,C/D); } } //check list USED for longest run void FindLongRun(){ node * ptr = USED; int Start; while(1){ Start = ptr -> num; while(ptr->next != NULL && ptr->next->num == ptr->num + 1) ptr = ptr->next; if(ptr->num - Start + 1 >= Len){ Len = ptr->num - Start + 1; Low = Start; High = ptr-> num; } if(ptr->next == NULL) return; ptr = ptr->next; } } // return list to free space void ClearUsed(){ node * ptr = USED, * ptr2; while(ptr->next != NULL){ ptr2 = ptr; ptr = ptr->next; delete(ptr2); } delete(ptr); } int main(){ int CaseNo = 1; int N[4]; cin>>N[0]>>N[1]>>N[2]>>N[3]; while(N[1]>0 && N[2]>0 && N[3]>0 && N[0]>0){ USED = NULL; // generate all permutation os N[0..3] for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(i != j) for(int k=0;k<4;k++) if(k!=i && k != j) for(int l=0;l<4;l++) if(l != k && l !=j && l != i) { // have a permutation now // do ((A?B)?C)?D operations Two(N[i]+N[j], N[k], N[l]); Two(N[i]-N[j], N[k], N[l]); Two(N[i]*N[j], N[k], N[l]); if (N[i] % N[j] == 0) Two(N[i]/N[j], N[k], N[l]); //now do (A?B)?(C?D) operations PairOff(N[i],N[j],N[k],N[l]); } Len = 0; Low = High = 0; FindLongRun(); cout<<"Case "<>N[0]>>N[1]>>N[2]>>N[3]; } return 0; }