Artifact Content
Not logged in

Artifact 8d41104c0e490245b54bc103cb0cdc8b44f175de


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

struct SaComp {
	const int sp, *sr, srlen;
	SaComp(int sp, const vector<int>& sr) : sp(sp), sr(&sr[0]), srlen(sr.size()) {}
	bool operator()(int a, int b) const
	  { return make_pair(sr[a], a+sp<srlen?sr[a+sp]:0x7fffffff)
	         < make_pair(sr[b], b+sp<srlen?sr[b+sp]:0x7fffffff); }
};

template<typename RanIt>
vector<int> compute_suffix_array(RanIt beg, RanIt end)
{
	const int N = end - beg;

	vector<int> sa(N);
	vector<int> sort_rank(beg, end);
	for(int i=0; i<N; ++i)
		sa[i] = i;

	sort(sa.begin(), sa.end(), SaComp(0, sort_rank));
	for(int sorted_prefix=1; sorted_prefix<N; sorted_prefix*=2)
	{
		SaComp cmp(sorted_prefix, sort_rank);
		sort(sa.begin(), sa.end(), cmp);

		vector<int> block_id(N);
		for(int i=1; i<N; ++i)
			block_id[i] = block_id[i-1] + (cmp(sa[i-1], sa[i]) ? 1 : 0);
		for(int i=0; i<N; ++i)
			sort_rank[sa[i]] = block_id[i];
	}
	return sa;
}

vector<int> inv_sa(const vector<int>& sa)
{
	vector<int> isa(sa.size());
	for(int i=0; i<sa.size(); ++i)
		isa[sa[i]] = i;
	return isa;
}

template<typename RanIte>
vector<int> longest_common_prefix(RanIte beg, RanIte end, const vector<int>& sa)
{
	const int N = sa.size();
	vector<int> lcp(N);
	vector<int> inv = inv_sa(sa);

	int len = 0;
	for(int i=0; i<N; ++i) {
		int sa_idx = inv[i];
		if( sa_idx == 0 )
			lcp[sa_idx] = -1;
		else {
			for(int k=sa[sa_idx-1]; i+len<N && k+len<N && *(beg+i+len)==*(beg+k+len);)
				++len;
			lcp[sa_idx] = len;
		}
		if(len) --len;
	}
	return lcp;
}

class StringsNightmareAgain { public:
	long long UniqueSubstrings(int a, int b, int c, int d, int n)
	{
		string S(n,'a');
		for(int i=0; i<a; ++i) {
			b = int((LL(b)*c+d)%n);
			S[b] = 'b';
		}
		return solve(S);
	}

	LL solve(const string& S)
	{
		vector<int> sa = compute_suffix_array(S.begin(), S.end());
		vector<int> lcp = longest_common_prefix(S.begin(), S.end(), sa);

		for(int i=0; i+1<sa.size(); ++i)
		{

		}
		return 0;
	}
};

// 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(_, StringsNightmareAgain().UniqueSubstrings(a, b, c, d, n));}
int main(){

CASE(0)
	int a = 0; 
	int b = 0; 
	int c = 0; 
	int d = 0; 
	int n = 4; 
	long long _ = 2LL; 
END
CASE(1)
	int a = 2; 
	int b = 3; 
	int c = 1; 
	int d = 1; 
	int n = 6; 
	long long _ = 3LL; 
END
CASE(2)
	int a = 4; 
	int b = 3; 
	int c = 1; 
	int d = 1; 
	int n = 6; 
	long long _ = 3LL; 
END
CASE(3)
	int a = 4; 
	int b = 3; 
	int c = 3; 
	int d = 3; 
	int n = 10; 
	long long _ = 5LL; 
END
CASE(4)
	int a = 5; 
	int b = 3; 
	int c = 2; 
	int d = 3; 
	int n = 11; 
	long long _ = 9LL; 
END
CASE(5)
	int a = 10; 
	int b = 1000000; 
	int c = 1000000; 
	int d = 1; 
	int n = 51; 
	long long _ = 63LL; 
END
/*
CASE(6)
	int a = ; 
	int b = ; 
	int c = ; 
	int d = ; 
	int n = ; 
	long long _ = LL; 
END
CASE(7)
	int a = ; 
	int b = ; 
	int c = ; 
	int d = ; 
	int n = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE