4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const LL MODVAL = 1234567891; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ADD(LL x, LL y) { return (x+y)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL MUL(LL x, LL y) { return (x*y)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL POW(LL x, LL e) { 4fd800b3a8 2011-02-23 kinaba: LL v = 1; 4fd800b3a8 2011-02-23 kinaba: for(;e;x=MUL(x,x),e>>=1) 4fd800b3a8 2011-02-23 kinaba: if(e&1) 4fd800b3a8 2011-02-23 kinaba: v = MUL(v, x); 4fd800b3a8 2011-02-23 kinaba: return v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } 4fd800b3a8 2011-02-23 kinaba: LL C(LL n, LL k) { 4fd800b3a8 2011-02-23 kinaba: LL v = 1; 4fd800b3a8 2011-02-23 kinaba: for(LL i=1; i<=k; ++i) 4fd800b3a8 2011-02-23 kinaba: v = DIV(MUL(v, n-i+1), i); 4fd800b3a8 2011-02-23 kinaba: return v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL GSS(LL k, LL b, LL e) // k^b + k^b+1 + ... + k^e 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( b > e ) return 0; 4fd800b3a8 2011-02-23 kinaba: if( k <= 1 ) return k*(e-b+1); 4fd800b3a8 2011-02-23 kinaba: return DIV(SUB(POW(k, e+1), POW(k,b)), k-1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class TheLongPalindrome 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int count(int n, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<=k; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL aa = f(i,i,n); 4fd800b3a8 2011-02-23 kinaba: for(int j=i-1; j>=1; --j) 4fd800b3a8 2011-02-23 kinaba: aa = (i-j)%2 4fd800b3a8 2011-02-23 kinaba: ? SUB(aa, MUL(f(j,i,n), C(i,j))) 4fd800b3a8 2011-02-23 kinaba: : ADD(aa, MUL(f(j,i,n), C(i,j))); 4fd800b3a8 2011-02-23 kinaba: ans = ADD(ans, MUL(aa, C(26,i))); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL f(LL i, LL b, LL n) // i^b + i^b + i^b+1 + i^b+1 + ... + (nth) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL v = MUL(GSS(i, b, n/2), 2); 4fd800b3a8 2011-02-23 kinaba: if( n%2 == 1 ) 4fd800b3a8 2011-02-23 kinaba: v = ADD(v, POW(i, n/2+1)); 4fd800b3a8 2011-02-23 kinaba: return v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: private: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { int Arg0 = 1; int Arg1 = 1; int Arg2 = 26; verify_case(0, Arg2, count(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { int Arg0 = 2; int Arg1 = 10; int Arg2 = 52; verify_case(1, Arg2, count(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 728; verify_case(2, Arg2, count(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { int Arg0 = 44; int Arg1 = 7; int Arg2 = 240249781; verify_case(3, Arg2, count(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { TheLongPalindrome().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE