#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class RollingHash { private: static const int X, M; vector xp; vector hash; public: RollingHash(const string& s){ int n = s.size(); xp.assign(n+1, 1); hash.assign(n+1, 0); for(int i=0; i> n; vector s(n); for(int i=0; i> s[i]; vector rh; for(int i=0; i> m >> x >> d; vector a(m), b(m); for(int i=0; i b[i]) swap(a[i], b[i]); else ++ b[i]; x = (x + d) % (n * (n - 1LL)); } long long ans = 0; map, int> memo; for(int i=0; i key(a[i], b[i]); if(memo.find(key) != memo.end()){ ans += memo[key]; } else{ int len = min(s[a[i]].size(), s[b[i]].size()); int tmp = solve(len, rh[a[i]], rh[b[i]]); ans += tmp; memo[key] = tmp; } } cout << ans << endl; return 0; }