Artifact Content
Not logged in

Artifact 85211b8e1f2074c00a103396af8732def766fa3a


#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 OneDimensionalBalls { public:
	long long countValidGuesses(vector <int> firstPicture, vector <int> secondPicture) 
	{
		LL answer = 0;

		const int N = firstPicture.size();
		set<int> sp(secondPicture.begin(), secondPicture.end());

		for(set<int>::iterator it=sp.begin(); it!=sp.end(); ++it)
		{
			if( firstPicture[0] != *it )
			{
				const int d = abs(firstPicture[0] - *it);

				set<int> avail = sp;
				avail.erase(*it);

				vector<bool> done(N, false);
				done[0] = true;

				for(;;)
				{
					bool upd = false;
					for(int k=0; k<N; ++k)
						if( !done[k] )
						{
							bool aL = avail.count(firstPicture[k]-d)>0;
							bool aR = avail.count(firstPicture[k]+d)>0;
							if( !aL && !aR )
								goto nextIt;
							else if( aL && !aR )
								{upd=done[k]=true; avail.erase(firstPicture[k]-d);}
							else if( !aL && aR )
								{upd=done[k]=true; avail.erase(firstPicture[k]+d);}
						}
					if( !upd )
						break;
				}

				set<int> notDone;
				for(int k=0; k<N; ++k)
					if( !done[k] )
						notDone.insert(firstPicture[k]);

				LL cnt = 1;

				set<int> nnd;
				for(set<int>::iterator jt=notDone.begin(); jt!=notDone.end(); ++jt)
					if( !nnd.count(*jt) )
					{
						int chain = 1;
						for(int q=*jt+2*d; notDone.count(q); q+=2*d)
							{nnd.insert(q); ++chain;}
						cnt *= chain+1;
					}
				answer += cnt;
			}
		nextIt:;
		}

		return answer;
	}
};

// 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(_, OneDimensionalBalls().countValidGuesses(firstPicture, secondPicture));}
int main(){

CASE(0)
	int firstPicture_[] = {12,11};
	  vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPicture_)/sizeof(*firstPicture_)); 
	int secondPicture_[] = {10,11,13};
	  vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secondPicture_)/sizeof(*secondPicture_)); 
	long long _ = 3LL; 
END
CASE(1)
	int firstPicture_[] = {1,2,3};
	  vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPicture_)/sizeof(*firstPicture_)); 
	int secondPicture_[] = {1,2,3};
	  vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secondPicture_)/sizeof(*secondPicture_)); 
	long long _ = 0LL; 
END
CASE(2)
	int firstPicture_[] = {1,3};
	  vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPicture_)/sizeof(*firstPicture_)); 
	int secondPicture_[] = {1,3};
	  vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secondPicture_)/sizeof(*secondPicture_)); 
	long long _ = 1LL; 
END
CASE(3)
	int firstPicture_[] = {7234};
	  vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPicture_)/sizeof(*firstPicture_)); 
	int secondPicture_[] = {6316,689156,689160,689161,800000,1000001};
	  vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secondPicture_)/sizeof(*secondPicture_)); 
	long long _ = 6LL; 
END
CASE(4)
	int firstPicture_[] = {6,2,4};
	  vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPicture_)/sizeof(*firstPicture_)); 
	int secondPicture_[] = {1,2,3,4,5,7,8};
	  vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secondPicture_)/sizeof(*secondPicture_)); 
	long long _ = 7LL; 
END
CASE(5)
int firstPicture_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
	  vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPicture_)/sizeof(*firstPicture_)); 
	int secondPicture_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
	  vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secondPicture_)/sizeof(*secondPicture_)); 
	long long _ = -1LL; 
END
CASE(6)
	int firstPicture_[] = {1,2,3,4};
	  vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPicture_)/sizeof(*firstPicture_)); 
	int secondPicture_[] = {1,2,3,4};
	  vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secondPicture_)/sizeof(*secondPicture_)); 
	long long _ = 2LL; 
END
}
// END CUT HERE