text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; long long a[4001], b[2001], n, m, now, ans = 0x3fffffffffffffff; bool flag; signed main() { std::ios::sync_with_stdio(false); cin >> n >> m; for (long long i = 1; i <= n; ++i) { cin >> a[i]; } for (long long i = 1; i <= n; ++i) { cin >> b[i]; } sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n); for (long long i = 1; i <= n; ++i) { a[i + n] = a[i]; now = (b[1] - a[i] + m) % m; flag = true; for (long long j = 2; j <= n; ++j) { if ((b[j] - a[j + i - 1] + m) % m != now) { flag = false; break; } } if (flag) { ans = min(now, ans); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long ab(long long n) { return n < 0 ? -n : n; } int main() { long long n, m, in; vector<long long> A, B; scanf("%lld %lld", &n, &m); for (int i = 0; i < n; i++) { scanf("%lld", &in); A.push_back(in); } for (int i = 0; i < n; i++) { scanf("%lld", &in); B.push_back(in); } long long ans = INT_MAX; set<long long> S; set<long long>::iterator it; vector<long long> d; for (int i = 0; i < n; i++) { int x = B[0] - A[i]; if (x < 0) x += m; S.insert(x); } for (it = S.begin(); it != S.end(); it++) { d.push_back(*it); } int len = d.size(); sort(B.begin(), B.end()); for (int i = 0; i < len; i++) { vector<long long> tmp; for (int j = 0; j < n; j++) { tmp.push_back((A[j] + d[i]) % m); } sort(tmp.begin(), tmp.end()); if (tmp == B) { ans = d[i]; break; } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int m, x, ans = -1; int n; cin >> n >> m; long long int arr[n], brr[n], copy_arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; copy_arr[i] = arr[i]; } for (int i = 0; i < n; i++) { cin >> brr[i]; } sort(brr, brr + n); int flag = 0; for (int i = 0; i < n; i++) { x = (brr[0] - arr[i] + m) % m; for (int j = 0; j < n; j++) { copy_arr[j] = (copy_arr[j] + x) % m; } sort(copy_arr, copy_arr + n); for (int j = 0; j < n; j++) { if (copy_arr[j] != brr[j]) { flag = 1; break; } } if (flag == 0) { if (ans == -1) ans = x; else ans = min(ans, x); } for (int j = 0; j < n; j++) copy_arr[j] = arr[j]; flag = 0; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int i, n, x, m, a[3000], c[3000], b[3000]; int main() { cin >> n >> m; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { cin >> b[i]; } sort(a, a + n); sort(b, b + n); int f, ans = 1e9 + 1, tempx; for (int i = 0; i < n; i++) { if (a[0] == b[i]) continue; if (a[0] < b[i]) x = b[i] - a[0]; else x = b[i] - a[0] + m; for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m; sort(c, c + n); f = 0; for (int j = 0; j < n; j++) if (c[j] != b[j]) { f = 1; break; } if (f == 0) ans = min(ans, x); } if (ans == 1e9 + 1) cout << 0; else cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long sum_mod(long long a, long long b, long long m) { return (a + b) % m; } bool check(int n, vector<long long>& a, vector<long long>& b) { for (int i = 0; i < n; i++) { if (a[i] != b[i]) return false; } return true; } void solve() { long long n, m; cin >> n >> m; vector<long long> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long ans = 1e18; long long curx = 0; for (int i = 0; i < n + 2; i++) { long long x; if ((*b.rbegin()) >= (*a.rbegin())) { x = (*b.rbegin()) - (*a.rbegin()); for (int j = 0; j < n; j++) { a[j] += x; } if (check(n, a, b)) { if (curx + x < ans) ans = curx + x; } } else { x = 0; } long long y = m - (*a.rbegin()); for (int j = 0; j < n; j++) { a[j] = sum_mod(a[j], y, m); } curx += x + y; sort(a.begin(), a.end()); } cout << ans << '\n'; } int main() { string file("product"); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n = 1; for (int t = 0; t < n; t++) { solve(); } }
#include <bits/stdc++.h> using namespace std; long long a[2005], b[2005]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; long long n, m, i, j, k; cin >> n >> m; map<long long, long long> mb; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { cin >> b[i]; mb[b[i]]++; } sort(a, a + n); sort(b, b + n); map<long long, long long> ck; long long ans = 1e10; for (i = 0; i < n; i++) { k = 0; if (a[i] != b[0]) { if (b[0] > a[i]) k = b[0] - a[i]; else k = m - a[i] + b[0]; } for (j = 0; j < n; j++) { ck[(a[j] + k) % m]++; } long long c = 1; for (j = 0; j < n; j++) { if (ck[b[j]] != mb[b[j]]) { c = 0; break; } } if (c) { ans = min(ans, k); } ck.clear(); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool check(vector<int> &a, vector<int> &b) { for (int i = 0; i < a.size(); i++) if (a[i] != b[i]) return false; return true; } int main() { ios ::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> a(n); vector<int> b(n); vector<int> t(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b.begin(), b.end()); int res = INT_MAX; for (int i = 0; i < n; i++) { t = a; int acc = b[0] - a[0]; if (acc < 0) acc += m; for (int i = 0; i < n; i++) t[i] = (t[i] + acc) % m; sort(t.begin(), t.end()); if (check(b, t)) res = min(res, acc); int temp = a.back(); a.pop_back(); a.insert(a.begin(), temp); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void Solve() { int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b.begin(), b.end()); int ans = m; for (int i = 0; i < n; i++) { int x = (b[i] - a[0] + m) % m; vector<int> c(n); for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m; sort(c.begin(), c.end()); if (c == b) ans = min(ans, x); } cout << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { Solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int NMAX = 2e3 + 5; int N, M, A[NMAX], B[NMAX], C[NMAX]; int Min = 0; static inline void Read() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cin >> N >> M; for (int i = 1; i <= N; ++i) cin >> A[i]; for (int i = 1; i <= N; ++i) cin >> B[i]; return; } static inline void Sort() { sort(A + 1, A + N + 1); sort(B + 1, B + N + 1); return; } static inline void Easy_Case() { for (int i = 1; i <= N; ++i) if (A[i] != B[i]) return; cout << 0 << '\n'; exit(0); return; } static inline void Check(int D) { for (int i = 1; i <= N; ++i) C[i] = (A[i] + D) % M; int pos = N; for (int i = 1; i < N; ++i) if (C[i] > C[i + 1]) { pos = i; break; } int j = 0; for (int i = pos + 1; i <= N; ++i) if (C[i] == B[++j]) continue; else return; for (int i = 1; i <= pos; ++i) if (C[i] == B[++j]) continue; else return; Min = min(Min, D); return; } static inline void Solve() { Min = 1e9; for (int i = 1; i <= N; ++i) { int D = B[1] - A[i]; if (D < 0) D += M; Check(D); } cout << Min << '\n'; return; } int main() { Read(); Sort(); Easy_Case(); Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } const long long mod = 1e9 + 7; bool is(long long n) { return (ceil(log2(n)) == floor(log2(n))); } bool q(const pair<long long, long long> &a, const pair<long long, long long> &b) { if (a.second == b.second) return a.first < b.first; return a.second < b.second; } bool q2(const pair<long long, long long> &a, const pair<long long, long long> &b) { if (a.first == b.first) return a.second > b.second; return a.first > b.first; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t = 1, n, m; while (t--) { cin >> n >> m; long long a[n], b[n]; map<long long, long long> m1, m2; for (long long i = 0; i < n; i++) { cin >> a[i]; m1[a[i]]++; } for (long long i = 0; i < n; i++) { cin >> b[i]; m2[b[i]]++; } sort(a, a + n); sort(b, b + n); long long x1 = INT_MAX, x2 = INT_MAX; vector<long long> ans; for (long long j = 0; j < n; j++) { if (m1[a[0]] == m2[b[j]]) { if (a[0] <= b[j]) x1 = b[j] - a[0] % m; else x1 = m + b[j] - a[0]; ans.push_back(x1); } } sort(ans.begin(), ans.end()); long long a2[n]; for (auto x : ans) { bool first = true; for (int i = 0; i < n; i++) { a2[i] = (a[i] % m + x % m) % m; } sort(a2, a2 + n); for (int i = 0; i < n; i++) { if (a2[i] != b[i]) { first = false; break; } } if (first) { cout << x << endl; return 0; } } x1 = min(x1, x2); cout << x1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } mt19937_64 rang( chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); } const long long int mod = 1e9 + 7; const long long int infinity = 1e18; long long int fastpow(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1; } return res % mod; } vector<long long int> factt(200005, 0); vector<long long int> inv(200005, 0); void fact() { factt[0] = 1; factt[1] = 1; inv[1] = 1; inv[0] = 1; long long int i; for (i = 2; 2 < 200005 ? i < 200005 : i > 200005; 2 < 200005 ? i += 1 : i -= 1) { factt[i] = (factt[i - 1] * i); inv[i] = fastpow(i, mod - 2); } } long long int C(long long int n, long long int k) { if (n < k) return 0; else return factt[n] * 1ll * inv[factt[k]] * 1ll * inv[factt[n - k]]; } vector<vector<long long int> > graph; long long int toceil(long long int x, long long int y) { return (x + y - 1) / y; } long long int in() { long long int data; cin >> data; return data; } void solve() { long long int n, m; cin >> n >> m; long long int i; vector<long long int> a, b; for (i = 0; i < n; i++) a.push_back(in()); for (i = 0; i < n; i++) b.push_back(in()); sort(b.begin(), b.end()); ; sort(a.begin(), a.end()); ; long long int j; long long int ans = infinity; for (i = 0; i < n; i++) { vector<long long int> c = a; long long int x = (b[0] - a[i] + m) % m; for (j = 0; j < n; j++) { c[j] = (c[j] + x) % m; } sort(c.begin(), c.end()); ; if (c == b) { ans = min(ans, x); } } cout << ans << "\n"; ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(12); int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; long long int pow_mod(long long int a, long long int b) { long long int res = 1; while (b != 0) { if (b & 1) { res = (res * a) % 1000000007; } a = (a * a) % 1000000007; b /= 2; } return res; } void solve() { long long int n, m; cin >> n >> m; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; long long int b[n]; for (long long int i = 0; i < n; i++) cin >> b[i]; long long int x = a[0]; long long int mn = (long long int)1e18; for (long long int i = 0; i < n; i++) { long long int y = b[i]; long long int add; if (y >= x) add = y - x; else add = m - x + y; vector<long long int> demo(n); for (long long int j = 0; j < n; j++) demo[j] = (a[j] + add) % m; vector<long long int> temo(n); for (long long int j = 0; j < n; j++) temo[j] = b[j]; sort(demo.begin(), demo.end()); sort(temo.begin(), temo.end()); bool flag = true; for (long long int j = 0; j < n; j++) { if (demo[j] != temo[j]) { flag = false; break; } } if (flag) { if (add < mn) mn = add; } } cout << mn << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, i, j, d, mi = -1; bool is; cin >> n >> m; vector<long long> as(n), bs(n); for (i = 0; i < n; i++) cin >> as[i]; for (i = 0; i < n; i++) cin >> bs[i]; sort(as.begin(), as.end()); sort(bs.begin(), bs.end()); for (i = 0; i < n; i++) bs[i] += m; for (i = 0; i < n; i++) { is = true; d = (bs[i] - as[0]) % m; for (j = 0; j < n; j++) { if (d != (bs[(i + j) % n] - as[j]) % m) { is = false; break; } } if (is) { if (mi == -1 or mi > d) mi = d; } } cout << mi << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; bool equals(vector<int> &s1, vector<int> &s2) { sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); for (int i = 0; i < s1.size(); i++) if (s1[i] != s2[i]) return false; return true; } int main() { int n, m, a[2003], b[2003]; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); vector<int> s1, s2; for (int i = 0; i < n; i++) { s1.push_back((m + a[i] - a[0]) % m); } int MIN = 1e9 + 1; for (int i = 0; i < n; i++) { int p = b[i]; s2.clear(); for (int j = 0; j < n; j++) { s2.push_back((m + b[j] - b[i]) % m); } if (equals(s1, s2)) { MIN = min(MIN, (m + b[i] - a[0]) % m); } } printf("%d", MIN); return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int main() { long long n, m, x; cin >> n >> m; std::vector<long long> a(n), b(n), temp(n); for (long long i = 0; i < n; i++) { cin >> x; a[i] = x; } for (long long i = 0; i < n; i++) { cin >> x; b[i] = x; } sort(b.begin(), b.end()); set<long long> s; for (long long i = 0; i < n; i++) s.insert((b[0] - a[i] + m) % m); for (auto it : s) { int flag = 0; for (long long i = 0; i < n; i++) temp[i] = (a[i] + it) % m; sort(temp.begin(), temp.end()); for (long long i = 0; i < n; i++) { if (temp[i] != b[i]) { flag = 1; break; } } if (flag == 0) { cout << it << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a[2000], b[2000]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } for (int i = 0; i < n; ++i) { scanf("%d", &b[i]); } sort(a, a + n); sort(b, b + n); for (int i = 0; i < n; ++i) { int diff = (b[0] - a[i] + m) % m; bool flag = 1; for (int j = 1; j < n; ++j) { if ((b[j] - a[(i + j) % n] + m) % m != diff) { flag = 0; break; } } if (flag) { printf("%d\n", diff); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const long double PI = acos(-1.0); long long int power(long long int x, long long int y) { long long int res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } void solve() { long long int n, k; cin >> n >> k; vector<long long int> arr(n), brr(n); map<long long int, long long int> mm1, mm2; for (int i = 0; i < n; i++) { cin >> arr[i]; mm1[arr[i]]++; } for (int i = 0; i < n; i++) { cin >> brr[i]; mm2[brr[i]]++; } sort(brr.begin(), brr.end()); vector<long long int> key; for (int i = 0; i < n; i++) key.push_back((brr[0] - arr[i] + k) % k); ; sort(key.begin(), key.end()); for (int i = 0; i < key.size(); i++) { vector<long long int> v = arr; for (int j = 0; j < v.size(); j++) { v[j] = (v[j] + key[i]) % k; } sort(v.begin(), v.end()); if (v == brr) { cout << key[i] << endl; return; } } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> const double PI = 3.1415926; const int KL = 1e6; using namespace std; long long q, x, y, z, n, m, a[KL], b[KL], mn = 1e9, mx = -1e9, ans; vector<long long> vec; set<long long> s, t, e; map<long long, bool> mp; map<long long, long long> cnt, cn; int main() { scanf("%lld", &n); scanf("%lld", &m); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); for (int i = 0; i < n; i++) scanf("%lld", &b[i]); for (int i = 0; i < n; i++) s.insert(a[i]); for (int i = 0; i < n; i++) { t.insert(b[i]); cnt[b[i]]++; } auto it = s.begin(); while (it != s.end()) { x = *(it); auto ot = t.begin(); while (ot != t.end()) { y = *(ot); if (x <= y) { z = y - x; } else { z = y - x + m; } if (!mp[z]) { vec.push_back(z); mp[z] = true; } ot++; } break; } sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { x = vec[i]; for (int j = 0; j < n; j++) { q = (a[j] + x) % m; e.insert(q); cn[q]++; } if (e.size() == t.size()) { auto ie = e.begin(); auto ot = t.begin(); bool ok = true; int k = 0; while (ie != e.end()) { y = *(ie); z = *(ot); if (y == z && cnt[z] == cn[y]) k++; else { ok = false; break; } ie++; ot++; } if (ok) { cout << x; break; } } e.clear(); cn.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 2002 + 20; int a[N], b[N], n, m, c[N]; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = (1); i <= (n); i++) { cin >> a[i]; } for (int i = (1); i <= (n); i++) cin >> b[i]; sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n); int ans = -1; for (int i = (1); i <= (n); i++) { int x = (b[i] - a[1] + m) % m; for (int j = (1); j <= (n); j++) c[j] = (a[j] + x) % m; sort(c + 1, c + 1 + n); for (int j = (1); j <= (n); j++) { if (c[j] != b[j]) break; if (j < n) continue; if (ans == -1) ans = x; else ans = min(ans, x); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << ' ' << *it << " = " << a; err(++it, args...); } const long long OOL = LLONG_MAX; const int N = 2e3 + 1, M = 3e5 + 1, OO = INT_MAX, MOD = 1e9 + 7; set<int> x; int a[N], b[N], temp[N]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d", a + i); for (int i = 0; i < n; ++i) scanf("%d", b + i); for (int i = 0; i < n; ++i) x.insert((b[0] - a[i] + m) % m); sort(b, b + n); bool f = 1; for (auto it = x.begin(); it != x.end(); ++it) { f = 1; int k = 0; for (int i = 0; i < n; ++i) temp[k++] = (a[i] + *it) % m; sort(temp, temp + k); for (int i = 0; i < n; ++i) if (temp[i] != b[i]) { f = 0; break; } if (f) { printf("%d\n", *it); break; } } assert(f); return 0; }
#include <bits/stdc++.h> const int maxn = 2008; int n, k, a[maxn], b[maxn]; int main(int argc, char *argv[]) { std::cin >> n >> k; for (int i = 0; i < n; i++) { std::cin >> a[i]; } for (int i = 0; i < n; i++) { std::cin >> b[i]; } std::sort(a, a + n); std::sort(b, b + n); for (int i = 0; i < n; i++) { int x = (b[i] + k - a[0]) % k; bool yes = 1; for (int j = 0; j < n; j++) { if (b[(j + i) % n] != (a[j] + x) % k) { yes = 0; break; } } if (yes == 1) { std::cout << x << '\n'; exit(0); } } }
#include <bits/stdc++.h> using namespace std; int n, m; int a[2005], b[2005], c[2005]; bool ok() { for (int i = 0; i < n; i++) { if (c[i] != b[i]) return 0; } return 1; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(a, a + n); sort(b, b + n); int mn = 1e9 + 100; for (int i = 0; i < n; i++) { int add = (b[i] - a[0] + m) % m; for (int i = 0; i < n; i++) { c[i] = (a[i] + add) % m; } sort(c, c + n); if (ok()) { mn = min(mn, add); } } cout << mn << endl; }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long long inf = 1e12; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long flag = 0, flag2 = 0; map<long long, long long> mm; set<long long> s; vector<long long> v; set<long long>::iterator itr; long long n, m; cin >> n >> m; long long a[n], b[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 0; i < n; i++) { cin >> b[i]; } long long j = 0; sort(a, a + n); sort(b, b + n); long long x = 0; long long ans = inf; for (long long j = 0; j < n; j++) { flag2 = 0; map<long long, long long> xx; for (long long i = 0; i < n; i++) { x = (b[(i + j) % n] - a[i] + m) % m; xx[x]++; } if (xx.size() == 1) { auto itr = xx.begin(); ans = min(ans, itr->first); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n, m; unordered_map<long long int, long long int> mp; cin >> n >> m; long long int a[n], b[n]; unordered_set<long long int> s; long long int ans; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = 0; i < n; i++) cin >> b[i], mp[b[i]]++; sort(a, a + n); sort(b, b + n); for (long long int i = 0; i < n; i++) s.insert((b[i] - a[0] + m) % m); for (auto it : s) { long long int cnt = 0; for (long long int i = 0; i < n; ++i) { if (mp[(a[i] + (long long int)it) % m] > 0) mp[(a[i] + (long long int)it) % m]--, cnt++; else break; } if (cnt == n) { ans = it; break; } mp.clear(); for (long long int i = 0; i < n; i++) mp[b[i]]++; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4; long long a[maxn]; long long b[maxn]; long long M; int check(int L, int R) { int j = 1; long long ans2; if (a[L] > b[1]) ans2 = M - a[L] + b[1]; else if (a[L] <= b[1]) ans2 = b[1] - a[L]; for (int i = L; i <= R; i++, j++) { if ((a[i] + ans2) % M != b[j]) return -1; } return ans2; } int main() { ios::sync_with_stdio(false); int N; cin >> N; cin >> M; for (int i = 1; i <= N; i++) cin >> a[i]; for (int i = 1; i <= N; i++) cin >> b[i]; sort(a + 1, a + 1 + N); sort(b + 1, b + 1 + N); for (int i = N + 1; i <= 2 * N; i++) a[i] = a[i - N]; long long ans = 1e9 + 100000; for (int i = 1; i <= N; i++) { long long ans2 = check(i, i + N - 1); if (ans2 == -1) continue; ans = min(ans, ans2); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void PUNKS_NOT_DEAD() { int n, m; cin >> n >> m; vector<int> a(n), b(n); for (auto &x : a) cin >> x; int best = 2e9 + 47; for (auto &x : b) { cin >> x; } vector<int> tr; for (auto x : b) { if (x > a[0]) { tr.push_back(x - a[0]); } else { tr.push_back(((m - a[0]) % m + x) % m); } } sort(b.begin(), b.end()); for (auto x : tr) { vector<int> lol; for (auto y : a) lol.push_back((y + x) % m); sort(lol.begin(), lol.end()); if (lol == b) best = min(best, x); } cout << best; } signed main() { ios_base::sync_with_stdio(false), cin.tie(0), PUNKS_NOT_DEAD(); }
#include <bits/stdc++.h> int main(void) { long long n, m; scanf("%lld %lld", &n, &m); std::vector<long long> a(n); std::vector<long long> b(n); std::map<long long, long long> am; std::map<long long, long long> bm; for (long long i = 0; i < n; ++i) { scanf("%lld", &a[i]); if (am.find(a[i]) == am.end()) am[a[i]] = 0; ++am[a[i]]; } for (long long i = 0; i < n; ++i) { scanf("%lld", &b[i]); if (bm.find(b[i]) == bm.end()) bm[b[i]] = 0; ++bm[b[i]]; } for (long long i = 0; i < n; ++i) { long long ta = m + b[i] - a[0]; if (a[0] == b[i]) ta = 0; if (a[0] < b[i]) ta = b[i] - a[0]; std::map<long long, long long> nw; for (auto &each : am) { long long conv = (each.first + ta) % m; if (nw.find(conv) == nw.end()) nw[conv] = 0; nw[conv] += each.second; } bool pos = true; for (auto &each : nw) { if (bm.find(each.first) == bm.end() || bm[each.first] != each.second) { pos = false; break; } } if (pos) { printf("%lld\n", ta); return 0; } } puts("ahah"); int *p; p = 0; *p = 555; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; long long a[n], b[n]; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) cin >> b[i]; sort(a, a + n); sort(b, b + n); long long ans = m; map<long long, long long> mp; for (long long i = 0; i < n; i++) { mp.clear(); for (long long j = 0; j < n; j++) { mp[(b[j] - a[(i + j + n) % n] + m) % m]++; } for (auto x : mp) { if (x.second == n) ans = min(ans, x.first); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long arr[2005], arr2[2005], n, m, tmp; bool solve(long long md) { long long a[n]; for (int i = 0; i < n; i++) { a[i] = (arr[i] + md) % m; } sort(a, a + n); for (int i = 0; i < n; i++) if (a[i] != arr2[i]) return 0; return 1; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n; i++) { cin >> arr2[i]; } sort(arr, arr + n); sort(arr2, arr2 + n); long long ans = 2e9; for (int i = 0; i < n; i++) { if (arr[i] > arr2[0]) tmp = m - arr[i] + arr2[0]; else tmp = arr2[0] - arr[i]; if (solve(tmp) == 1) ans = min(ans, tmp); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, m, x; cin >> n >> m; vector<long long int> a(n), b(n), v1, v2; for (long long int i = 0; i < n; i++) { cin >> a[i]; } for (long long int i = 0; i < n; i++) { cin >> b[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long int ans = INT_MAX; for (long long int i = 0; i < n; i++) { long long int f = 0; long long int z = b[i] - a[0]; z = (z + m) % m; for (long long int j = 0; j < n; j++) { if ((a[j] + z) % m != b[(j + i) % n]) { f = 1; break; } } if (f == 0) { ans = min(ans, z); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int maxn = 1e9 + 10; template <typename T> T mod(T n, T p, T m) { n %= m; T res = 1; while (p > 0) { if (p & 1) res = ((res % m) * (n % m)) % m; n = ((n % m) * (n % m)) % m; p >>= 1; } return res; } template <typename T> T phi(T n) { long long int result = n; for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, m; cin >> n >> m; long long int a[n], b[n], c[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b, b + n); long long int ans = maxn; for (int i = 0; i < n; i++) { long long int x = (b[i] - a[0] + m) % m; for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m; sort(c, c + n); bool ok = 1; for (int j = 0; j < n; j++) { if (c[j] != b[j]) { ok = 0; break; } } if (ok) { ans = min(x, ans); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m; int a[N]; int b[N]; int count(int x, int arr[]) { int cnt = upper_bound(arr + 1, arr + n + 1, x) - lower_bound(arr + 1, arr + n + 1, x); return cnt; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n); int ans = 1e9; for (int i = 1; i <= n; i++) { int cnta = count(a[1], a); int cntb = count(b[i], b); if (cnta == cntb) { bool ok = 1; int x = (b[i] - a[1] + m) % m; for (int j = 2; j <= n; j++) { if (count(a[j], a) != count((a[j] + x) % m, b)) { ok = 0; break; } } if (ok) { ans = min(ans, x); } } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, k, l, x; cin >> n >> m; vector<int> a(n); vector<int> b(n); for (i = 0; i < n; i++) { cin >> a[i]; } for (j = 0; j < n; j++) { cin >> b[j]; } sort(b.begin(), b.end()); sort(a.begin(), a.end()); for (j = 0; j < n; j++) { if (b[0] >= a[j]) { x = b[0] - a[j]; } else { x = m - a[j] + b[0]; } vector<int> temp(a.begin(), a.end()); for (i = 0; i < n; i++) { temp[i] = (temp[i] + x) % m; } sort(temp.begin(), temp.end()); for (i = 0; i < n; i++) { if (temp[i] != b[i]) { break; } } if (i == n) { cout << x << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; bool verify(long long difference, long long n, long long m, long long a[], map<long long, long long> d) { bool succeed = true; map<long long, long long> c(d); long long a2[2002]; copy(a, a + n, a2); for (long long i = 0; i < n; i++) { a2[i] = (a2[i] + difference) % m; c[a2[i]]--; } map<long long, long long>::iterator it; for (it = c.begin(); it != c.end(); it++) { pair<long long, long long> p = *it; if (p.second != 0) { succeed = false; break; } } return succeed; } int main() { long long n, m, suma = 0, sumb = 0; long long a[2002], b[2002]; map<long long, long long> c; cin >> n >> m; for (long long i = 0; i < n; i++) { cin >> a[i]; suma += a[i]; suma = suma % m; } for (long long i = 0; i < n; i++) { cin >> b[i]; sumb += b[i]; sumb = sumb % m; c[b[i]]++; } sort(a, a + n); sort(b, b + n); long long minimum = m; for (long long i = 0; i < n; i++) { long long difference; if (b[i] >= a[0]) difference = b[i] - a[0]; else difference = b[i] + m - a[0]; bool succeed = false; if ((suma + (difference * n)) % m == sumb % m) { succeed = verify(difference, n, m, a, c); } if (succeed) { if (difference < minimum) minimum = difference; } } cout << minimum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long d, x, y; void extendedEuclid(long long A, long long B) { if (B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A % B); long long temp = x; x = y; y = temp - (A / B) * y; } } long long modInverse(long long A, long long M) { extendedEuclid(A, M); return (x % M + M) % M; } long long expo(long long a, long long b, long long m) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) return expo((a * a) % m, b / 2, m); return (a * expo((a * a) % m, (b - 1) / 2, m)) % m; } int32_t main() { long long tt = 1; while (tt--) { long long n, m; cin >> n >> m; vector<long long> a(n), b(n); long long s1 = 0, s2 = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; s1 += a[i]; } for (long long i = 0; i < n; i++) { cin >> b[i]; } sort(b.begin(), b.end()); long long mi = INT_MAX; for (long long i = 0; i < n; i++) { long long x = (b[i] - a[0] + m) % m; vector<long long> t; for (long long i = 0; i < n; i++) t.push_back((a[i] + x) % m); sort(t.begin(), t.end()); if (t == b) mi = min(mi, x); } cout << mi << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n = 0, m = 0; cin >> n >> m; vector<int> A(n); vector<int> B(n); for (int i = 0; i < n; i++) { cin >> A[i]; } for (int i = 0; i < n; i++) { cin >> B[i]; } vector<int> AA = A; vector<int> BB = B; sort(BB.begin(), BB.end()); if (AA == BB) { cout << 0; return 0; } vector<int> ans; vector<int> temp(n); int x; for (int i = 0; i < n; i++) { x = (BB[0] - A[i] + m) % m; for (int j = 0; j < n; j++) { temp[j] = (A[j] + x) % m; } sort(temp.begin(), temp.end()); if (temp == BB) { ans.push_back(x); } } sort(ans.begin(), ans.end()); cout << ans[0] << endl; return 0; }
#include <bits/stdc++.h> long long int n, m; void merging(long long arr[], long long start, long long mid, long long end); void sort(long long arr[], long long start, long long end) { long long mid; if (end - start + 1 > 1) { mid = (start + end) / 2; sort(arr, start, mid); sort(arr, mid + 1, end); merging(arr, start, mid, end); } } void merging(long long arr[], long long start, long long mid, long long end) { long long len1, len2, i, j, k; len1 = mid - start + 1; len2 = end - mid - 1 + 1; long long right[len2], left[len1]; for (i = 0; i < len1; i++) { left[i] = arr[i + start]; } for (i = 0; i < len2; i++) { right[i] = arr[i + mid + 1]; } i = 0; j = 0; k = start; while ((i < len1) && (j < len2)) { if (left[i] > right[j]) { arr[k] = right[j]; j++; k++; } else { arr[k] = left[i]; i++; k++; } } if (i == len1) { while (j < len2) { arr[k] = right[j]; j++; k++; } } else { while (i < len1) { arr[k] = left[i]; i++; k++; } } } int main() { scanf("%I64lld %I64lld", &n, &m); long long a[2005], b[2005], dup[2005]; for (int i = 0; i < n; i++) { scanf("%I64lld", &b[i]); } for (int i = 0; i < n; i++) { scanf("%I64lld", &a[i]); dup[i] = b[i]; } sort(a, 0, n - 1); long long int d, l = 0, fin[2005]; for (int i = 0; i < n; i++) { d = a[0] - b[i]; if (d < 0) d = d + m; for (int j = 0; j < n; j++) { dup[j] = (dup[j] + d) % m; } sort(dup, 0, n - 1); int flag = 1; for (int j = 0; j < n; j++) { if (dup[j] != a[j]) { flag = -1; break; } } if (flag == 1) { fin[l] = d; l++; } for (int j = 0; j < n; j++) { dup[j] = b[j]; } } long long int min = fin[0]; for (int i = 0; i < l; i++) { if (fin[i] < min) min = fin[i]; } printf("%I64lld\n", min); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; int main() { int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = (0); i < (n); ++i) cin >> a[i]; sort((a).begin(), (a).end()); for (int i = (0); i < (n); ++i) cin >> b[i]; sort((b).begin(), (b).end()); int ans = INF; for (int i = (0); i < (n); ++i) { int x = (b[0] - a[i] + m) % m; bool ok = true; for (int j = (0); j < (n); ++j) ok &= (a[(i + j) % n] + x) % m == b[j]; if (ok) ans = min(ans, x); } assert(ans != INF); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> ar, br; for (int i = 0; i < n; i++) { int y; cin >> y; ar.push_back(y); } for (int i = 0; i < n; i++) { int y; cin >> y; br.push_back(y); } sort(br.begin(), br.end()); int ans = m; for (int i = 0; i < n; i++) { int x = (br[i] - ar[0] + m) % m; vector<int> v; for (int j = 0; j < n; j++) { int p = (ar[j] + x) % m; v.push_back(p); } sort(v.begin(), v.end()); if (v == br) ans = min(ans, x); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; bool check(long long int a[], long long int b[], long long int n, long long int m, long long int x) { long long int A[n], B[n]; for (long long int i = 0; i < n; i++) { A[i] = a[i]; B[i] = b[i]; } for (long long int i = 0; i < n; i++) { A[i] = (A[i] + x) % m; } sort(A, A + n); sort(B, B + n); for (long long int i = 0; i < n; i++) { if (A[i] != B[i]) { return false; } } return true; } void solve() { long long int n, m; cin >> n >> m; long long int a[n], b[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } for (long long int i = 0; i < n; i++) { cin >> b[i]; } long long int minm = 1000000000000000000; for (long long int i = 0; i < n; i++) { long long int x = (b[i] - a[0] + m) % m; if (check(a, b, n, m, x)) { minm = min(minm, x); } } cout << minm << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; bool check(vector<pair<long long, long long> > A, vector<pair<long long, long long> > B, long long val, long long m) { long long i, size = A.size(); vector<pair<long long, long long> > v3(size); for (i = 0; i < size; i++) { v3[i].second = (A[i].second + val) % m; v3[i].first = A[i].first; } sort(v3.begin(), v3.end()); if (v3.size() != B.size()) return 0; for (i = 0; i < size; i++) { if (v3[i].first != B[i].first || v3[i].second != B[i].second) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; long long m; cin >> m; map<long long, long long> M; vector<pair<long long, long long> > A, B; for (int i = 0; i < n; i++) { long long x; cin >> x; M[x]++; } for (auto it : M) { A.push_back({it.second, it.first}); } M.clear(); for (int i = 0; i < n; i++) { long long x; cin >> x; M[x]++; } for (auto it : M) { B.push_back({it.second, it.first}); } sort(A.begin(), A.end()); sort(B.begin(), B.end()); long long ans = INT_MAX; for (int i = 0; i < B.size(); i++) { int f1 = A[0].first; int f2 = B[i].first; if (f1 == f2) { long long val = (B[i].second - A[0].second + m) % m; if (check(A, B, val, m)) ans = min(ans, val); } else break; } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, i, x, p = 0, dif, ans = 0, k, q; vector<long long> a, b; cin >> n >> m; for (i = 0; i < n; i++) { cin >> x; a.push_back(x); } for (i = 0; i < n; i++) { cin >> x; b.push_back(x); } sort(b.begin(), b.end()); k = m; while (k > 0) { sort(a.begin(), a.end()); p = 0; dif = b[0] - a[0]; for (i = 1; i < n; i++) { if (b[i] - a[i] != dif) { p = 1; i = n; } } if (p == 0) { ans += dif; if (ans < 0) { ans += m; } cout << ans << endl; exit(0); } else { q = m - a[n - 1]; for (i = 0; i < n; i++) { a[i] += q; a[i] = a[i] % m; } ans += q; } k--; } }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, ans, i, j; cin >> n >> m; map<long long int, long long int> mp; long long int arr1[n], arr2[n]; for (i = 0; i < n; i++) cin >> arr1[i]; for (i = 0; i < n; i++) { cin >> arr2[i]; mp[arr2[i]]++; } sort(arr1, arr1 + n); sort(arr2, arr2 + n); for (i = 0; i < n; i++) { map<long long int, long long int> temp; long long int k; if (arr2[i] >= arr1[0]) k = arr2[i] - arr1[0]; else k = arr2[i] + m - arr1[0]; for (j = 0; j < n; j++) { long long int val = (arr1[j] + k) % m; temp[val]++; } if (mp.size() != temp.size()) continue; bool flag = true; for (auto it2 = temp.begin(), it1 = mp.begin(); it1 != mp.end(), it2 != temp.end(); ++it1, ++it2) { long long int a = it1->first, b = it1->second, c = it2->first, d = it2->second; if ((a != c) || (b != d)) { flag = false; break; } } if (flag) { ans = k; break; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); int ans = m; for (int i = 0; i < n; i++) { int x = (b[i] - a[0] + m) % m; vector<int> c(n); for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m; sort(c.begin(), c.end()); if (c == b) { ans = min(ans, x); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, n, m, d, f, x, d1, ans = 1e9; cin >> n >> m; long long a[n], b[n]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) cin >> b[i]; sort(a, a + n); sort(b, b + n); for (i = 0; i < n; i++) { x = i; d = (b[0] - a[i % n] + m) % m; f = 1; for (j = 1; j < n; j++) { x++; d1 = (b[j] - a[x % n] + m) % m; if (d1 != d) { f = 0; break; } } if (f) ans = min(ans, d); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, ans = INT_MAX; ; cin >> n >> m; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i]; multiset<int> sb; for (int i = 0; i < n; i++) cin >> b[i], sb.insert(b[i]); for (int j = 0; j < n; j++) { int x = (b[0] - a[j] + m) % m; multiset<int> sa; for (int i = 0; i < n; i++) sa.insert((a[i] + x) % m); if (sa == sb) ans = min(ans, x); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } sort(begin(b), end(b)); int ans = m; for (int i = 0; i < n; ++i) { int x = (b[0] - a[i] + m) % m; vector<int> tmp(n); for (int j = 0; j < n; ++j) { tmp[j] = (a[j] + x) % m; } sort(begin(tmp), end(tmp)); if (tmp == b) { ans = min(ans, x); } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, m, i, j, k = 0, l, x, y, z, ans = INT_MAX, ans1, p = 0, q = 0, r; cin >> n >> m; long long int a[n]; vector<long long int> b(n); for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) cin >> b[i]; sort(b.begin(), b.end()); for (i = 0; i < n; i++) { if (b[0] - a[i] >= 0) x = b[0] - a[i]; else x = m + b[0] - a[i]; vector<long long int> v(n); for (j = 0; j < n; j++) { v[j] = (a[j] + x) % m; } sort(v.begin(), v.end()); if (v == b) ans = min(ans, x); } cout << ans; }
#include <bits/stdc++.h> using namespace std; long long INF = 1e9 + 7; int main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; ; cin >> n; ; long long m; ; cin >> m; ; long long arr[n]; for (long long i = 0; i < n; i++) { ; cin >> arr[i]; ; } long long arr2[n]; set<long long> s1, s2; for (long long i = 0; i < n; i++) { ; cin >> arr2[i]; ; } long long ans = INF; sort(arr, arr + n); sort(arr2, arr2 + n); for (long long i = 0; i < n; i++) { long long diff = (arr2[i] + m - arr[0]) % m; bool ok = 1; for (long long j = 0; j < n; j++) { if ((arr[j] + diff) % m != arr2[(i + j) % n]) { ok = 0; } } if (ok) { ans = min(ans, diff); } } cout << ans << endl; ; }
#include <bits/stdc++.h> using namespace std; int main() { int64_t n, m, x, t, l = INT_MAX, r, k, tc, c; cin >> n >> m; int64_t a[n + 10], b[n + 10]; map<int64_t, int64_t> p; map<int64_t, int64_t> q; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; q[b[i]]++; } sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) { k = 0; if (a[1] > b[i]) { x = (b[i] + m) - a[1]; } else { x = (b[i] - a[1]); } for (int j = 1; j <= n; j++) { t = (a[j] + x) % m; p[t]++; if (p[t] > q[t]) { k = 1; break; } } if (k == 0) { l = min(l, x); } p.clear(); } cout << l; }
#include <bits/stdc++.h> using namespace std; int mod(int a, int m) { if (a < 0) { a += m; } return a % m; } int main() { int n, m; scanf("%d %d", &n, &m); vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } for (int i = 0; i < n; ++i) { scanf("%d", &b[i]); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (a == b) { cout << 0; return 0; } int dAns = m + 1; for (int i = 0; i < n; ++i) { int distance = mod(b[0] - a[i], m); vector<int> nw(a); for (int j = 0; j < n; ++j) { nw[j] += distance; nw[j] %= m; } int ai, mn = m + 1; for (int j = 0; j < n; ++j) { if (nw[j] < mn) { mn = nw[j]; ai = j; } } bool ok = true; for (int j = 0; j < n; ++j) { ok &= nw[(ai + j) % n] == b[j]; } if (ok && distance < dAns) { dAns = distance; } } cout << dAns; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; int x[n]; for (int i = 0; i < n; i++) x[i] = (b[0] - a[i] + m) % m; sort(b.begin(), b.end()); sort(x, x + n); for (int i = 0; i < n; i++) { vector<int> tmp(n); for (int j = 0; j < n; j++) { tmp[j] = (a[j] + x[i]) % m; } sort(tmp.begin(), tmp.end()); if (tmp == b) { cout << x[i] << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; long long a[2003]; long long b[2003]; int main() { long long n, m; scanf("%lld %lld", &n, &m); for (long long i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for (long long i = 1; i <= n; i++) { scanf("%lld", &b[i]); } sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); long long ans = 0x3f3f3f3f3f3f3f3f; for (int i = 1; i <= n; i++) { long long x = (b[i] - a[1] + m) % m; int top = 2; int f = 1; for (int j = i + 1; j <= n; j++) { long long tmp = (a[top] + x) % m; if (tmp != b[j]) { f = 0; } top++; } for (int j = 1; j < i; j++) { long long tmp = (a[top] + x) % m; if (tmp != b[j]) { f = 0; } top++; } if (f == 1) ans = min(ans, x); } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int a[2123]; int b[2123]; int c[2123]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); sort(b, b + n); int ans = m; for (int i = 0; i < n; i++) { int x = b[i] + m - a[0]; x = x % m; for (int i = 0; i < n; i++) c[i] = (a[i] + x) % m; sort(c, c + n); int flag = 0; for (int i = 0; i < n; i++) if (b[i] != c[i]) flag = 1; if (flag == 0) ans = min(ans, x); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t_c = 1; for (long long t = 1; t <= t_c; t++) { long long n, m; cin >> n >> m; long long arr1[n], arr2[n]; for (long long i = 0; i < n; i++) cin >> arr1[i]; for (long long i = 0; i < n; i++) cin >> arr2[i]; long long ans = m; sort(arr1, arr1 + n); sort(arr2, arr2 + n); for (long long i = 0; i < n; i++) { long long idx = i % n; long long diff = (arr2[0] - arr1[idx] + m) % m; bool fine = true; for (long long j = 0; j < n; j++) { idx = (i + j) % n; if ((arr2[j] - arr1[idx] + m) % m != diff) { fine = false; break; } } if (fine) { ans = min(ans, diff); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long INF = 1e18; bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long nCr(long long n, long long r) { long long res = 1; if (n < r) return -1; r = min(r, n - r); for (long long i = 0; i < r; i++) { res *= (n - i); res /= (i + 1); } return res; } long long nPr(long long n, long long r) { long long res = 1; if (n < r) return -1; for (long long i = 0; i < r; i++) res *= (n - i); return res; } void solve() { long long n, m; cin >> n >> m; vector<long long> a(n), b(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 0; i < n; i++) { cin >> b[i]; } sort(b.begin(), b.end()); long long ans = INF; for (long long i = 0; i < n; i++) { long long x = (b[i] - a[0] + m) % m; vector<long long> v(n); for (long long j = 0; j < n; j++) v[j] = (a[j] + x) % m; sort(v.begin(), v.end()); if (v == b) ans = min(ans, x); } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m; cin >> n >> m; long long int a[n], b[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } for (long long int i = 0; i < n; i++) { cin >> b[i]; } sort(a, a + n); sort(b, b + n); vector<long long int> v; long long int ans = 9e18; for (long long int i = 0; i < n; i++) { long long int val = b[0] - a[i] + m; val %= m; for (long long int j = 0; j < n; j++) { v.push_back((a[j] + val) % m); } sort(v.begin(), v.end()); long long int ok = 1; for (long long int j = 0; j < n; j++) { if (v[j] != b[j]) { ok = 0; break; } } v.clear(); if (ok) { ans = min(ans, val); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2000 + 10; int a[maxn], b[maxn], n, m, ans = 1 << 30, p1[maxn], p2[maxn]; vector<int> v1, v2; int get(int a, int b) { if (a > b) return b + m - a; else return b - a; } int check(int x) { int ans = get(a[p1[x] - 1], b[0]); for (int i = 0; i < v1.size(); i++) { if (v1[(x + i) % v1.size()] != v2[i] || (a[p1[(x + i) % v1.size()] - 1] + ans) % m != b[p2[i] - 1]) return -1; } return ans; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(a, a + n); sort(b, b + n); v1.push_back(1); for (int i = 1; i < n; i++) { if (a[i] == a[i - 1]) v1.back()++; else v1.push_back(1); } v2.push_back(1); for (int i = 1; i < n; i++) { if (b[i] == b[i - 1]) v2.back()++; else v2.push_back(1); } p1[0] = v1[0]; for (int i = 1; i < v1.size(); i++) p1[i] = p1[i - 1] + v1[i]; p2[0] = v2[0]; for (int i = 1; i < v2.size(); i++) p2[i] = p2[i - 1] + v2[i]; int t; for (int i = 0; i < v1.size(); i++) { if (v1[i] == v2[0] && (t = check(i)) != -1) { ans = min(ans, t); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, ad; cin >> n >> m; int A[n], B[n], C[n], res = m + 1; for (i = 0; i < n; i++) { cin >> A[i]; } for (i = 0; i < n; i++) { cin >> B[i]; } sort(B, B + n); for (i = 0; i < n; i++) { if (B[i] >= A[0]) { ad = B[i] - A[0]; } else { ad = B[i] - A[0] + m; } for (j = 0; j < n; j++) { C[j] = (A[j] + ad) % m; } sort(C, C + n); for (j = 0; j < n; j++) { if (C[j] != B[j]) { break; } } if (j == n) { res = min(res, ad); } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long m; vector<long long> a, b; long long minn = 1000000000000000000LL; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; a.resize(n); b.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b.begin(), b.end()); for (int i = 0; i < n; i++) { vector<long long> tmp(n); long long dt = (b[i] + m - a[0]) % m; for (int j = 0; j < n; j++) tmp[j] = (a[j] + dt) % m; sort(tmp.begin(), tmp.end()); int chk = 1; for (int i = 0; i < n; i++) if (tmp[i] != b[i]) chk = 0; if (chk) minn = min(minn, dt); } cout << minn; return 0; }
#include <bits/stdc++.h> using namespace std; int a[2100], b[2100]; int n, m; int main() { int ans = 1e9; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b, b + n); for (int i = 0; i < n; i++) { int x = b[0] - a[i]; if (x < 0) x += m; int c[2100]; for (int i = 0; i < n; i++) c[i] = (a[i] + x) % m; sort(c, c + n); bool flg = 1; for (int i = 0; i < n; i++) if (b[i] != c[i]) { flg = 0; break; } if (flg) ans = min(ans, x); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct re { int x, y; }; const int inf = 1e9 + 10; const int N = 2000 + 10; int n, m, a[N], b[N], c[N], ans = inf; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; ++i) { int tg = (b[i] - a[1] + m) % m; for (int i = 1; i <= n; ++i) c[i] = (a[i] + tg) % m; sort(c + 1, c + 1 + n); bool ok = true; for (int i = 1; i <= n; ++i) if (c[i] != b[i]) { ok = false; break; } if (ok) ans = min(ans, tg); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long int n, m; cin >> n >> m; vector<long int> a(n), b(n), c(n), d(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b.begin(), b.end()); for (int i = 0; i < n; i++) c[i] = a[i]; for (int i = 0; i < n; i++) d[i] = b[i]; long int temp = a[0] % m; long int x; for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) c[i] = a[i]; d[i] %= m; if (d[i] >= temp) x = d[i] - temp; else x = (m - temp) + d[i]; for (int i = 0; i < n; i++) { c[i] = (c[i] + x) % m; } sort(c.begin(), c.end()); int flag = 1; for (int i = 0; i < n; i++) { if (c[i] != b[i]) flag = 0; } if (flag == 1) { cout << x << endl; return; } } } int main() { int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T getint() { T val = 0; char c; bool neg = false; while ((c = getchar()) && !(c >= '0' && c <= '9')) { neg |= c == '-'; } do { val = (val * 10) + c - '0'; } while ((c = getchar()) && (c >= '0' && c <= '9')); return val * (neg ? -1 : 1); } long long n, m; unordered_map<int, long long> m1, m2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { int a; cin >> a; ++m1[a]; } int first; for (int i = 0; i < n; ++i) { int a; cin >> a; if (i == 0) first = a; ++m2[a]; } int ans = m + 1; for (auto& e : m1) { bool ok = true; if (e.second != m2[first]) continue; int x; if (e.first > first) x = first + m - e.first; else x = first - e.first; for (auto e2 : m1) { if (e2.second != m2[(e2.first + x) % m]) ok = false; } if (ok) ans = min(ans, x); } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { long long int n, m; cin >> n >> m; long long int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long int maxi = 0, mini = 1e9 + 1; for (int i = 0; i < n; i++) { cin >> b[i]; } sort(b, b + n); long long int t[n], ans = 1e9 + 1, i = 0; while (i < n) { long long int x = (b[0] - a[i] + m) % m; i++; for (int j = 0; j < n; j++) { t[j] = (a[j] + x) % m; } sort(t, t + n); long long int flag = 1; for (int j = 0; j < n; j++) { if (b[j] != t[j]) { flag = 0; break; } } if (flag) ans = min(ans, x); } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; int p[2005], q[2005]; void solve() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &p[i]); p[i] %= m; } for (int i = 0; i < n; i++) scanf("%d", &q[i]); map<int, int> mp, mp1; for (int i = 0; i < n; i++) { mp[p[i]]++, mp1[q[i]]++; } map<int, int> MAP; for (auto it : mp) { for (auto jt : mp1) { if (it.second == jt.second) { if (jt.first >= it.first) { MAP[jt.first - it.first]++; } else { MAP[(abs(jt.first - 0) + abs(m - it.first))]++; ; } } } } for (auto it : MAP) { if (it.second == (int)(mp).size()) { printf("%d\n", it.first); return; } } } int main() { int tc = 1; while (tc--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename... T> void see(T&... args) { ((cin >> args), ...); } template <typename... T> void put(T&&... args) { ((cout << args << " "), ...); } template <typename... T> void putl(T&&... args) { ((cout << args << " "), ...); cout << '\n'; } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << "=" << a << ", "; err(++it, args...); } const long long inf = INT_MAX; const long double ep = 0.0000001; const long double pi = acos(-1.0); const long long md = 1000000007; void solve() { long long n, m; see(n, m); long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; }; long long b[n]; for (long long i = 0; i < n; i++) { cin >> b[i]; }; map<long long, long long> mp, mp2; for (long long i = 0; i < n; ++i) { mp[b[i]]++; mp2[a[i]]++; } vector<long long> v; long long x = mp.begin()->second, y = mp.begin()->first; for (auto [i, j] : mp2) { if (j == x) v.push_back(i); } long long mn = inf; for (auto k : v) { long long z = (y - k) % m; while (z < 0) z += m; map<long long, long long> mp3; for (long long i = 0; i < n; ++i) { mp3[(a[i] + z) % m]++; } long long f = 0; for (auto [i, j] : mp3) { if (mp.find(i) == mp.end() || mp[i] != j) { f = 1; break; } } if (f == 0) mn = min(mn, z); } put(mn); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const int N = 2001; int a[N], b[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } sort(a, a + n); sort(b, b + n); int ans = -1; for (int it = 0; it < n; it++) { int x = (b[0] + m - a[it]) % m; bool f = true; for (int i = 0; i < n; i++) { if ((a[(it + i) % n] + x) % m != b[i]) { f = false; break; } } if (f) { ans = x; break; } } cout << ans; return 0; }
#include <bits/stdc++.h> const int maxn = 1e9; const int N = 2020; const int INF = 0x3f3f3f3f; using namespace std; int main() { vector<int> a, b; int n, m; scanf("%d%d", &n, &m); a.resize(n), b.resize(n); for (int i = 0; i <= n - 1; i++) { scanf("%d", &a[i]); } for (int i = 0; i <= n - 1; i++) { scanf("%d", &b[i]); } sort(b.begin(), b.end()); int minn = INF; for (int i = 0; i <= n - 1; i++) { int x; if (a[i] <= b[0]) x = b[0] - a[i]; else x = m + b[0] - a[i]; vector<int> c(a); for (int j = 0; j <= n - 1; j++) { c[j] = (c[j] + x) % m; } sort(c.begin(), c.end()); if (c == b) minn = min(minn, x); } printf("%d\n", minn); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; t = 1; while (t--) { long long n, m; cin >> n >> m; vector<long long> a(n), b(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 0; i < n; i++) { cin >> b[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long diff, ans = INT_MAX; bool flg = true; for (long long i = 0; i < n; i++) { flg = true; diff = (b[i] - a[0] + m) % m; vector<long long> c(n); for (long long j = 0; j < n; j++) { c[j] = (a[j] + diff) % m; } sort(c.begin(), c.end()); for (long long k = 0; k < n; k++) { if (c[k] != b[k]) { flg = false; break; } } if (flg) { ans = min(ans, diff); } } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2005; int a[N], b[N]; int solve(vector<int>& vet, int m, int n) { int x; if (vet[0] <= b[0]) x = b[0] - vet[0]; else x = b[0] + m - vet[0]; for (int i = 0; i < n; i++) { vet[i] += x; vet[i] %= m; } sort(vet.begin(), vet.end()); for (int i = 0; i < n; i++) if (vet[i] != b[i]) return -1; return x; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { scanf("%d", &b[i]); } sort(b, b + n); long long resp = 1000000000000; for (int i = 0; i < n; i++) { vector<int> vet; for (int j = i; j < n; j++) vet.push_back(a[j]); for (int j = 0; j < i; j++) vet.push_back(a[j]); int ans = solve(vet, m, n); if (ans != -1) { resp = min((long long)ans, resp); } } printf("%d", resp); }
#include <bits/stdc++.h> using namespace std; const int maxn = 2005; int a[maxn]; int b[maxn]; int c[maxn]; int main() { int n, m; int ans = 0x3f3f3f3f; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b, b + n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) c[j] = a[j]; int cnt; if (b[i] - c[0] < 0) cnt = b[i] - c[0] + m; else cnt = b[i] - c[0]; int flag = 0; for (int j = 0; j < n; j++) c[j] = (c[j] + cnt) % m; sort(c, c + n); for (int j = 0; j < n; j++) if (c[j] != b[j]) { flag = 1; break; } if (!flag) { ans = min(ans, cnt); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2005; const int INF = 2e9 + 5; int n, m, a[N], b[N], ans = INF; int Dif(int p, int q) { if (p <= q) { return (q - p); } else { return (q + m - p); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { cin >> b[i]; } sort(b + 1, b + n + 1); for (int sta = 1; sta <= n; ++sta) { int i = sta + 1; if (i == n + 1) i = 1; int should = Dif(a[sta], b[1]); bool ok = 1; for (int j = 2; j <= n; ++j) { if (Dif(a[i], b[j]) != should) { ok = 0; break; } ++i; if (i == n + 1) i = 1; } if (ok) { ans = min(ans, should); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000000 + 10, mod = 1000 * 1000 * 1000 + 7; long long int n, m, a[MAXN], b[MAXN], ap[MAXN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; if (m == 1) { cout << 0; return 0; } sort(b, b + n); long long int ans = 1000 * 1000 * 1000 + 100; for (int i = 0; i < n; i++) { long long int x = b[i] - a[0]; if (x >= 0 && (a[0] + x) % m == b[i]) { for (int j = 0; j < n; j++) ap[j] = (a[j] + x) % m; sort(ap, ap + n); bool flag = 1; for (int j = 0; j < n; j++) if (ap[j] != b[j]) flag = 0; if (flag) ans = min(ans, x); } x = (x + 2 * m) % m; if (x >= 0 && (a[0] + x) % m == b[i]) { for (int j = 0; j < n; j++) ap[j] = (a[j] + x) % m; sort(ap, ap + n); bool flag = 1; for (int j = 0; j < n; j++) if (ap[j] != b[j]) flag = 0; if (flag) ans = min(ans, x); } } while (ans == 1000 * 1000 * 1000 + 100) { } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, i, j, k, l, Min = INT_MAX; cin >> n >> m; vector<int> v, a, b; for (i = 0; i < n; i++) { cin >> j; v.push_back(j); } for (i = 0; i < n; i++) { cin >> j; a.push_back(j); } sort(a.begin(), a.end()); for (i = 0; i < n; i++) { if (a[i] >= v[0]) k = a[i] - v[0]; else if (a[i] < v[0]) k = m - v[0] + a[i]; b.clear(); for (j = 0; j < n; j++) { b.push_back((v[j] + k) % m); } sort(b.begin(), b.end()); long long flag = 0; for (l = 0; l < n; l++) { if (b[l] != a[l]) { flag = 1; break; } } if (flag == 0 && k < Min) Min = k; } cout << Min; }
#include <bits/stdc++.h> using namespace std; int main() { long long int m, n; cin >> n >> m; long long int a[n], b[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } for (long long int i = 0; i < n; i++) { cin >> b[i]; } long long int min = 1000000007; sort(a, a + n); sort(b, b + n); bool ans = true; long long int temp[n]; for (long long int i = 0; i < n; i++) { ans = true; long long int inc; if (b[0] - a[i] >= 0) inc = b[0] - a[i]; else inc = (m + b[0] - a[i]); for (long long int j = 0; j < n; j++) { temp[j] = (a[j] + inc) % m; } sort(temp, temp + n); for (long long int j = 0; j < n; j++) { if (b[j] != temp[j]) { ans = false; break; } } if (ans == true) { if (min > inc) { min = inc; } } } cout << min; }
#include <bits/stdc++.h> using namespace std; int n, m, a[2001], b[2001], ans, x; int main() { cin >> n >> m; int i, j; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= n; i++) cin >> b[i]; sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); ans = m; for (i = 1; i <= n; i++) { if (a[1] <= b[i]) x = b[i] - a[1]; else x = b[i] - a[1] + m; int f = 1; for (j = i; j <= n; j++) if (b[j] != (x + a[j - i + 1]) % m) f = 0; for (j = 1; j < i; j++) if (b[j] != (x + a[n - i + 1 + j]) % m) f = 0; if (f == 1) ans = min(ans, x); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int v[2005], w[2005]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> v[i]; for (int i = 1; i <= n; i++) cin >> w[i]; sort(v + 1, v + n + 1); sort(w + 1, w + n + 1); int minX = m; for (int i = 1; i <= n; i++) { int x = ((long long)w[i] - v[1] + m) % m; if (x > minX) continue; vector<int> newV; for (int j = 1; j <= n; j++) newV.push_back(((long long)v[j] + x) % m); sort(newV.begin(), newV.end()); bool valid = true; for (int j = 0; j < n; j++) if (newV[j] != w[j + 1]) valid = false; if (valid) minX = min(x, minX); } cout << minX; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T = 1; for (long long n, m; T-- > 0 && cin >> n >> m;) { map<int, int> a, b; for (int i = 0, x; i < n && cin >> x; ++i) ++a[x]; for (int i = 0, x; i < n && cin >> x; ++i) ++b[x]; map<int, vector<int>> c, d; for (const auto& p : a) c[p.second].push_back(p.first); for (const auto& p : b) d[p.second].push_back(p.first); auto chk = [&](int e) { for (const auto& p : c) { const auto& v1 = p.second; const auto& v2 = d[p.first]; int i = 0, j = 0; while (j < v2.size() && v2[j] != (v1[i] + e) % m) ++j; if (j >= v2.size()) return false; while (i < v1.size()) { if ((v1[i] + e) % m != v2[j]) return false; ++i; (++j) %= v2.size(); } } return true; }; int z = 1000010000; for (const auto& p : c) z = min(z, (int)p.second.size()); int ans = m; for (const auto& p : c) { if (z == p.second.size()) { for (const auto x : d[p.first]) { int e = (x - p.second[0]) % m; if (e < 0) e += m; if (chk(e)) ans = min(ans, e); } break; } } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int MOD = (long long int)1e9 + 7; long long int powmod(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b = b >> 1; } return res % MOD; } void solve(long long int testnum) { long long int n, m; cin >> n >> m; vector<long long int> a(n), b(n); for (long long int i = 0; i < n; i++) { cin >> a[i]; } for (long long int i = 0; i < n; i++) { cin >> b[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long int ans = (long long int)1e9; for (long long int i = 0; i < n; i++) { long long int st = (m + b[i] - a[0]) % m; long long int check = 1; for (long long int j = 0; j < n; j++) { long long int diff = (m + b[(i + j) % n] - a[j]) % m; if (diff != st) { check = 0; break; } } if (check == 1) { ans = min(ans, st); } } cout << ans << "\n"; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; for (long long int i = 1; i <= t; i++) { solve(i); } }
#include <bits/stdc++.h> using namespace std; signed main() { long long n, m; cin >> n >> m; vector<long long> v1(n), v2(n); map<long long, long long> c1, c2; for (long long i = 0; i < n; i++) { cin >> v1[i]; c1[v1[i]]++; } for (long long i = 0; i < n; i++) { cin >> v2[i]; c2[v2[i]]++; } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); vector<long long> d; for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { if (v2[j] - v1[i] >= 0) { d.push_back(v2[j] - v1[i]); } else d.push_back(v2[j] - v1[j] + m); } } d.resize(unique(d.begin(), d.end()) - d.begin()); sort(d.begin(), d.end()); for (long long i = 0; i < d.size(); i++) { long long check = 1; for (long long j = 0; j < n; j++) { if (c1[v1[j]] != c2[(v1[j] + d[i]) % m]) { check = 0; break; } } if (check) { cout << d[i] << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; int A[2005], B[2005], C[2005]; int FindMinimumValue(int N, int m) { sort(B + 1, B + N + 1); sort(A + 1, A + N + 1); int i, j, x, Ans = m; for (i = 1; i <= N; ++i) { if (A[i] != B[i]) break; } if (i == N + 1) return 0; for (i = 1; i <= N; ++i) { if (B[i] >= A[1]) x = B[i] - A[1]; else x = m - (A[1] - B[i]); for (j = 1; j <= N; ++j) C[j] = (A[j] + x) % m; sort(C + 1, C + N + 1); for (j = 1; j <= N; ++j) { if (B[j] != C[j]) break; } if (j == N + 1) Ans = min(Ans, x); } return Ans; } int main() { int N, m, i; scanf("%d%d", &N, &m); for (i = 1; i <= N; ++i) scanf("%d", A + i); for (i = 1; i <= N; ++i) scanf("%d", B + i); printf("%d\n", FindMinimumValue(N, m)); return 0; }
#include <bits/stdc++.h> using namespace std; int finder(int toreserve[], int k, int p) { int pos = 1; while (pos < p) { if (toreserve[2 * pos] >= k) { pos = 2 * pos; } else { k -= toreserve[2 * pos]; pos = 2 * pos + 1; } } return pos - p; } int query(int tree[], int l, int r) { int ans = 0; while (l < r) { if (l % 2) { ans += tree[l]; l++; } if (r % 2) { r--; ans += tree[r]; } l /= 2; r /= 2; } return ans; } void update(int tree[], int pos, int val) { while (pos > 0) { tree[pos] += val; pos /= 2; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int i, j, tt = 1; while (tt--) { long long int temp, n, m, diff, flag = 0, ans = 1e9 + 1; cin >> n >> m; long long int a[n], b[n]; map<long long int, long long int> m1, m2; for (i = 0; i < n; i++) { cin >> a[i]; m1[a[i]]++; } for (i = 0; i < n; i++) { cin >> b[i]; m2[b[i]]++; } sort(a, a + n); sort(b, b + n); temp = -1; for (i = 0; i < n; i++) { if (temp == b[i]) continue; else { temp = b[i]; diff = (b[i] - a[0] + m) % m; } flag = 0; for (auto x : m1) { if (m2.find((x.first + diff) % m) == m2.end()) { flag = 1; break; } if (m2[(x.first + diff) % m] != m1[x.first]) { flag = 1; break; } } if (flag == 0) { ans = min(ans, diff); } } cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; int a[2005], b[2005], d; int ans = 1e9 + 7; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; sort(a, a + n); sort(b, b + n); for (int i = 0, j; i < n; ++i) { int d = ((b[i] - a[0]) % m + m) % m; j = 0; for (j = 0; j < n; ++j) { if ((a[j] - b[(j + i) % n] + d) % m) break; } if (j == n) ans = min(ans, d); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int KINF = 0x3f3f3f3f; const int N = 2010; int a[N], b[N], c[N]; int n, m; bool check(int x) { for (int i = (int)0; i < (int)n; ++i) c[i] = (a[i] + x) % m; sort(c, c + n); for (int i = (int)0; i < (int)n; ++i) if (c[i] != b[i]) return false; return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = (int)0; i < (int)n; ++i) cin >> a[i]; for (int i = (int)0; i < (int)n; ++i) cin >> b[i]; int ans = KINF; sort(b, b + n); for (int i = (int)0; i < (int)n; ++i) { int x = (b[0] - a[i] + m) % m; if (check(x)) ans = min(ans, x); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> long long a[2005]; long long b[2005]; long long c[2005]; long long d[2000]; void merge(long long arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(long long arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } int main() { long long count, n, q, i, j, k, x, y, z, l, ans, min, f, len, m, r, t, max; scanf("%lld %lld", &n, &m); for (i = 0; i < n; i++) scanf("%lld", &a[i]); for (i = 0; i < n; i++) scanf("%lld", &b[i]); mergeSort(a, 0, n - 1); mergeSort(b, 0, n - 1); x = 0; for (i = 0; i < n; i++) { if (a[i] == b[i]) x = x + 1; } if (x == n) { printf("0\n"); return 0; } z = 0; for (i = 0; i < n; i++) { if (b[0] < a[i]) { for (j = 0; j < n; j++) { c[j] = (a[j] + m + b[0] - a[i]) % m; } } if (b[0] > a[i]) { for (j = 0; j < n; j++) { c[j] = (a[j] + b[0] - a[i]) % m; } } mergeSort(c, 0, n - 1); x = 0; for (k = 0; k < n; k++) { if (c[k] == b[k]) x = x + 1; } if (x == n) { if (b[0] > a[i]) { d[z] = b[0] - a[i]; z++; } if (b[0] < a[i]) { d[z] = m + b[0] - a[i]; z++; } } } min = 1000000000000; for (i = 0; i < z; i++) { if (d[i] < min && d[i] <= 1e9) min = d[i]; } printf("%lld\n", min); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<ll>; using pi = pair<ll, ll>; using vii = vector<pi>; using vvi = vector<vi>; const ll MOD = 1e9 + 7; struct unordered_custom { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<int64_t, int64_t> Y) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM); } }; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int64_t generate(int64_t L, int64_t R) { uniform_int_distribution<int64_t> generator(L, R); return generator(rng); } inline int64_t generate() { uniform_int_distribution<int64_t> generator(INT64_MIN, INT64_MAX); return generator(rng); } inline ll read_int() { ll x; cin >> x; return x; } inline string read_str() { string x; cin >> x; return x; } inline void read(vi& x) { for (auto& i : (x)) cin >> i; } inline void out(vi x) { for (auto& i : (x)) cout << i << ' '; cout << '\n'; ; } inline void out(ll x) { cout << x << '\n'; ; } int tc; void test_case() { ll N = read_int(); ll M = read_int(); vi A(N); read(A); vi B(N); read(B); sort(B.begin(), B.end()); ll ANS = LLONG_MAX; for (int j = (0); (j) < (N); ++(j)) { ll X = (B[0] + M - A[j] + M) % M; vi C(N); for (int i = (0); (i) < (N); ++(i)) { C[i] = (A[i] + X) % M; } sort(C.begin(), C.end()); if (B == C) { ANS = min(ANS, X); } } out(ANS); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll TC = 1; for (int i = (0); (i) < (TC); ++(i)) tc++, test_case(); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m; map<long long, long long> ca, cb; int main() { cin >> n >> m; vector<long long> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; ca[a[i]]++; } for (int i = 0; i < n; i++) { cin >> b[i]; cb[b[i]]++; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); bool f = false; long long it = 0, t = 0; for (int i = 0; i < n; i++) { if (ca[a[it]] != cb[b[i]]) continue; if (a[it] <= b[i]) t = b[i] - a[it]; else t = m - a[it] + b[i]; f = true; it++; for (int j = (i + 1) % n; j != i; j = (j + 1) % n, it = (it + 1) % n) { if (ca[a[it]] != cb[b[j]] || a[it] <= b[j] && b[j] - a[it] != t || a[it] > b[j] && m - a[it] + b[j] != t) { f = 0; break; } } if (f) { cout << t; return 0; } it = 0; } }
#include <bits/stdc++.h> using namespace std; long long a[2010], b[2010], c[2010]; long long n, m; long long chk() { long long i, j; for (i = 0; i < n; i++) { if (b[i] != c[i]) return 0; } return 1; } int main() { long long i, j, k, l; scanf("%lld %lld", &n, &m); for (i = 0; i < n; i++) { scanf("%lld", &a[i]); } for (i = 0; i < n; i++) scanf("%lld", &b[i]); sort(a, a + n); sort(b, b + n); long long ans = 1e18; for (i = 0; i < n; i++) { long long x = b[i] - a[0]; if (x < 0) x += m; for (j = 0; j < n; j++) c[j] = (a[j] + x) % m; sort(c, c + n); if (chk() == 1) ans = min(ans, x); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long n = 0, f = 1, ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { n = n * 10 + ch - '0'; ch = getchar(); } return n * f; } long long n, x; long long a[100001], b[100001], c[100001]; signed main() { n = read(); long long jiaa = 0, ans = 1121131433448687LL, mod; mod = read(); for (long long i = 1; i <= n; i++) { x = read(); a[i] = x; } for (long long i = 1; i <= n; i++) { x = read(); b[i] = x; } sort(b + 1, b + n + 1); bool flag = false; for (long long i = 1; i <= n; i++) { jiaa = (b[i] - a[1] + mod) % mod; for (long long j = 1; j <= n; j++) { c[j] = (a[j] + jiaa) % mod; } sort(c + 1, c + n + 1); flag = false; for (long long j = 1; j <= n; j++) if (b[j] != c[j]) { flag = true; break; } if (flag == false) ans = min(ans, jiaa); } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long pow(long long a, long long b, long long m = 998244353) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } const long long N = 1000005; long long modInv(long long a, long long m) { a = a % m; for (long long x = 1; x < m; x++) if ((a * x) % m == 1) return x; } long long modinv(long long a, long long m = 998244353) { return pow(a, m - 2, m); } long long dp[25][1005]; void solve() { long long n, m; cin >> n >> m; long long a[n], b[n]; map<long long, long long> aa, bb; set<long long> s; for (long long i = 0; i < n; ++i) { cin >> a[i]; aa[a[i]]++; s.insert(a[i]); } for (long long i = 0; i < n; ++i) { cin >> b[i]; bb[b[i]]++; } auto it = s.begin(); long long cnt = 0; for (long long i = 0; i < aa.size(); ++i) { if (bb.find(*it) != bb.end() and aa[*it] == bb[*it]) { aa.erase(*it); bb.erase(*it); cnt++; } it++; } if (cnt == m) { cout << "0"; return; } auto p = aa.begin(); long long key = p->first; long long ele = p->second; long long ans = INT_MAX; for (auto x : bb) { if (x.second == ele) { multiset<long long> s1, s2; long long val = abs((m + x.first - key) % m); for (long long i = 0; i < n; ++i) { s1.insert((a[i] + val) % m); s2.insert((b[i])); } long long sum = 0; auto foo = s1.begin(); auto foo1 = s2.begin(); while (foo != s1.end()) { sum += abs(*foo - *foo1); foo++; foo1++; } if (sum == 0) { ans = min(ans, val); } } } cout << ans; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> ta, tb; int main() { cin >> n >> m; int b; for (int i = 0; i < n; i++) { cin >> b; ta.push_back(b); } for (int i = 0; i < n; i++) { cin >> b; tb.push_back(b); } sort(ta.begin(), ta.end()); sort(tb.begin(), tb.end()); int small = 1000000000; for (int i = 0; i < n; i++) { int a = (tb[0] - ta[0] + m) % m; bool works = true; for (int x = 1; x < n; x++) { if ((tb[x] - ta[x] + m) % m != a) { works = false; break; } } if (works) small = min(a, small); rotate(ta.begin(), ta.begin() + 1, ta.end()); } cout << small; return 0; }
#include <bits/stdc++.h> using namespace std; int mod = 1000000007; const int inf = 1034567891; const long long LL_INF = 1234567890123456789ll; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <typename T> T GCD(T a, T b) { long long t; while (a) { t = a; a = b % a; b = t; } return b; } template <typename T> string toString(T a) { return to_string(a); } template <typename T> void toInt(string s, T& x) { stringstream str(s); str >> x; } inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; } inline int mul(int x, int y) { return (x * 1ll * y) % mod; } inline int powr(int a, long long b) { int x = 1 % mod; while (b) { if (b & 1) x = mul(x, a); a = mul(a, a); b >>= 1; } return x; } inline int inv(int a) { return powr(a, mod - 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<int> vec1, vec2; int v; for (int i = 0; i < n; i++) { cin >> v; vec1.push_back(v); } for (int i = 0; i < n; i++) { cin >> v; vec2.push_back(v); } sort(vec2.begin(), vec2.end()); int ans = inf; sort(vec1.begin(), vec1.end()); for (int i = 0; i < n; i++) { int y = (vec2[i] - vec1[n - 1] + m) % m; vector<int> temp; for (int j = 1; j <= 10; j++) { int x = y * j; temp.clear(); for (auto it : vec1) { temp.push_back((it + x) % m); } sort(temp.begin(), temp.end()); if (temp == vec2) { ans = min(ans, x); } } } assert(ans != inf); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int N = 2e5 + 5; void solve() { long long int n, m, i; cin >> n >> m; vector<long long int> a(n), b(n); for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { cin >> b[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long int ans = LLONG_MAX; for (i = 0; i < n; i++) { long long int x; if (b[i] >= a[0]) { x = b[i] - a[0]; } else { x = m - a[0] + b[i]; } vector<long long int> c(n); for (long long int j = 0; j < n; j++) { c[j] = (a[j] + x) % m; } sort(c.begin(), c.end()); if (b == c) { ans = min(ans, x); } } cout << ans; } signed main() { ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); long long int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int dx[4] = {0, 0, -1, 1}; int dy[4] = {-1, 1, 0, 0}; int par[1005]; int _find(int x) { return par[x] == x ? x : par[x] = _find(par[x]); } int _union(int x, int y) { x = _find(x), y = _find(y); if (x == y) return 0; par[x] = y; return 1; } int N, M; int arr[2005], brr[2005]; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M; for (int i = 0; i < N; i++) { cin >> arr[i]; } for (int i = 0; i < N; i++) { cin >> brr[i]; } sort(arr, arr + N); sort(brr, brr + N); for (int i = 0; i < N; i++) { int cha = brr[i] - arr[0]; if (cha < 0) cha += M; if (cha >= M) cha -= M; int chk = 1; for (int j = 0; j < N; j++) { int ccha = brr[(j + i) % N] - arr[j]; if (ccha < 0) ccha += M; if (ccha >= M) ccha -= M; if (ccha != cha) { chk = 0; } } if (chk) { cout << cha << '\n'; return 0; } } }
#include <bits/stdc++.h> const long long int MAX = (long long int)(1e18 + 1); const long long int MIN = (long long int)(-1e18 - 1); const long long int mod = (long long int)(1e9 + 7); using namespace std; long long int power(long long int a, long long int n) { long long int p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long int power_mod(long long int a, long long int n, long long int mod_) { long long int p = 1; while (n) { if (n % 2) { p = (p * a) % mod_; } n /= 2; a = (a * a) % mod_; } return p % mod_; } void ingraph(vector<vector<long long int> >& graph, long long int m) { long long int x, y, i; for (i = 0; i < m; i++) { cin >> x >> y; x--, y--; graph[x].push_back(y); graph[y].push_back(x); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long int mn = MAX, mx = MIN; long long int n, m, k, i, j, sum = 0, prev, flag = 0, cnt = 0, even = 0; long long int x = 0, y = 0, fx; int TC = 1; cin >> n >> m; vector<long long int> a(n), b(n); for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) cin >> b[i]; sort((b).begin(), (b).end()); multiset<long long int> s; x = b[i] - a[i] + m; x %= m; for (i = 0; i < n; i++) { x = (b[i] - a[0] + m) % m; x %= m; s.clear(); for (j = 0; j < n; j++) { s.insert(b[j]); } for (j = 0; j < n; j++) { y = (a[j] + x + m) % m; if (s.find(y) == s.end()) { break; } else { auto it = s.find(y); s.erase(it); } } if (s.empty()) { cout << x << '\n'; return 0; } } while (TC--) { } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } const int maxn = 2009; int n; int m; vector<int> a; vector<int> b; int ans; map<int, int> bfreq; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; a.resize(n); b.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); ans = 2e9; for (int i = 0; i < n; i++) { set<int> diffs; for (int j = 0; j < n; j++) { diffs.insert((b[j] - a[j] + m) % m); } if (diffs.size() <= 1) { ans = min(ans, *diffs.begin()); } b.push_back(b.front()); b.erase(b.begin()); } assert(ans < 2e9); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; vector<long long> a(n), b(n); for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) cin >> b[i]; sort(b.begin(), b.end()); long long ans = INT_MAX; for (long long i = 0; i < n; i++) { long long x = (b[i] - a[0] + m) % m; vector<long long> c(n); for (long long j = 0; j < n; j++) c[j] = (a[j] + x + m) % m; sort(c.begin(), c.end()); if (b == c) ans = min(ans, x); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int arr[n], brr[n], crr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n; i++) cin >> brr[i]; sort(brr, brr + n); int x; int cnt, mn = 2000000000; for (int i = 0; i < n; i++) { cnt = 0; x = (m + brr[i] - arr[0]) % m; for (int j = 0; j < n; j++) { crr[j] = (arr[j] + x) % m; } sort(crr, crr + n); for (int k = 0; k < n; k++) { if (crr[k] == brr[k]) cnt++; } if (cnt == n) { mn = min(mn, x); } } cout << mn; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(NULL); long long n, m; cin >> n >> m; long long a[n], b[n]; for (int j = 0; j < n; j++) cin >> a[j]; for (int j = 0; j < n; j++) cin >> b[j]; bool flag = 0; long long ans = INT_MAX; while (flag == 0) { for (int i = 0; i < n; i++) { long long c[n], d[n]; for (int j = 0; j < n; j++) { c[j] = a[j]; d[j] = b[j]; } long long start = b[0], end = a[i]; while (start < end) { start = start + m; } long long x = start - end; for (int j = 0; j < n; j++) c[j] = (c[j] + x) % m; sort(c, c + n); sort(d, d + n); int k; for (k = 0; k < n; k++) { if (c[k] == d[k]) continue; else break; } if (k == n) { ans = min(ans, x); } } flag = 1; } cout << ans; return 0; }