535b5a8c1c 2012-04-21 kinaba: #include <iostream> 535b5a8c1c 2012-04-21 kinaba: #include <sstream> 535b5a8c1c 2012-04-21 kinaba: #include <iomanip> 535b5a8c1c 2012-04-21 kinaba: #include <vector> 535b5a8c1c 2012-04-21 kinaba: #include <string> 535b5a8c1c 2012-04-21 kinaba: #include <map> 535b5a8c1c 2012-04-21 kinaba: #include <set> 535b5a8c1c 2012-04-21 kinaba: #include <algorithm> 535b5a8c1c 2012-04-21 kinaba: #include <numeric> 535b5a8c1c 2012-04-21 kinaba: #include <iterator> 535b5a8c1c 2012-04-21 kinaba: #include <functional> 535b5a8c1c 2012-04-21 kinaba: #include <complex> 535b5a8c1c 2012-04-21 kinaba: #include <queue> 535b5a8c1c 2012-04-21 kinaba: #include <stack> 535b5a8c1c 2012-04-21 kinaba: #include <cmath> 535b5a8c1c 2012-04-21 kinaba: #include <cassert> 535b5a8c1c 2012-04-21 kinaba: #include <cstring> 535b5a8c1c 2012-04-21 kinaba: #ifdef __GNUC__ 535b5a8c1c 2012-04-21 kinaba: #include <ext/hash_map> 535b5a8c1c 2012-04-21 kinaba: #define unordered_map __gnu_cxx::hash_map 535b5a8c1c 2012-04-21 kinaba: #else 535b5a8c1c 2012-04-21 kinaba: #include <unordered_map> 535b5a8c1c 2012-04-21 kinaba: #endif 535b5a8c1c 2012-04-21 kinaba: using namespace std; 535b5a8c1c 2012-04-21 kinaba: typedef long long LL; 535b5a8c1c 2012-04-21 kinaba: typedef complex<double> CMP; 535b5a8c1c 2012-04-21 kinaba: 535b5a8c1c 2012-04-21 kinaba: class ImportantSequence { public: 535b5a8c1c 2012-04-21 kinaba: int getCount(vector <int> B, string operators) 535b5a8c1c 2012-04-21 kinaba: { 535b5a8c1c 2012-04-21 kinaba: vector< pair<LL,LL> > A; 535b5a8c1c 2012-04-21 kinaba: A.push_back( make_pair(1,0) ); 535b5a8c1c 2012-04-21 kinaba: for(int i=0; i<B.size(); ++i) 535b5a8c1c 2012-04-21 kinaba: { 535b5a8c1c 2012-04-21 kinaba: LL a = A.back().first; 535b5a8c1c 2012-04-21 kinaba: LL b = A.back().second; 535b5a8c1c 2012-04-21 kinaba: LL c = B[i]; 535b5a8c1c 2012-04-21 kinaba: if( operators[i] == '+' ) { 535b5a8c1c 2012-04-21 kinaba: // (ax+b) + (px+q) = c 535b5a8c1c 2012-04-21 kinaba: A.push_back( make_pair(-a, c-b) ); 535b5a8c1c 2012-04-21 kinaba: } else { 535b5a8c1c 2012-04-21 kinaba: // (ax+b) - (px+q) = c 535b5a8c1c 2012-04-21 kinaba: A.push_back( make_pair(a, b-c) ); 535b5a8c1c 2012-04-21 kinaba: } 535b5a8c1c 2012-04-21 kinaba: } 535b5a8c1c 2012-04-21 kinaba: 535b5a8c1c 2012-04-21 kinaba: static const LL INF = (1LL<<62); 535b5a8c1c 2012-04-21 kinaba: LL xMin = 1; 535b5a8c1c 2012-04-21 kinaba: LL xMax = INF; 535b5a8c1c 2012-04-21 kinaba: for(int i=0; i<A.size(); ++i) 535b5a8c1c 2012-04-21 kinaba: { 535b5a8c1c 2012-04-21 kinaba: LL a = A[i].first; 535b5a8c1c 2012-04-21 kinaba: LL b = A[i].second; 535b5a8c1c 2012-04-21 kinaba: // ax + b > 0 535b5a8c1c 2012-04-21 kinaba: if( a == +1 ) { 535b5a8c1c 2012-04-21 kinaba: // x > -b 535b5a8c1c 2012-04-21 kinaba: xMin = max(xMin, -b+1); 535b5a8c1c 2012-04-21 kinaba: } else { 535b5a8c1c 2012-04-21 kinaba: // -x > -b == x < b 535b5a8c1c 2012-04-21 kinaba: xMax = min(xMax, b-1); 535b5a8c1c 2012-04-21 kinaba: } 535b5a8c1c 2012-04-21 kinaba: } 535b5a8c1c 2012-04-21 kinaba: 535b5a8c1c 2012-04-21 kinaba: if( xMax == INF ) 535b5a8c1c 2012-04-21 kinaba: return -1; 535b5a8c1c 2012-04-21 kinaba: return (int)max(0LL, xMax-xMin+1); 535b5a8c1c 2012-04-21 kinaba: } 535b5a8c1c 2012-04-21 kinaba: }; 535b5a8c1c 2012-04-21 kinaba: 535b5a8c1c 2012-04-21 kinaba: // BEGIN CUT HERE 535b5a8c1c 2012-04-21 kinaba: #include <ctime> 535b5a8c1c 2012-04-21 kinaba: double start_time; string timer() 535b5a8c1c 2012-04-21 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 535b5a8c1c 2012-04-21 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 535b5a8c1c 2012-04-21 kinaba: { os << "{ "; 535b5a8c1c 2012-04-21 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 535b5a8c1c 2012-04-21 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 535b5a8c1c 2012-04-21 kinaba: void verify_case(const int& Expected, const int& Received) { 535b5a8c1c 2012-04-21 kinaba: bool ok = (Expected == Received); 535b5a8c1c 2012-04-21 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 535b5a8c1c 2012-04-21 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 535b5a8c1c 2012-04-21 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 535b5a8c1c 2012-04-21 kinaba: #define END verify_case(_, ImportantSequence().getCount(B, operators));} 535b5a8c1c 2012-04-21 kinaba: int main(){ 535b5a8c1c 2012-04-21 kinaba: 535b5a8c1c 2012-04-21 kinaba: CASE(0) 535b5a8c1c 2012-04-21 kinaba: int B_[] = {3, -1, 7}; 535b5a8c1c 2012-04-21 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 535b5a8c1c 2012-04-21 kinaba: string operators = "+-+"; 535b5a8c1c 2012-04-21 kinaba: int _ = 2; 535b5a8c1c 2012-04-21 kinaba: END 535b5a8c1c 2012-04-21 kinaba: CASE(1) 535b5a8c1c 2012-04-21 kinaba: int B_[] = {1}; 535b5a8c1c 2012-04-21 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 535b5a8c1c 2012-04-21 kinaba: string operators = "-"; 535b5a8c1c 2012-04-21 kinaba: int _ = -1; 535b5a8c1c 2012-04-21 kinaba: END 535b5a8c1c 2012-04-21 kinaba: CASE(2) 535b5a8c1c 2012-04-21 kinaba: int B_[] = {1}; 535b5a8c1c 2012-04-21 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 535b5a8c1c 2012-04-21 kinaba: string operators = "+"; 535b5a8c1c 2012-04-21 kinaba: int _ = 0; 535b5a8c1c 2012-04-21 kinaba: END 535b5a8c1c 2012-04-21 kinaba: CASE(3) 535b5a8c1c 2012-04-21 kinaba: int B_[] = {10}; 535b5a8c1c 2012-04-21 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 535b5a8c1c 2012-04-21 kinaba: string operators = "+"; 535b5a8c1c 2012-04-21 kinaba: int _ = 9; 535b5a8c1c 2012-04-21 kinaba: END 535b5a8c1c 2012-04-21 kinaba: CASE(4) 535b5a8c1c 2012-04-21 kinaba: int B_[] = {540, 2012, 540, 2012, 540, 2012, 540}; 535b5a8c1c 2012-04-21 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 535b5a8c1c 2012-04-21 kinaba: string operators = "-+-+-+-"; 535b5a8c1c 2012-04-21 kinaba: int _ = 1471; 535b5a8c1c 2012-04-21 kinaba: END 535b5a8c1c 2012-04-21 kinaba: CASE(5) 535b5a8c1c 2012-04-21 kinaba: int B_[] = {1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 535b5a8c1c 2012-04-21 kinaba: 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 535b5a8c1c 2012-04-21 kinaba: 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 535b5a8c1c 2012-04-21 kinaba: 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 535b5a8c1c 2012-04-21 kinaba: 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 535b5a8c1c 2012-04-21 kinaba: 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 535b5a8c1c 2012-04-21 kinaba: 1000000000}; 535b5a8c1c 2012-04-21 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 535b5a8c1c 2012-04-21 kinaba: string operators = "------------------------------+"; 535b5a8c1c 2012-04-21 kinaba: int _ = -1; 535b5a8c1c 2012-04-21 kinaba: END 535b5a8c1c 2012-04-21 kinaba: /* 535b5a8c1c 2012-04-21 kinaba: CASE(6) 535b5a8c1c 2012-04-21 kinaba: int B_[] = ; 535b5a8c1c 2012-04-21 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 535b5a8c1c 2012-04-21 kinaba: string operators = ; 535b5a8c1c 2012-04-21 kinaba: int _ = ; 535b5a8c1c 2012-04-21 kinaba: END 535b5a8c1c 2012-04-21 kinaba: */ 535b5a8c1c 2012-04-21 kinaba: } 535b5a8c1c 2012-04-21 kinaba: // END CUT HERE