3de32bb725 2011-11-12 kinaba: #include <iostream> 3de32bb725 2011-11-12 kinaba: #include <sstream> 3de32bb725 2011-11-12 kinaba: #include <iomanip> 3de32bb725 2011-11-12 kinaba: #include <vector> 3de32bb725 2011-11-12 kinaba: #include <string> 3de32bb725 2011-11-12 kinaba: #include <map> 3de32bb725 2011-11-12 kinaba: #include <set> 3de32bb725 2011-11-12 kinaba: #include <algorithm> 3de32bb725 2011-11-12 kinaba: #include <numeric> 3de32bb725 2011-11-12 kinaba: #include <iterator> 3de32bb725 2011-11-12 kinaba: #include <functional> 3de32bb725 2011-11-12 kinaba: #include <complex> 3de32bb725 2011-11-12 kinaba: #include <queue> 3de32bb725 2011-11-12 kinaba: #include <stack> 3de32bb725 2011-11-12 kinaba: #include <cmath> 3de32bb725 2011-11-12 kinaba: #include <cassert> 3de32bb725 2011-11-12 kinaba: #include <cstring> 3de32bb725 2011-11-12 kinaba: using namespace std; 3de32bb725 2011-11-12 kinaba: typedef long long LL; 3de32bb725 2011-11-12 kinaba: typedef complex<double> CMP; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: class CountingSeries { public: 3de32bb725 2011-11-12 kinaba: long long countThem(long long a, long long b, long long c, long long d, long long upperBound) 3de32bb725 2011-11-12 kinaba: { 3de32bb725 2011-11-12 kinaba: LL cnt = a<=upperBound ? (upperBound-a)/b+1 : 0; 3de32bb725 2011-11-12 kinaba: for(; c<=upperBound; c*=d) { 3de32bb725 2011-11-12 kinaba: if( !in_arith(a, b, c) ) 3de32bb725 2011-11-12 kinaba: ++cnt; 3de32bb725 2011-11-12 kinaba: if( d == 1 ) 3de32bb725 2011-11-12 kinaba: break; 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: return cnt; 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: bool in_arith(LL a, LL b, LL v) const 3de32bb725 2011-11-12 kinaba: { 3de32bb725 2011-11-12 kinaba: return v-a>=0 && (v-a)%b==0; 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: }; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: // BEGIN CUT HERE 3de32bb725 2011-11-12 kinaba: #include <ctime> 3de32bb725 2011-11-12 kinaba: double start_time; string timer() 3de32bb725 2011-11-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 3de32bb725 2011-11-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 3de32bb725 2011-11-12 kinaba: { os << "{ "; 3de32bb725 2011-11-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 3de32bb725 2011-11-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 3de32bb725 2011-11-12 kinaba: void verify_case(const long long& Expected, const long long& Received) { 3de32bb725 2011-11-12 kinaba: bool ok = (Expected == Received); 3de32bb725 2011-11-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 3de32bb725 2011-11-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 3de32bb725 2011-11-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 3de32bb725 2011-11-12 kinaba: #define END verify_case(_, CountingSeries().countThem(a, b, c, d, upperBound));} 3de32bb725 2011-11-12 kinaba: int main(){ 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: CASE(0) 3de32bb725 2011-11-12 kinaba: long long a = 1LL; 3de32bb725 2011-11-12 kinaba: long long b = 1LL; 3de32bb725 2011-11-12 kinaba: long long c = 1LL; 3de32bb725 2011-11-12 kinaba: long long d = 2LL; 3de32bb725 2011-11-12 kinaba: long long upperBound = 1000LL; 3de32bb725 2011-11-12 kinaba: long long _ = 1000LL; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(1) 3de32bb725 2011-11-12 kinaba: long long a = 3LL; 3de32bb725 2011-11-12 kinaba: long long b = 3LL; 3de32bb725 2011-11-12 kinaba: long long c = 1LL; 3de32bb725 2011-11-12 kinaba: long long d = 2LL; 3de32bb725 2011-11-12 kinaba: long long upperBound = 1000LL; 3de32bb725 2011-11-12 kinaba: long long _ = 343LL; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(2) 3de32bb725 2011-11-12 kinaba: long long a = 40LL; 3de32bb725 2011-11-12 kinaba: long long b = 77LL; 3de32bb725 2011-11-12 kinaba: long long c = 40LL; 3de32bb725 2011-11-12 kinaba: long long d = 100000LL; 3de32bb725 2011-11-12 kinaba: long long upperBound = 40LL; 3de32bb725 2011-11-12 kinaba: long long _ = 1LL; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(3) 3de32bb725 2011-11-12 kinaba: long long a = 452LL; 3de32bb725 2011-11-12 kinaba: long long b = 24LL; 3de32bb725 2011-11-12 kinaba: long long c = 4LL; 3de32bb725 2011-11-12 kinaba: long long d = 5LL; 3de32bb725 2011-11-12 kinaba: long long upperBound = 600LL; 3de32bb725 2011-11-12 kinaba: long long _ = 10LL; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(4) 3de32bb725 2011-11-12 kinaba: long long a = 234LL; 3de32bb725 2011-11-12 kinaba: long long b = 24LL; 3de32bb725 2011-11-12 kinaba: long long c = 377LL; 3de32bb725 2011-11-12 kinaba: long long d = 1LL; 3de32bb725 2011-11-12 kinaba: long long upperBound = 10000LL; 3de32bb725 2011-11-12 kinaba: long long _ = 408LL; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(5) 3de32bb725 2011-11-12 kinaba: long long a = 8LL; 3de32bb725 2011-11-12 kinaba: long long b = 8LL; 3de32bb725 2011-11-12 kinaba: long long c = 2LL; 3de32bb725 2011-11-12 kinaba: long long d = 2LL; 3de32bb725 2011-11-12 kinaba: long long upperBound = 80LL; 3de32bb725 2011-11-12 kinaba: long long _ = 12LL; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(6) 3de32bb725 2011-11-12 kinaba: long long a = 1LL; 3de32bb725 2011-11-12 kinaba: long long b = 12345LL; 3de32bb725 2011-11-12 kinaba: long long c = 1LL; 3de32bb725 2011-11-12 kinaba: long long d = 100000LL; 3de32bb725 2011-11-12 kinaba: long long upperBound = 1000000000000LL; 3de32bb725 2011-11-12 kinaba: long long _ = -1LL; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: // END CUT HERE