#include #include #include using namespace std; // indexではなく実値を返す template struct SparseTable{ vector> table; vector log_table; F f; SparseTable(const vector &v, F ff):f(ff){ int n = v.size(); int bit = 0; while((1<>1]+1; } // 0-indexed, [a, b] T query(int a, int b){ int d = b-a+1; int k = log_table[d]; return f(table[k][a], table[k][b-(1<> n; vector> vp; for(int i = 0; i < n; i++){ string s; cin >> s; vp.push_back({s, i}); } sort(vp.begin(), vp.end()); vector rev(n); for(int i = 0; i < n; i++){ rev[vp[i].second] = i; } vector v; for(int i = 1; i < n; i++){ int cnt = 0, len = min(vp[i-1].first.length(), vp[i].first.length()); while(cnt < len && vp[i-1].first[cnt]==vp[i].first[cnt]) cnt++; v.push_back(cnt); } auto f = [](int i, int j){return min(i, j);}; SparseTable table(v, f); int m; ll x, d; cin >> m >> x >> d; ll ans = 0; for(int loop = 0; loop < m; loop++){ ll i = x/(n-1); ll j = x%(n-1); if(i > j) swap(i, j); else j++; if(rev[i] > rev[j]) swap(i, j); ans += table.query(rev[i], rev[j]-1); x = (x+d)%(n*(n-1)); } cout << ans << endl; return 0; }