Artifact Content
Not logged in

Artifact dc8b7855f7fb41f0a3f67f0c22c5cf5ef9e2026e


#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 SetMultiples { public:
	long long smallestSubset(long long A, long long B, long long C, long long D) 
	{
		vector< pair<LL,LL> > noneed( 1, make_pair(1,100000) );

		LL cnt = 0;
		for(LL p=1; p<=100000; ++p)
		{
			if( A<=p&&p<=B || C<=p&&p<=D )
				if( need(p, A, B, C, D) )
					++cnt;
			if( p > 1 ) {
				add(noneed, p, A, B);
				add(noneed, p, C, D);
			}
		}
		sort(noneed.begin(), noneed.end(), &my_order);
		LL a = count_need(noneed, A, B);
		LL b = count_need(noneed, C, D);
		return cnt + a + b;
	}

	static bool need(LL p, LL A, LL B, LL C, LL D)
	{
		return !covered(p,A,B) && !covered(p,C,D);
	}

	static bool covered(LL p, LL A, LL B)
	{
		LL lo = A%p==0 ? A/p : A/p+1;
		LL hi = B/p;
		return ( lo<=hi && 2<=hi );
	}

	static void add(vector< pair<LL,LL> >& noneed, LL p, LL A, LL B)
	{
		LL lo = A%p==0 ? A/p : A/p+1;
		LL hi = B/p;
		if( lo <= hi )
			noneed.push_back( make_pair(lo,hi) );
	}

	static bool my_order( const pair<LL,LL>& lhs, const pair<LL,LL>& rhs )
	{
		if( lhs.second != rhs.second )
			return lhs.second > rhs.second;
		return lhs.first < rhs.first;
	}

	static LL count_need(const vector< pair<LL,LL> >& noneed, LL A, LL B)
	{
		LL cnt = 0;
		LL highest_need = B;
		for(int i=0; i<noneed.size(); ++i)
		{
			LL lo = noneed[i].first;
			LL hi = noneed[i].second;
			if( hi < highest_need )
				cnt += max(0LL, highest_need - max(hi,A-1));
			highest_need = min(highest_need, lo-1);
			if( highest_need < A )
				return cnt;
		}
		return cnt + (highest_need - A + 1);
	}
};

// 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 long long& Expected, const long long& 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(_, SetMultiples().smallestSubset(A, B, C, D));}
int main(){

CASE(0)
	long long A = 1LL; 
	long long B = 1LL; 
	long long C = 2LL; 
	long long D = 2LL; 
	long long _ = 1LL; 
END
CASE(1)
	long long A = 1LL; 
	long long B = 2LL; 
	long long C = 3LL; 
	long long D = 4LL; 
	long long _ = 2LL; 
END
CASE(2)
	long long A = 2LL; 
	long long B = 3LL; 
	long long C = 5LL; 
	long long D = 7LL; 
	long long _ = 3LL; 
END
CASE(3)
	long long A = 1LL; 
	long long B = 10LL; 
	long long C = 100LL; 
	long long D = 1000LL; 
	long long _ = 500LL; 
END
CASE(4)
	long long A = 1000000000LL; 
	long long B = 2000000000LL; 
	long long C = 9000000000LL; 
	long long D = 10000000000LL; 
	long long _ = 1254365078LL; 
END
CASE(5)
	long long A = 1LL; 
	long long B = 1000000000LL; 
	long long C = 2000000000LL; 
	long long D = 10000000000LL; 
	long long _ = -1LL; 
END
CASE(6)
	long long A = 1LL; 
	long long B = 2LL; 
	long long C = 3LL; 
	long long D = 5LL; 
	long long _ = 3LL; 
END

}
// END CUT HERE