b4af6486f6 2011-05-19 kinaba: #include <iostream> b4af6486f6 2011-05-19 kinaba: #include <sstream> b4af6486f6 2011-05-19 kinaba: #include <iomanip> b4af6486f6 2011-05-19 kinaba: #include <vector> b4af6486f6 2011-05-19 kinaba: #include <string> b4af6486f6 2011-05-19 kinaba: #include <map> b4af6486f6 2011-05-19 kinaba: #include <set> b4af6486f6 2011-05-19 kinaba: #include <algorithm> b4af6486f6 2011-05-19 kinaba: #include <numeric> b4af6486f6 2011-05-19 kinaba: #include <iterator> b4af6486f6 2011-05-19 kinaba: #include <functional> b4af6486f6 2011-05-19 kinaba: #include <complex> b4af6486f6 2011-05-19 kinaba: #include <queue> b4af6486f6 2011-05-19 kinaba: #include <stack> b4af6486f6 2011-05-19 kinaba: #include <cmath> b4af6486f6 2011-05-19 kinaba: #include <cassert> b4af6486f6 2011-05-19 kinaba: #include <cstring> b4af6486f6 2011-05-19 kinaba: using namespace std; b4af6486f6 2011-05-19 kinaba: typedef long long LL; b4af6486f6 2011-05-19 kinaba: typedef complex<double> CMP; b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: class SetMultiples { public: b4af6486f6 2011-05-19 kinaba: long long smallestSubset(long long A, long long B, long long C, long long D) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: vector< pair<LL,LL> > noneed( 1, make_pair(1,100000) ); b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: LL cnt = 0; b4af6486f6 2011-05-19 kinaba: for(LL p=1; p<=100000; ++p) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: if( A<=p&&p<=B || C<=p&&p<=D ) b4af6486f6 2011-05-19 kinaba: if( need(p, A, B, C, D) ) b4af6486f6 2011-05-19 kinaba: ++cnt; b4af6486f6 2011-05-19 kinaba: if( p > 1 ) { b4af6486f6 2011-05-19 kinaba: add(noneed, p, A, B); b4af6486f6 2011-05-19 kinaba: add(noneed, p, C, D); b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: sort(noneed.begin(), noneed.end(), &my_order); b4af6486f6 2011-05-19 kinaba: LL a = count_need(noneed, A, B); b4af6486f6 2011-05-19 kinaba: LL b = count_need(noneed, C, D); b4af6486f6 2011-05-19 kinaba: return cnt + a + b; b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: static bool need(LL p, LL A, LL B, LL C, LL D) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: return !covered(p,A,B) && !covered(p,C,D); b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: static bool covered(LL p, LL A, LL B) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: LL lo = A%p==0 ? A/p : A/p+1; b4af6486f6 2011-05-19 kinaba: LL hi = B/p; b4af6486f6 2011-05-19 kinaba: return ( lo<=hi && 2<=hi ); b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: static void add(vector< pair<LL,LL> >& noneed, LL p, LL A, LL B) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: LL lo = A%p==0 ? A/p : A/p+1; b4af6486f6 2011-05-19 kinaba: LL hi = B/p; b4af6486f6 2011-05-19 kinaba: if( lo <= hi ) b4af6486f6 2011-05-19 kinaba: noneed.push_back( make_pair(lo,hi) ); b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: static bool my_order( const pair<LL,LL>& lhs, const pair<LL,LL>& rhs ) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: if( lhs.second != rhs.second ) b4af6486f6 2011-05-19 kinaba: return lhs.second > rhs.second; b4af6486f6 2011-05-19 kinaba: return lhs.first < rhs.first; b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: static LL count_need(const vector< pair<LL,LL> >& noneed, LL A, LL B) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: LL cnt = 0; b4af6486f6 2011-05-19 kinaba: LL highest_need = B; b4af6486f6 2011-05-19 kinaba: for(int i=0; i<noneed.size(); ++i) b4af6486f6 2011-05-19 kinaba: { b4af6486f6 2011-05-19 kinaba: LL lo = noneed[i].first; b4af6486f6 2011-05-19 kinaba: LL hi = noneed[i].second; b4af6486f6 2011-05-19 kinaba: if( hi < highest_need ) b4af6486f6 2011-05-19 kinaba: cnt += max(0LL, highest_need - max(hi,A-1)); b4af6486f6 2011-05-19 kinaba: highest_need = min(highest_need, lo-1); b4af6486f6 2011-05-19 kinaba: if( highest_need < A ) b4af6486f6 2011-05-19 kinaba: return cnt; b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: return cnt + (highest_need - A + 1); b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: }; b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: // BEGIN CUT HERE b4af6486f6 2011-05-19 kinaba: #include <ctime> b4af6486f6 2011-05-19 kinaba: double start_time; string timer() b4af6486f6 2011-05-19 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } b4af6486f6 2011-05-19 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) b4af6486f6 2011-05-19 kinaba: { os << "{ "; b4af6486f6 2011-05-19 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) b4af6486f6 2011-05-19 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } b4af6486f6 2011-05-19 kinaba: void verify_case(const long long& Expected, const long long& Received) { b4af6486f6 2011-05-19 kinaba: bool ok = (Expected == Received); b4af6486f6 2011-05-19 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; b4af6486f6 2011-05-19 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } b4af6486f6 2011-05-19 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); b4af6486f6 2011-05-19 kinaba: #define END verify_case(_, SetMultiples().smallestSubset(A, B, C, D));} b4af6486f6 2011-05-19 kinaba: int main(){ b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: CASE(0) b4af6486f6 2011-05-19 kinaba: long long A = 1LL; b4af6486f6 2011-05-19 kinaba: long long B = 1LL; b4af6486f6 2011-05-19 kinaba: long long C = 2LL; b4af6486f6 2011-05-19 kinaba: long long D = 2LL; b4af6486f6 2011-05-19 kinaba: long long _ = 1LL; b4af6486f6 2011-05-19 kinaba: END b4af6486f6 2011-05-19 kinaba: CASE(1) b4af6486f6 2011-05-19 kinaba: long long A = 1LL; b4af6486f6 2011-05-19 kinaba: long long B = 2LL; b4af6486f6 2011-05-19 kinaba: long long C = 3LL; b4af6486f6 2011-05-19 kinaba: long long D = 4LL; b4af6486f6 2011-05-19 kinaba: long long _ = 2LL; b4af6486f6 2011-05-19 kinaba: END b4af6486f6 2011-05-19 kinaba: CASE(2) b4af6486f6 2011-05-19 kinaba: long long A = 2LL; b4af6486f6 2011-05-19 kinaba: long long B = 3LL; b4af6486f6 2011-05-19 kinaba: long long C = 5LL; b4af6486f6 2011-05-19 kinaba: long long D = 7LL; b4af6486f6 2011-05-19 kinaba: long long _ = 3LL; b4af6486f6 2011-05-19 kinaba: END b4af6486f6 2011-05-19 kinaba: CASE(3) b4af6486f6 2011-05-19 kinaba: long long A = 1LL; b4af6486f6 2011-05-19 kinaba: long long B = 10LL; b4af6486f6 2011-05-19 kinaba: long long C = 100LL; b4af6486f6 2011-05-19 kinaba: long long D = 1000LL; b4af6486f6 2011-05-19 kinaba: long long _ = 500LL; b4af6486f6 2011-05-19 kinaba: END b4af6486f6 2011-05-19 kinaba: CASE(4) b4af6486f6 2011-05-19 kinaba: long long A = 1000000000LL; b4af6486f6 2011-05-19 kinaba: long long B = 2000000000LL; b4af6486f6 2011-05-19 kinaba: long long C = 9000000000LL; b4af6486f6 2011-05-19 kinaba: long long D = 10000000000LL; b4af6486f6 2011-05-19 kinaba: long long _ = 1254365078LL; b4af6486f6 2011-05-19 kinaba: END b4af6486f6 2011-05-19 kinaba: CASE(5) b4af6486f6 2011-05-19 kinaba: long long A = 1LL; b4af6486f6 2011-05-19 kinaba: long long B = 1000000000LL; b4af6486f6 2011-05-19 kinaba: long long C = 2000000000LL; b4af6486f6 2011-05-19 kinaba: long long D = 10000000000LL; b4af6486f6 2011-05-19 kinaba: long long _ = -1LL; b4af6486f6 2011-05-19 kinaba: END b4af6486f6 2011-05-19 kinaba: CASE(6) b4af6486f6 2011-05-19 kinaba: long long A = 1LL; b4af6486f6 2011-05-19 kinaba: long long B = 2LL; b4af6486f6 2011-05-19 kinaba: long long C = 3LL; b4af6486f6 2011-05-19 kinaba: long long D = 5LL; b4af6486f6 2011-05-19 kinaba: long long _ = 3LL; b4af6486f6 2011-05-19 kinaba: END b4af6486f6 2011-05-19 kinaba: b4af6486f6 2011-05-19 kinaba: } b4af6486f6 2011-05-19 kinaba: // END CUT HERE