188 lines
3.7 KiB
C++

/* Problem URL: https://codeforces.com/gym/106159/problem/D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 0
struct hashing {
static vl p;
static ll m;
static ll b;
vl hash;
hashing(vi &now) {
while (p.size() <= now.size()) {
p.push_back(((__int128)p.back() * b % m));
}
hash.push_back(0);
rep(i, now.size()) {
hash.push_back(((__int128)hash.back() * b % m + now[i]) % m);
}
}
ll gethash(int l, int r) {
return (((__int128)hash[r + 1] - (__int128)hash[l] * p[r - l + 1]) % m + m) % m;
}
};
vl hashing::p = {1};
ll hashing::m = (1LL << 61) - 1;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashing::b = uniform_int_distribution<ll>(1000, m - 1)(rng);
void solve()
{
int n, m;
cin >> n >> m;
vi a(n);
vi b(m);
cin >> a >> b;
vi diffa(n - 1);
vi diffb(m - 1);
rep(i, n - 1) {
if (a[i] <= a[i + 1]) {
diffa[i] = a[i + 1] - a[i];
} else {
diffa[i] = a[i + 1] - a[i] + 10000;
}
}
rep(i, m - 1) {
if (b[i] <= b[i + 1]) {
diffb[i] = b[i + 1] - b[i];
} else {
diffb[i] = b[i + 1] - b[i] + 10000;
}
}
vi all;
hashing ah(diffa);
hashing bh(diffb);
rep(i, n - m + 1) {
if (bh.gethash(0, m - 2) == ah.gethash(i, i + m - 2)) {
all.push_back(i);
}
}
vi ans(10000);
repv(i, all) {
if (a[i] <= b[0]) {
ans[b[0] - a[i]]++;
continue;
}
ans[b[0] - a[i] + 10000]++;
}
int maximal = 0;
int choice = 0;
rep(i, 10000) {
if (ans[i] > maximal) {
maximal = ans[i];
choice = i;
}
}
cout << choice << ' ' << maximal << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}