Artifact Content
Not logged in

Artifact 435de136cbd3a80e0b5a4499b487eeac2d491036


#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;

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint() :val(0) {}
	mint(int      x) :val(x%MODVAL) {}
	mint(unsigned x) :val(x%MODVAL) {}
	mint(LL       x) :val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val + y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x += y; }
mint operator-(mint x, mint y) { return x -= y; }
mint operator*(mint x, mint y) { return x *= y; }

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> data;
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) {
		assert(data.size() * sizeof(T)<(1 << 28));
	}
	T& operator()(int i1, int i2)
	{
		return data[(i1*N2) + i2];
	}
};

class PalindromicSubseq { public:
	int solve(string s)
	{
		const int N = s.size();

		DP2<mint> outer(N, N), inner(N,N);
		for (int l = 0; l < N; ++l)
			for (int r = l; r < N; ++r)
				if (s[l] == s[r])
					outer(l, r) = inner(l, r) = 1;

		for(int len=N; len>=1; --len)
			for (int l = 0; l+len-1 < N; ++l) {
				int r = l + len - 1;
				if (s[l] == s[r]) {
					for (int x = l - 1; x >= 0; --x)
						for (int y = r + 1; y < N; ++y)
							if (s[x] == s[y])
								outer(l,r) += outer(x, y);
				}
			}
		for (int len = 1; len <= N; ++len)
			for (int l = 0; l+len-1 < N; ++l) {
				int r = l + len - 1;
				if (s[l] == s[r]) {
					for (int x = l + 1; x < r; ++x)
						for (int y = r - 1; y >= x; --y)
							if (s[x] == s[y])
								inner(l,r) += inner(x, y);
				}
			}
		vector<mint> X(N);
		for (int l = 0; l < N; ++l)
		for (int r = l; r < N; ++r)
			if (s[l] == s[r]) {
				mint total = inner(l, r) * outer(l, r);
				X[l] += total;
				if (l != r) X[r] += total;
			}

		int ans = 0;
		for (int i = 0; i < N; ++i)
			ans ^= ((i+1)*X[i]).val;
		return ans;
	}
};

// 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(_, PalindromicSubseq().solve(s));}
int main(){

CASE(0)
	string s = "aaba"; 
	int _ = 30; 
END
CASE(1)
	string s = "abcd"; 
	int _ = 4; 
END
CASE(2)
	string s = "tcoct"; 
	int _ = 60; 
END
CASE(3)
	string s = "zyzyzzzzxzyz"; 
	int _ = 717; 
END
CASE(4)
	string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 
	int _ = 1025495382; 
END
CASE(5)
	string s = "a";
	int _ = 1;
END
CASE(6)
	string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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