2e8a7a70fd 2014-12-09 kinaba: #include <iostream> 2e8a7a70fd 2014-12-09 kinaba: #include <sstream> 2e8a7a70fd 2014-12-09 kinaba: #include <iomanip> 2e8a7a70fd 2014-12-09 kinaba: #include <vector> 2e8a7a70fd 2014-12-09 kinaba: #include <string> 2e8a7a70fd 2014-12-09 kinaba: #include <map> 2e8a7a70fd 2014-12-09 kinaba: #include <set> 2e8a7a70fd 2014-12-09 kinaba: #include <algorithm> 2e8a7a70fd 2014-12-09 kinaba: #include <numeric> 2e8a7a70fd 2014-12-09 kinaba: #include <iterator> 2e8a7a70fd 2014-12-09 kinaba: #include <functional> 2e8a7a70fd 2014-12-09 kinaba: #include <complex> 2e8a7a70fd 2014-12-09 kinaba: #include <queue> 2e8a7a70fd 2014-12-09 kinaba: #include <stack> 2e8a7a70fd 2014-12-09 kinaba: #include <cmath> 2e8a7a70fd 2014-12-09 kinaba: #include <cassert> 2e8a7a70fd 2014-12-09 kinaba: #include <tuple> 2e8a7a70fd 2014-12-09 kinaba: using namespace std; 2e8a7a70fd 2014-12-09 kinaba: typedef long long LL; 2e8a7a70fd 2014-12-09 kinaba: typedef complex<double> CMP; 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: class BoardFolding { public: 2e8a7a70fd 2014-12-09 kinaba: int howMany(int N, int M, vector <string> compressedPaper) 2e8a7a70fd 2014-12-09 kinaba: { 2e8a7a70fd 2014-12-09 kinaba: // decode 2e8a7a70fd 2014-12-09 kinaba: int tonumber[128]; 2e8a7a70fd 2014-12-09 kinaba: for(char c='0'; c<='9'; ++c) tonumber[c] = c-'0'; 2e8a7a70fd 2014-12-09 kinaba: for(char c='a'; c<='z'; ++c) tonumber[c] = c-'a' + 10; 2e8a7a70fd 2014-12-09 kinaba: for(char c='A'; c<='Z'; ++c) tonumber[c] = c-'A' + 36; 2e8a7a70fd 2014-12-09 kinaba: tonumber['#'] = 62; 2e8a7a70fd 2014-12-09 kinaba: tonumber['@'] = 63; 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: vector<vector<int>> P(N, vector<int>(M)); 2e8a7a70fd 2014-12-09 kinaba: for(int i=0; i<N; ++i) 2e8a7a70fd 2014-12-09 kinaba: for(int j=0; j<M; ++j) 2e8a7a70fd 2014-12-09 kinaba: P[i][j] = (tonumber[compressedPaper[i][j / 6]] >> (j % 6)) % 2; 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: // solve 2e8a7a70fd 2014-12-09 kinaba: return solve(N, M, P); 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: int solve(int H, int W, const vector<vector<int>>& P) 2e8a7a70fd 2014-12-09 kinaba: { 2e8a7a70fd 2014-12-09 kinaba: // transpose 2e8a7a70fd 2014-12-09 kinaba: vector<vector<int>> PT(W, vector<int>(H)); 2e8a7a70fd 2014-12-09 kinaba: for(int y=0; y<H; ++y) 2e8a7a70fd 2014-12-09 kinaba: for(int x=0; x<W; ++x) 2e8a7a70fd 2014-12-09 kinaba: PT[x][y] = P[y][x]; 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: // solve_each 2e8a7a70fd 2014-12-09 kinaba: return solve_one(P) * solve_one(PT); 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: int solve_one(const vector<vector<int>>& P) 2e8a7a70fd 2014-12-09 kinaba: { 2e8a7a70fd 2014-12-09 kinaba: // hash 2e8a7a70fd 2014-12-09 kinaba: vector<int> PH; 2e8a7a70fd 2014-12-09 kinaba: map<vector<int>, int> hash; 2e8a7a70fd 2014-12-09 kinaba: for(const auto& Pi: P) { 2e8a7a70fd 2014-12-09 kinaba: if(!hash.count(Pi)) { 2e8a7a70fd 2014-12-09 kinaba: int id = hash.size(); 2e8a7a70fd 2014-12-09 kinaba: hash[Pi] = id; 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: PH.push_back(hash[Pi]); 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: // solve 2e8a7a70fd 2014-12-09 kinaba: return solve_one(PH); 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: int solve_one(const vector<int>& P) 2e8a7a70fd 2014-12-09 kinaba: { 2e8a7a70fd 2014-12-09 kinaba: const int N = P.size(); 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: vector<int> ok_range(N); 2e8a7a70fd 2014-12-09 kinaba: for(int m=0; m<N; ++m) 2e8a7a70fd 2014-12-09 kinaba: for(int w=1; m-w>=0 && m+w<=N; ++w) { 2e8a7a70fd 2014-12-09 kinaba: if(P[m-w] != P[m+w-1]) 2e8a7a70fd 2014-12-09 kinaba: break; 2e8a7a70fd 2014-12-09 kinaba: ok_range[m] = w; 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: vector<vector<bool>> vis(N, vector<bool>(N+1)); 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: function<void(int,int)> rec; 2e8a7a70fd 2014-12-09 kinaba: rec = [&](int s, int e) { 2e8a7a70fd 2014-12-09 kinaba: if(vis[s][e]) 2e8a7a70fd 2014-12-09 kinaba: return; 2e8a7a70fd 2014-12-09 kinaba: vis[s][e] = true; 2e8a7a70fd 2014-12-09 kinaba: for(int m=s+1; m<e; ++m) 2e8a7a70fd 2014-12-09 kinaba: if(ok_range[m] >= min(m-s, e-m)) { 2e8a7a70fd 2014-12-09 kinaba: if(m-s <= e-m) 2e8a7a70fd 2014-12-09 kinaba: rec(m, e); 2e8a7a70fd 2014-12-09 kinaba: if(e-m <= m-s) 2e8a7a70fd 2014-12-09 kinaba: rec(s, m); 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: }; 2e8a7a70fd 2014-12-09 kinaba: rec(0, N); 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: int cnt = 0; 2e8a7a70fd 2014-12-09 kinaba: for(int s=0; s<N; ++s) 2e8a7a70fd 2014-12-09 kinaba: for(int e=s+1; e<=N; ++e) 2e8a7a70fd 2014-12-09 kinaba: if(vis[s][e] == 1) 2e8a7a70fd 2014-12-09 kinaba: ++cnt; 2e8a7a70fd 2014-12-09 kinaba: return cnt; 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: }; 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: // BEGIN CUT HERE 2e8a7a70fd 2014-12-09 kinaba: #include <ctime> 2e8a7a70fd 2014-12-09 kinaba: double start_time; string timer() 2e8a7a70fd 2014-12-09 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2e8a7a70fd 2014-12-09 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2e8a7a70fd 2014-12-09 kinaba: { os << "{ "; 2e8a7a70fd 2014-12-09 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2e8a7a70fd 2014-12-09 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2e8a7a70fd 2014-12-09 kinaba: void verify_case(const int& Expected, const int& Received) { 2e8a7a70fd 2014-12-09 kinaba: bool ok = (Expected == Received); 2e8a7a70fd 2014-12-09 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2e8a7a70fd 2014-12-09 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2e8a7a70fd 2014-12-09 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2e8a7a70fd 2014-12-09 kinaba: #define END verify_case(_, BoardFolding().howMany(N, M, compressedPaper));} 2e8a7a70fd 2014-12-09 kinaba: int main(){ 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: CASE(1) 2e8a7a70fd 2014-12-09 kinaba: int N = 2; 2e8a7a70fd 2014-12-09 kinaba: int M = 7; 2e8a7a70fd 2014-12-09 kinaba: string compressedPaper_[] = {"@@", "@@"}; 2e8a7a70fd 2014-12-09 kinaba: vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 2e8a7a70fd 2014-12-09 kinaba: int _ = 84; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(0) 2e8a7a70fd 2014-12-09 kinaba: int N = 2; 2e8a7a70fd 2014-12-09 kinaba: int M = 2; 2e8a7a70fd 2014-12-09 kinaba: string compressedPaper_[] = {"1", "3"}; 2e8a7a70fd 2014-12-09 kinaba: vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 2e8a7a70fd 2014-12-09 kinaba: int _ = 1; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(2) 2e8a7a70fd 2014-12-09 kinaba: int N = 4; 2e8a7a70fd 2014-12-09 kinaba: int M = 4; 2e8a7a70fd 2014-12-09 kinaba: string compressedPaper_[] = {"6", "9", "9", "6"}; 2e8a7a70fd 2014-12-09 kinaba: vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 2e8a7a70fd 2014-12-09 kinaba: int _ = 9; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(3) 2e8a7a70fd 2014-12-09 kinaba: int N = 6; 2e8a7a70fd 2014-12-09 kinaba: int M = 1; 2e8a7a70fd 2014-12-09 kinaba: string compressedPaper_[] = {"0", "2", "6", "@", "4", "A"}; 2e8a7a70fd 2014-12-09 kinaba: vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 2e8a7a70fd 2014-12-09 kinaba: int _ = 6; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(4) 2e8a7a70fd 2014-12-09 kinaba: int N = 3; 2e8a7a70fd 2014-12-09 kinaba: int M = 3; 2e8a7a70fd 2014-12-09 kinaba: string compressedPaper_[] = {"0", "2", "0"} 2e8a7a70fd 2014-12-09 kinaba: ; 2e8a7a70fd 2014-12-09 kinaba: vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 2e8a7a70fd 2014-12-09 kinaba: int _ = 1; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: /* 2e8a7a70fd 2014-12-09 kinaba: CASE(5) 2e8a7a70fd 2014-12-09 kinaba: int N = ; 2e8a7a70fd 2014-12-09 kinaba: int M = ; 2e8a7a70fd 2014-12-09 kinaba: string compressedPaper_[] = ; 2e8a7a70fd 2014-12-09 kinaba: vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 2e8a7a70fd 2014-12-09 kinaba: int _ = ; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(6) 2e8a7a70fd 2014-12-09 kinaba: int N = ; 2e8a7a70fd 2014-12-09 kinaba: int M = ; 2e8a7a70fd 2014-12-09 kinaba: string compressedPaper_[] = ; 2e8a7a70fd 2014-12-09 kinaba: vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 2e8a7a70fd 2014-12-09 kinaba: int _ = ; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: */ 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: // END CUT HERE