724e2f81fd 2017-02-09 kinaba: #include <iostream> 724e2f81fd 2017-02-09 kinaba: #include <sstream> 724e2f81fd 2017-02-09 kinaba: #include <iomanip> 724e2f81fd 2017-02-09 kinaba: #include <vector> 724e2f81fd 2017-02-09 kinaba: #include <string> 724e2f81fd 2017-02-09 kinaba: #include <map> 724e2f81fd 2017-02-09 kinaba: #include <set> 724e2f81fd 2017-02-09 kinaba: #include <algorithm> 724e2f81fd 2017-02-09 kinaba: #include <numeric> 724e2f81fd 2017-02-09 kinaba: #include <iterator> 724e2f81fd 2017-02-09 kinaba: #include <functional> 724e2f81fd 2017-02-09 kinaba: #include <complex> 724e2f81fd 2017-02-09 kinaba: #include <queue> 724e2f81fd 2017-02-09 kinaba: #include <stack> 724e2f81fd 2017-02-09 kinaba: #include <cmath> 724e2f81fd 2017-02-09 kinaba: #include <cassert> 724e2f81fd 2017-02-09 kinaba: #include <tuple> 724e2f81fd 2017-02-09 kinaba: using namespace std; 724e2f81fd 2017-02-09 kinaba: typedef long long LL; 724e2f81fd 2017-02-09 kinaba: typedef complex<double> CMP; 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: static const unsigned MODVAL = 1000000007; 724e2f81fd 2017-02-09 kinaba: struct mint 724e2f81fd 2017-02-09 kinaba: { 724e2f81fd 2017-02-09 kinaba: unsigned val; 724e2f81fd 2017-02-09 kinaba: mint() :val(0) {} 724e2f81fd 2017-02-09 kinaba: mint(int x) :val(x%MODVAL) {} 724e2f81fd 2017-02-09 kinaba: mint(unsigned x) :val(x%MODVAL) {} 724e2f81fd 2017-02-09 kinaba: mint(LL x) :val(x%MODVAL) {} 724e2f81fd 2017-02-09 kinaba: }; 724e2f81fd 2017-02-09 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val + y.val; } 724e2f81fd 2017-02-09 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; } 724e2f81fd 2017-02-09 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 724e2f81fd 2017-02-09 kinaba: mint operator+(mint x, mint y) { return x += y; } 724e2f81fd 2017-02-09 kinaba: mint operator-(mint x, mint y) { return x -= y; } 724e2f81fd 2017-02-09 kinaba: mint operator*(mint x, mint y) { return x *= y; } 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: template<typename T> 724e2f81fd 2017-02-09 kinaba: struct DP2 724e2f81fd 2017-02-09 kinaba: { 724e2f81fd 2017-02-09 kinaba: const int N1, N2; 724e2f81fd 2017-02-09 kinaba: vector<T> data; 724e2f81fd 2017-02-09 kinaba: DP2(int N1, int N2, const T& t = T()) 724e2f81fd 2017-02-09 kinaba: : N1(N1), N2(N2), data(N1*N2, t) { 724e2f81fd 2017-02-09 kinaba: assert(data.size() * sizeof(T)<(1 << 28)); 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: T& operator()(int i1, int i2) 724e2f81fd 2017-02-09 kinaba: { 724e2f81fd 2017-02-09 kinaba: return data[(i1*N2) + i2]; 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: }; 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: class PalindromicSubseq { public: 724e2f81fd 2017-02-09 kinaba: int solve(string s) 724e2f81fd 2017-02-09 kinaba: { 724e2f81fd 2017-02-09 kinaba: const int N = s.size(); 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: DP2<mint> outer(N, N), inner(N,N); 724e2f81fd 2017-02-09 kinaba: for (int l = 0; l < N; ++l) 724e2f81fd 2017-02-09 kinaba: for (int r = l; r < N; ++r) 724e2f81fd 2017-02-09 kinaba: if (s[l] == s[r]) 724e2f81fd 2017-02-09 kinaba: outer(l, r) = inner(l, r) = 1; 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: for(int len=N; len>=1; --len) 724e2f81fd 2017-02-09 kinaba: for (int l = 0; l+len-1 < N; ++l) { 724e2f81fd 2017-02-09 kinaba: int r = l + len - 1; 724e2f81fd 2017-02-09 kinaba: if (s[l] == s[r]) { 724e2f81fd 2017-02-09 kinaba: for (int x = l - 1; x >= 0; --x) 724e2f81fd 2017-02-09 kinaba: for (int y = r + 1; y < N; ++y) 724e2f81fd 2017-02-09 kinaba: if (s[x] == s[y]) 724e2f81fd 2017-02-09 kinaba: outer(l,r) += outer(x, y); 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: for (int len = 1; len <= N; ++len) 724e2f81fd 2017-02-09 kinaba: for (int l = 0; l+len-1 < N; ++l) { 724e2f81fd 2017-02-09 kinaba: int r = l + len - 1; 724e2f81fd 2017-02-09 kinaba: if (s[l] == s[r]) { 724e2f81fd 2017-02-09 kinaba: for (int x = l + 1; x < r; ++x) 724e2f81fd 2017-02-09 kinaba: for (int y = r - 1; y >= x; --y) 724e2f81fd 2017-02-09 kinaba: if (s[x] == s[y]) 724e2f81fd 2017-02-09 kinaba: inner(l,r) += inner(x, y); 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: vector<mint> X(N); 724e2f81fd 2017-02-09 kinaba: for (int l = 0; l < N; ++l) 724e2f81fd 2017-02-09 kinaba: for (int r = l; r < N; ++r) 724e2f81fd 2017-02-09 kinaba: if (s[l] == s[r]) { 724e2f81fd 2017-02-09 kinaba: mint total = inner(l, r) * outer(l, r); 724e2f81fd 2017-02-09 kinaba: X[l] += total; 724e2f81fd 2017-02-09 kinaba: if (l != r) X[r] += total; 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: int ans = 0; 724e2f81fd 2017-02-09 kinaba: for (int i = 0; i < N; ++i) 724e2f81fd 2017-02-09 kinaba: ans ^= ((i+1)*X[i]).val; 724e2f81fd 2017-02-09 kinaba: return ans; 724e2f81fd 2017-02-09 kinaba: } 724e2f81fd 2017-02-09 kinaba: }; 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: // BEGIN CUT HERE 724e2f81fd 2017-02-09 kinaba: #include <ctime> 724e2f81fd 2017-02-09 kinaba: double start_time; string timer() 724e2f81fd 2017-02-09 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 724e2f81fd 2017-02-09 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 724e2f81fd 2017-02-09 kinaba: { os << "{ "; 724e2f81fd 2017-02-09 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 724e2f81fd 2017-02-09 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 724e2f81fd 2017-02-09 kinaba: void verify_case(const int& Expected, const int& Received) { 724e2f81fd 2017-02-09 kinaba: bool ok = (Expected == Received); 724e2f81fd 2017-02-09 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 724e2f81fd 2017-02-09 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 724e2f81fd 2017-02-09 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 724e2f81fd 2017-02-09 kinaba: #define END verify_case(_, PalindromicSubseq().solve(s));} 724e2f81fd 2017-02-09 kinaba: int main(){ 724e2f81fd 2017-02-09 kinaba: 724e2f81fd 2017-02-09 kinaba: CASE(0) 724e2f81fd 2017-02-09 kinaba: string s = "aaba"; 724e2f81fd 2017-02-09 kinaba: int _ = 30; 724e2f81fd 2017-02-09 kinaba: END 724e2f81fd 2017-02-09 kinaba: CASE(1) 724e2f81fd 2017-02-09 kinaba: string s = "abcd"; 724e2f81fd 2017-02-09 kinaba: int _ = 4; 724e2f81fd 2017-02-09 kinaba: END 724e2f81fd 2017-02-09 kinaba: CASE(2) 724e2f81fd 2017-02-09 kinaba: string s = "tcoct"; 724e2f81fd 2017-02-09 kinaba: int _ = 60; 724e2f81fd 2017-02-09 kinaba: END 724e2f81fd 2017-02-09 kinaba: CASE(3) 724e2f81fd 2017-02-09 kinaba: string s = "zyzyzzzzxzyz"; 724e2f81fd 2017-02-09 kinaba: int _ = 717; 724e2f81fd 2017-02-09 kinaba: END 724e2f81fd 2017-02-09 kinaba: CASE(4) 724e2f81fd 2017-02-09 kinaba: string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 724e2f81fd 2017-02-09 kinaba: int _ = 1025495382; 724e2f81fd 2017-02-09 kinaba: END 724e2f81fd 2017-02-09 kinaba: CASE(5) 724e2f81fd 2017-02-09 kinaba: string s = "a"; 724e2f81fd 2017-02-09 kinaba: int _ = 1; 724e2f81fd 2017-02-09 kinaba: END 724e2f81fd 2017-02-09 kinaba: CASE(6) 724e2f81fd 2017-02-09 kinaba: string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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