Artifact Content
Not logged in

Artifact d3ffd1bee1dc3b17ef3a7fe898c3046f9c251a3c


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class TheSquareRootDilemma { public:
	int countPairs(int N, int M)
	{
		static const int X = 279;
		vector<bool> isp(X+1, true);
		vector<int> ps;
		for(int p=2; p<=X; ++p)
			if( isp[p] ) {
				ps.push_back(p);
				for(int q=p+p; q<=X; q+=p)
					isp[q] = false;
			}

		map<vector<int>, int> AA;
		for(int A=1; A<=N; ++A)
		{
			int x = A;
			vector<int> v;
			for(int i=0; i<ps.size(); ++i) {
				int p = ps[i];
				int k = 0;
				while(x%p==0) {k++; x/=p;}
				v.push_back(k&1);
			}
			if(x==1)
				AA[v]++;
		}

		int result = 0;
		for(int B=1; B<=M; ++B)
		{
			int x = B;
			vector<int> v;
			for(int i=0; i<ps.size(); ++i) {
				int p = ps[i];
				int k = 0;
				while(x%p==0) {k++; x/=p;}
				v.push_back(k&1);
			}
			if(x==1)
				result += AA[v];
		}
		return result;
	}
};

// 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 int& Expected, const int& 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(_, TheSquareRootDilemma().countPairs(N, M));}
int main(){

CASE(0)
	int N = 2; 
	int M = 2; 
	int _ = 2; 
END
CASE(1)
	int N = 10; 
	int M = 1; 
	int _ = 3; 
END
CASE(2)
	int N = 3; 
	int M = 8; 
	int _ = 5; 
END
CASE(3)
	int N = 100; 
	int M = 100; 
	int _ = 310; 
END
CASE(4)
	int N = 77777; 
	int M = 77777; 
	int _ = -1; 
END
/*
CASE(5)
	int N = ; 
	int M = ; 
	int _ = ; 
END
*/
}
// END CUT HERE