Artifact Content
Not logged in

Artifact ede2eda09095edba08755a9e94a975b1a0ed6047


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class Suminator { public:
	int findMissing(vector <int> program, int wantedResult)
	{
		set<int> cand;

		int it = find(program.begin(), program.end(), -1) - program.begin();
		program[it] = 0;
		if( eval(program) == complex<LL>(wantedResult, 0) )
			cand.insert(0);
		program[it] = -1;

		complex<LL> r = eval(program);
		if( r.imag()==0 ) {
			if( r.real()==wantedResult )
				cand.insert(1);
		} else {
			// r.real() + r.imag()*modify == wantedResult
			if( wantedResult > r.real() )
				if( (wantedResult - r.real()) % r.imag() == 0 )
					cand.insert(int((wantedResult - r.real())/r.imag()));
		}

		return cand.empty() ? -1 : *cand.begin();
	}

	complex<LL> eval(const vector<int>& p)
	{
		stack< complex<LL> > stk;
		for(int i=0; i<p.size(); ++i)
			if(p[i] == -1)
				stk.push(complex<LL>(0,1));
			else if(p[i] != 0)
				stk.push(complex<LL>(p[i],0));
			else {
				complex<LL> a = 0; if(!stk.empty()){ a=stk.top(); stk.pop(); }
				complex<LL> b = 0; if(!stk.empty()){ b=stk.top(); stk.pop(); }
				stk.push(a+b);
			}
		complex<LL> r = 0; if(!stk.empty()){ r=stk.top(); stk.pop(); }
		return r;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, Suminator().findMissing(program, wantedResult));}
int main(){

CASE(0)
	int program_[] = {7,-1,0};
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = 10; 
	int _ = 3; 
END
CASE(1)
	int program_[] = {100, 200, 300, 0, 100, -1};
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = 600; 
	int _ = 0; 
END
CASE(2)
	int program_[] = {-1, 7, 3, 0, 1, 2, 0, 0};
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = 13; 
	int _ = 0; 
END
CASE(3)
	int program_[] = {-1, 8, 4, 0, 1, 2, 0, 0};
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = 16; 
	int _ = -1; 
END
CASE(4)
	int program_[] = {1000000000, 1000000000, 1000000000,  1000000000, -1, 0, 0, 0, 0};
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = 1000000000; 
	int _ = -1; 
END
CASE(5)
	int program_[] = {7, -1, 3, 0};
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = 3; 
	int _ = -1; 
END
/*
CASE(6)
	int program_[] = ;
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = ; 
	int _ = ; 
END
CASE(7)
	int program_[] = ;
	  vector <int> program(program_, program_+sizeof(program_)/sizeof(*program_)); 
	int wantedResult = ; 
	int _ = ; 
END
*/
}
// END CUT HERE