Artifact Content
Not logged in

Artifact 3ac34d38668840d0c3b7c66280f7a3e3b0a48964


#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>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class AgeEncoding { public:
	double getRadix(int age, string c) 
	{
		int i = c.find('1');
		if( i==string::npos ) i = c.size();
		c = c.substr(i);

		int N = c.size();
		if( N == 0 )
			return -1;
		if( N == 1 )
			return (age==1 ? -2 : -1);
		if( age == 1 && c[N-1]=='1' )
			return -1;

		double L=0, R=1;
		for(;;R*=2){
			double fR = 0.0;
			for(int i=0; i<N; ++i)
				fR +=pow(R,i)*(c[N-1-i]=='1');
			if(fR>age)
				break;
		}

		for(int i=0; i<1000; ++i)
		{
			double C = (L+R)/2;

			double fL = 0.0;
			for(int i=0; i<N; ++i)
				fL +=pow(L,i)*(c[N-1-i]=='1');
			double fR = 0.0;
			for(int i=0; i<N; ++i)
				fR +=pow(R,i)*(c[N-1-i]=='1');
			double fC = 0.0;
			for(int i=0; i<N; ++i)
				fC +=pow(C,i)*(c[N-1-i]=='1');

			(age < fC ? R : L) = C;
		}
		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 double& Expected, const double& Received) {
 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff < 1e-9);
 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(_, AgeEncoding().getRadix(age, candlesLine));}
int main(){

CASE(0)
	int age = 10; 
	string candlesLine = "00010"; 
	double _ = 10.0; 
END
CASE(1)
	int age = 21; 
	string candlesLine = "10101"; 
	double _ = 2.0; 
END
CASE(2)
	int age = 6; 
	string candlesLine = "10100"; 
	double _ = 1.414213562373095; 
END
CASE(3)
	int age = 21; 
	string candlesLine = "10111111110111101111111100111111110111111111111100"; 
	double _ = 0.9685012944510603; 
END
CASE(4)
	int age = 16; 
	string candlesLine = "1"; 
	double _ = -1.0; 
END
CASE(5)
	int age = 1; 
	string candlesLine = "1"; 
	double _ = -2.0; 
END
CASE(6)
	int age = 1; 
	string candlesLine = "001000"; 
	double _ = 1.0; 
END
CASE(7)
	int age = 1; 
	string candlesLine = "110"; 
	double _ = (sqrt(5)-1)/2; 
END
CASE(8)
	int age = 1; 
	string candlesLine = "0000"; 
	double _ = -1; 
END
CASE(9)
	int age = 1; 
	string candlesLine = "11"; 
	double _ = -1; 
END
CASE(10)
	int age = 1; 
	string candlesLine = "11"; 
	double _ = -1; 
END

}
// END CUT HERE