File Annotation
Not logged in
f7e7bde312 2012-09-01        kinaba: #include <iostream>
f7e7bde312 2012-09-01        kinaba: #include <sstream>
f7e7bde312 2012-09-01        kinaba: #include <iomanip>
f7e7bde312 2012-09-01        kinaba: #include <vector>
f7e7bde312 2012-09-01        kinaba: #include <string>
f7e7bde312 2012-09-01        kinaba: #include <map>
f7e7bde312 2012-09-01        kinaba: #include <set>
f7e7bde312 2012-09-01        kinaba: #include <algorithm>
f7e7bde312 2012-09-01        kinaba: #include <numeric>
f7e7bde312 2012-09-01        kinaba: #include <iterator>
f7e7bde312 2012-09-01        kinaba: #include <functional>
f7e7bde312 2012-09-01        kinaba: #include <complex>
f7e7bde312 2012-09-01        kinaba: #include <queue>
f7e7bde312 2012-09-01        kinaba: #include <stack>
f7e7bde312 2012-09-01        kinaba: #include <cmath>
f7e7bde312 2012-09-01        kinaba: #include <cassert>
f7e7bde312 2012-09-01        kinaba: using namespace std;
f7e7bde312 2012-09-01        kinaba: typedef long long LL;
f7e7bde312 2012-09-01        kinaba: typedef long double LD;
f7e7bde312 2012-09-01        kinaba: typedef complex<LD> CMP;
f7e7bde312 2012-09-01        kinaba: 
f7e7bde312 2012-09-01        kinaba: class Suminator { public:
f7e7bde312 2012-09-01        kinaba: 	int findMissing(vector <int> program, int wantedResult)
f7e7bde312 2012-09-01        kinaba: 	{
f7e7bde312 2012-09-01        kinaba: 		set<int> cand;
f7e7bde312 2012-09-01        kinaba: 
f7e7bde312 2012-09-01        kinaba: 		int it = find(program.begin(), program.end(), -1) - program.begin();
f7e7bde312 2012-09-01        kinaba: 		program[it] = 0;
f7e7bde312 2012-09-01        kinaba: 		if( eval(program) == complex<LL>(wantedResult, 0) )
f7e7bde312 2012-09-01        kinaba: 			cand.insert(0);
f7e7bde312 2012-09-01        kinaba: 		program[it] = -1;
f7e7bde312 2012-09-01        kinaba: 
f7e7bde312 2012-09-01        kinaba: 		complex<LL> r = eval(program);
f7e7bde312 2012-09-01        kinaba: 		if( r.imag()==0 ) {
f7e7bde312 2012-09-01        kinaba: 			if( r.real()==wantedResult )
f7e7bde312 2012-09-01        kinaba: 				cand.insert(1);
f7e7bde312 2012-09-01        kinaba: 		} else {
f7e7bde312 2012-09-01        kinaba: 			// r.real() + r.imag()*modify == wantedResult
f7e7bde312 2012-09-01        kinaba: 			if( wantedResult > r.real() )
f7e7bde312 2012-09-01        kinaba: 				if( (wantedResult - r.real()) % r.imag() == 0 )
f7e7bde312 2012-09-01        kinaba: 					cand.insert(int((wantedResult - r.real())/r.imag()));
f7e7bde312 2012-09-01        kinaba: 		}
f7e7bde312 2012-09-01        kinaba: 
f7e7bde312 2012-09-01        kinaba: 		return cand.empty() ? -1 : *cand.begin();
f7e7bde312 2012-09-01        kinaba: 	}
f7e7bde312 2012-09-01        kinaba: 
f7e7bde312 2012-09-01        kinaba: 	complex<LL> eval(const vector<int>& p)
f7e7bde312 2012-09-01        kinaba: 	{
f7e7bde312 2012-09-01        kinaba: 		stack< complex<LL> > stk;
f7e7bde312 2012-09-01        kinaba: 		for(int i=0; i<p.size(); ++i)
f7e7bde312 2012-09-01        kinaba: 			if(p[i] == -1)
f7e7bde312 2012-09-01        kinaba: 				stk.push(complex<LL>(0,1));
f7e7bde312 2012-09-01        kinaba: 			else if(p[i] != 0)
f7e7bde312 2012-09-01        kinaba: 				stk.push(complex<LL>(p[i],0));
f7e7bde312 2012-09-01        kinaba: 			else {
f7e7bde312 2012-09-01        kinaba: 				complex<LL> a = 0; if(!stk.empty()){ a=stk.top(); stk.pop(); }
f7e7bde312 2012-09-01        kinaba: 				complex<LL> b = 0; if(!stk.empty()){ b=stk.top(); stk.pop(); }
f7e7bde312 2012-09-01        kinaba: 				stk.push(a+b);
f7e7bde312 2012-09-01        kinaba: 			}
f7e7bde312 2012-09-01        kinaba: 		complex<LL> r = 0; if(!stk.empty()){ r=stk.top(); stk.pop(); }
f7e7bde312 2012-09-01        kinaba: 		return r;
f7e7bde312 2012-09-01        kinaba: 	}
f7e7bde312 2012-09-01        kinaba: };
f7e7bde312 2012-09-01        kinaba: 
f7e7bde312 2012-09-01        kinaba: // BEGIN CUT HERE
f7e7bde312 2012-09-01        kinaba: #include <ctime>
f7e7bde312 2012-09-01        kinaba: double start_time; string timer()
f7e7bde312 2012-09-01        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
f7e7bde312 2012-09-01        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
f7e7bde312 2012-09-01        kinaba:  { os << "{ ";
f7e7bde312 2012-09-01        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
f7e7bde312 2012-09-01        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
f7e7bde312 2012-09-01        kinaba: void verify_case(const int& Expected, const int& Received) {
f7e7bde312 2012-09-01        kinaba:  bool ok = (Expected == Received);
f7e7bde312 2012-09-01        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
f7e7bde312 2012-09-01        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
f7e7bde312 2012-09-01        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
f7e7bde312 2012-09-01        kinaba: #define END	 verify_case(_, Suminator().findMissing(program, wantedResult));}
f7e7bde312 2012-09-01        kinaba: int main(){
f7e7bde312 2012-09-01        kinaba: 
f7e7bde312 2012-09-01        kinaba: CASE(0)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = {7,-1,0};
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = 10;
f7e7bde312 2012-09-01        kinaba: 	int _ = 3;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: CASE(1)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = {100, 200, 300, 0, 100, -1};
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = 600;
f7e7bde312 2012-09-01        kinaba: 	int _ = 0;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: CASE(2)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = {-1, 7, 3, 0, 1, 2, 0, 0};
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = 13;
f7e7bde312 2012-09-01        kinaba: 	int _ = 0;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: CASE(3)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = {-1, 8, 4, 0, 1, 2, 0, 0};
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = 16;
f7e7bde312 2012-09-01        kinaba: 	int _ = -1;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: CASE(4)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = {1000000000, 1000000000, 1000000000,  1000000000, -1, 0, 0, 0, 0};
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = 1000000000;
f7e7bde312 2012-09-01        kinaba: 	int _ = -1;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: CASE(5)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = {7, -1, 3, 0};
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = 3;
f7e7bde312 2012-09-01        kinaba: 	int _ = -1;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: /*
f7e7bde312 2012-09-01        kinaba: CASE(6)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = ;
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = ;
f7e7bde312 2012-09-01        kinaba: 	int _ = ;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: CASE(7)
f7e7bde312 2012-09-01        kinaba: 	int program_[] = ;
f7e7bde312 2012-09-01        kinaba: 	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_));
f7e7bde312 2012-09-01        kinaba: 	int wantedResult = ;
f7e7bde312 2012-09-01        kinaba: 	int _ = ;
f7e7bde312 2012-09-01        kinaba: END
f7e7bde312 2012-09-01        kinaba: */
f7e7bde312 2012-09-01        kinaba: }
f7e7bde312 2012-09-01        kinaba: // END CUT HERE