Artifact Content
Not logged in

Artifact 21cb8b2950411a4a2de09f90a1c1b9fbf8d5950d


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long LL;

vector< vector<double> > mMul( const vector< vector<double> >& A, const vector< vector<double> >& B )
{
	const int n = A.size();

	vector< vector<double> > C(n, vector<double>(n));
	for(int i=0; i<n; ++i)
		for(int j=0; j<n; ++j) {
			double Cij = 0;
			for(int k=0; k<n; ++k)
				Cij += A[i][k] * B[k][j];
			C[i][j] = Cij;
		}
	return C;
}

vector< vector<double> > mPow( vector< vector<double> > M, int t ) // t>0
{
	vector< vector<double> > R;
	for(; t; t>>=1, M=mMul(M,M))
		if( t&1 )
			R = (R.empty() ? M : mMul(R,M));
	return R;
}

class RandomSwaps
{
public:
	double getProbability(int arrayLength, int swapCount, int a, int b) 
	{
		double P = 1 / double(arrayLength * (arrayLength-1)/2);
		double Q = 1 - (arrayLength-1)*P;
		if( a != b ) {
			vector< vector<double> > M(3, vector<double>(3));
			M[0][0] = Q;
			M[0][1] = P;
			M[0][2] = 1 - P - Q;
			M[1][0] = P;
			M[1][1] = Q;
			M[1][2] = 1 - P - Q;
			M[2][0] = P;
			M[2][1] = P;
			M[2][2] = 1 - M[2][0] - M[2][1];
			M = mPow( M, swapCount );
			return M[0][1];
		} else {
			vector< vector<double> > M(2, vector<double>(2));
			M[0][0] = Q;
			M[0][1] = 1 - M[0][0];
			M[1][0] = P;
			M[1][1] = 1 - P;
			M = mPow( M, swapCount );
			return M[0][0];
		}
	}

// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 0; int Arg3 = 0; double Arg4 = 0.6; verify_case(0, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); }
	void test_case_1() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 0; int Arg3 = 3; double Arg4 = 0.1; verify_case(1, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); }
	void test_case_2() { int Arg0 = 5; int Arg1 = 2; int Arg2 = 0; int Arg3 = 0; double Arg4 = 0.4; verify_case(2, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); }
	void test_case_3() { int Arg0 = 100; int Arg1 = 500; int Arg2 = 3; int Arg3 = 3; double Arg4 = 0.010036635745123007; verify_case(3, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); }

// END CUT HERE
};
// BEGIN CUT HERE 
int main() { RandomSwaps().run_test(-1); }
// END CUT HERE