text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; using ll = long long; tuple<ll, ll> t45(ll x, ll y) { return {x + y, x - y}; } ll nsec(ll x, ll y, ll k) { if (x > y) swap(x, y); return int(floor(double(y) / k) - floor(double(x + k - 1) / k) + 1.2); } ll a, b, x[2], y[2]; int main() { ios_base::sync_with_stdio(0); cin >> a >> b; for (int i = (0); i < (2); i++) cin >> x[i] >> y[i]; for (int i = (0); i < (2); i++) tie(x[i], y[i]) = t45(x[i], y[i]); ll seca = nsec(x[0], x[1], 2 * a), secb = nsec(y[0], y[1], 2 * b); cout << max(seca, secb) << "\n"; }
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a >= b) return a; else return b; } long long int min(long long int a, long long int b) { if (a <= b) return a; else return b; } long long int kabs(long long int a) { if (a >= 0) return a; else return -a; } long long int num(vector<long long int> mono, vector<long long int> jun, long long int n) { long long int tmp = 0; for (long long int i = 0; i < ((long long int)(n)); i++) { tmp = tmp * 10 + mono[jun[i]]; } return tmp; } int main() { long long int a, b, x1, y1, x2, y2; long long int wa1, wa2, sa1, sa2; long long int mod1, mod2, m1, m2, res = 0; int flag[6] = {0}; cin >> a >> b >> x1 >> y1 >> x2 >> y2; wa1 = x1 + y1; wa2 = x2 + y2; sa1 = x1 - y1; sa2 = x2 - y2; mod1 = wa1 / (a * 2); mod2 = wa2 / (a * 2); if (wa1 < 0) mod1--; if (wa2 < 0) mod2--; res += kabs(mod1 - mod2); m1 = sa1 / (b * 2); m2 = sa2 / (b * 2); if (sa1 < 0) m1--; if (sa2 < 0) m2--; res += kabs(m1 - m2); if (wa1 % (a * 2) == 0) flag[0] = 1; if (sa1 % (b * 2) == 0) flag[1] = 1; if (wa2 % (a * 2) == 0) flag[2] = 1; if (sa2 % (b * 2) == 0) flag[3] = 1; res += (flag[0] || flag[1]) + (flag[2] || flag[3]); if (flag[0] && wa1 > wa2) flag[4] = 1; if (flag[2] && wa1 < wa2) flag[4] = 1; if (flag[1] && sa1 > sa2) flag[5] = 1; if (flag[3] && sa1 < sa2) flag[5] = 1; res -= flag[4] || flag[5]; res -= min(kabs(m1 - m2), kabs(mod1 - mod2)); if (x1 == x2 && y1 == y2) { res = 0 + (flag[0] || flag[1]); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, x1, y1, x2, y2, left, right; cin >> a >> b >> x1 >> y1 >> x2 >> y2; int r1 = (x2 - y2) / (2 * b); int r2 = (x1 - y1) / (2 * b); int r3 = (x2 + y2) / (2 * a); int r4 = (x1 + y1) / (2 * a); left = ((x2 - y2) / (2 * b) - (x1 - y1) / (2 * b)); right = ((x2 + y2) / (2 * a) - (x1 + y1) / (2 * a)); if (x2 - y2 < 0) left -= 1; if (x1 - y1 < 0) left += 1; if (x2 + y2 < 0) right -= 1; if (x1 + y1 < 0) right += 1; cout << max(abs(left), abs(right)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, x2, y1, y2; int s, t, sum = 0; cin >> a >> b >> x1 >> y1 >> x2 >> y2; a *= 2; b *= 2; s = x1 + y1; t = x2 + y2; if (s < 0) { if ((-s) % a == 0) s /= a; else s = s / a - 1; } else s /= a; if (t < 0) { if ((-t) % a == 0) t /= a; else t = t / a - 1; } else t /= a; sum = (int)(fabs(s - t)); s = x1 - y1; t = x2 - y2; if (s < 0) { if ((-s) % b == 0) s /= b; else s = s / b - 1; } else s /= b; if (t < 0) { if ((-t) % b == 0) t /= b; else t = t / b - 1; } else t /= b; if (sum < (int)(fabs(s - t))) sum = (int)(fabs(s - t)); cout << sum << endl; scanf(" "); }
#include <bits/stdc++.h> using namespace std; const long long MAXN = -1; long long a, b, xa, xb, ya, yb; pair<long long, long long> getCuad(long long x, long long y) { if (x > 0 && -x < y && y < x) return {-1, 1}; if (x < 0 && x < y && y < -x) return {1, -1}; if (y < 0 && y < x && x < -y) return {-1, -1}; if (y > 0 && -y < x && x < y) return {1, 1}; assert(false); } pair<long long, long long> getCoord(long long x, long long y) { pair<long long, long long> M = getCuad(x, y); int X = abs(x + y) / a; int Y = abs(x - y) / b; X = X - (M.first == -1 ? 1 : 0); Y = Y - (M.second == -1 ? 1 : 0); return make_pair(X, Y); } void floordiv(int x, int y, int &q, int &r) { q = x / y; r = x % y; if ((r != 0) && (r < 0) != (y < 0)) q--, r += y; } int main() { while (scanf("%lld%lld%lld%lld%lld%lld", &a, &b, &xa, &ya, &xb, &yb) >= 1) { a *= 2; b *= 2; int q1, r1; floordiv(xa + ya, a, q1, r1); int q2, r2; floordiv(xa - ya, b, q2, r2); int q3, r3; floordiv(xb + yb, a, q3, r3); int q4, r4; floordiv(xb - yb, b, q4, r4); int v1 = abs(q1 - q3); int v2 = abs(q2 - q4); printf("%d\n", max(v1, v2)); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int _t[6]; int main() { for (int i = 0; i < 6; ++i) { scanf("%d", &_t[i]); } long long a, b, x1, y1, x2, y2; a = 2 * _t[0]; b = 2 * _t[1]; x1 = _t[2] - _t[3]; y1 = _t[2] + _t[3]; x2 = _t[4] - _t[5]; y2 = _t[4] + _t[5]; long long X1 = x1 / b + (x1 > 0); long long X2 = x2 / b + (x2 > 0); long long Y1 = y1 / a + (y1 > 0); long long Y2 = y2 / a + (y2 > 0); long long D = max(abs(X1 - X2), abs(Y1 - Y2)); cout << D << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a, b; int calc2(int x, int a) { if (x >= 0) return x / a / 2; return -((-x) / a / 2 + 1); } pair<int, int> calc(int x, int y) { return make_pair(calc2(x + y, a), calc2(x - y, b)); } int abs(int n) { if (n >= 0) return n; else return -n; } int calc2(int a, int b, int c, int d) { int dist = abs(d - b); int a1 = dist + abs(a + dist - c); int a2 = dist + abs(a - dist - c); return min(a1, a2); } int main() { int x1, x2, y1, y2; while (cin >> a >> b >> x1 >> y1 >> x2 >> y2) { pair<int, int> z1 = calc(x1, y1); pair<int, int> z2 = calc(x2, y2); cout << min(calc2(z1.first, z1.second, z2.first, z2.second), calc2(z1.second, z1.first, z2.second, z2.first)) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, y1, x2, y2; int x, y; cin >> a >> b >> x1 >> y1 >> x2 >> y2; x = x1, y = y1; x1 = x + y, y1 = x - y; x = x2, y = y2; x2 = x + y, y2 = x - y; a *= 2, b *= 2; x1 = x1 / a + (x1 > 0); x2 = x2 / a + (x2 > 0); y1 = y1 / b + (y1 > 0); y2 = y2 / b + (y2 > 0); cout << max(abs(x1 - x2), abs(y1 - y2)) << endl; getchar(); return 0; }
#include <bits/stdc++.h> inline int dist(int a, int b, int d) { assert(a % d && b % d); if (a >= 0) a /= d; else a = a / d - 1; if (b >= 0) b /= d; else b = b / d - 1; return abs(a - b); } int main() { int a, b, x1, y1, x2, y2; scanf("%d%d%d%d%d%d", &a, &b, &x1, &y1, &x2, &y2); printf("%d\n", ((dist(x1 + y1, x2 + y2, 2 * a)) > (dist(x1 - y1, x2 - y2, 2 * b)) ? (dist(x1 + y1, x2 + y2, 2 * a)) : (dist(x1 - y1, x2 - y2, 2 * b)))); return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, asdfasdf, asdf, asdfasdfasdfasd, asdfsdfsadfadsfasdf; int main() { cin >> a >> b >> asdfasdf >> asdf >> asdfasdfasdfasd >> asdfsdfsadfadsfasdf; long long l1 = (asdfasdf + asdf) / (2 * a) + (asdfasdf + asdf > 0); long long r1 = (asdfasdf - asdf) / (2 * b) + (asdfasdf - asdf > 0); long long l2 = (asdfasdfasdfasd + asdfsdfsadfadsfasdf) / (2 * a) + (asdfasdfasdfasd + asdfsdfsadfadsfasdf > 0); long long r2 = (asdfasdfasdfasd - asdfsdfsadfadsfasdf) / (2 * b) + (asdfasdfasdfasd - asdfsdfsadfadsfasdf > 0); cout << max(abs(l1 - l2), abs(r1 - r2)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { long long a, b, x1, y1, x2, y2, x3, y3, x4, y4, ans; cin >> a >> b >> x1 >> y1 >> x2 >> y2; x3 = (x1 + y1); x3 -= 2 * a * (x3 < 0); x3 /= (2 * a); y3 = (x1 - y1); y3 -= 2 * b * (y3 < 0); y3 /= (2 * b); x4 = (x2 + y2); x4 -= 2 * a * (x4 < 0); x4 /= (2 * a); y4 = (x2 - y2); y4 -= 2 * b * (y4 < 0); y4 /= (2 * b); ans = max(abs(x3 - x4), abs(y3 - y4)); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a < 0) a = -a; if (b < 0) b = -b; return b == 0 ? a : gcd(b, a % b); } template <typename T> void read(T &res) { bool flag = false; char ch; while (!isdigit(ch = getchar())) (ch == '-') && (flag = true); for (res = ch - 48; isdigit(ch = getchar()); res = (res << 1) + (res << 3) + ch - 48) ; flag && (res = -res); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long qp(long long a, long long b, long long mod) { long long ans = 1; if (b == 0) { return ans % mod; } while (b) { if (b % 2 == 1) { b--; ans = ans * a % mod; } a = a * a % mod; b = b / 2; } return ans % mod; } long long qpn(long long a, long long b, long long p) { long long ans = 1; a %= p; while (b) { if (b & 1) { ans = (ans * a) % p; --b; } a = (a * a) % p; b >>= 1; } return ans % p; } struct pe { long long x; long long y; long long xx; long long yy; long long pasx; long long pasy; } pos[3]; signed main() { long long a, b; read(a); read(b); a = a * 2; b = b * 2; for (long long i = 1; i <= 2; i++) { read(pos[i].x); read(pos[i].y); pos[i].xx = pos[i].x + pos[i].y; pos[i].yy = pos[i].y - pos[i].x; } for (long long i = 1; i <= 2; i++) { pos[i].pasx = pos[i].xx / (a); if (pos[i].xx > 0) { pos[i].pasx++; } } for (long long i = 1; i <= 2; i++) { pos[i].pasy = pos[i].yy / (b); if (pos[i].yy > 0) { pos[i].pasy++; } } printf("%lld\n", max(abs(pos[1].pasx - pos[2].pasx), abs(pos[1].pasy - pos[2].pasy))); }
#include <bits/stdc++.h> using namespace std; template <typename T> inline void smin(T &a, T b) { a = a < b ? a : b; } template <typename T> inline void smax(T &a, T b) { a = a > b ? a : b; } void err(istream_iterator<string> it) { cout << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); } template <typename T> inline void Int(T &n) { n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0'; n = n * f; } template <typename T, typename TT> inline void Int(T &n, TT &m) { Int(n); Int(m); } template <typename T, typename TT, typename TTT> inline void Int(T &n, TT &m, TTT &l) { Int(n, m); Int(l); } const int mod = (int)1e9 + 7; inline int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b) { return (long long)a * b % mod; } const int inf = (int)2e9 + 5; const long long Inf = (long long)2e18 + 5; const int N = (int)2e5 + 5; int solve() { int a, b, x1, y1, x2, y2; Int(a, b), Int(x1, y1), Int(x2, y2); int x = x1, y = y1; x1 = x + y, y1 = x - y; x = x2, y = y2; x2 = x + y, y2 = x - y; a *= 2, b *= 2; x1 = x1 / a + (x1 > 0); x2 = x2 / a + (x2 > 0); y1 = y1 / b + (y1 > 0); y2 = y2 / b + (y2 > 0); printf("%d\n", max(abs(x1 - x2), abs(y1 - y2))); return 0; } int main() { int test = 1, tc = 0; while (test--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int abs(int a) { if (a > 0) return a; return -a; } int min(int a, int b) { if (a > b) return b; return a; } int main() { int a, b, x_1, y_1, x_2, y_2; int k1, t2, k2, t1, com, ans; while (cin >> a >> b >> x_1 >> y_1 >> x_2 >> y_2) { k1 = (y_1 - x_1) / (2 * b); if (y_1 - x_1 < 0) k1--; t1 = (y_1 + x_1) / (2 * a); if (y_1 + x_1 < 0) t1--; k2 = (y_2 - x_2) / (2 * b); if (y_2 - x_2 < 0) k2--; t2 = (y_2 + x_2) / (2 * a); if (y_2 + x_2 < 0) t2--; com = min(abs(k1 - k2), abs(t1 - t2)); ans = abs(k1 - k2) + abs(t1 - t2) - com; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long A, B, x1, y1, x2, y2; cin >> A >> B >> x1 >> y1 >> x2 >> y2; A *= 2; B *= 2; long long s1 = x1 + y1; long long s2 = x2 + y2; if (s1 > s2) swap(s1, s2); long long tmp = s1 % A; if (s1 < 0) s1 += fabs(tmp); else s1 += A - fabs(tmp); tmp = s2 % A; if (s2 < 0) s2 -= A - fabs(tmp); else s2 -= fabs(tmp); long long res = 0LL; if (s1 <= s2) res += fabs(s2 - s1) / A + 1; long long t1 = x1 - y1; long long t2 = x2 - y2; if (t1 > t2) swap(t1, t2); tmp = t1 % B; if (t1 < 0) t1 += fabs(tmp); else t1 += B - fabs(tmp); tmp = t2 % B; if (t2 < 0) t2 -= B - fabs(tmp); else t2 -= fabs(tmp); if (t1 <= t2) res = max(res, (long long)fabs(t2 - t1) / B + 1); cout << res << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") const double PI = acos(-1.0); using namespace std; const int N = 100009, M = 1000000; long long ans[100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long a, b, r1, r2, c1, c2, u1, u2, v1, v2; cin >> a >> b >> r1 >> c1 >> r2 >> c2; u1 = r1; u2 = r2; v1 = c1; v2 = c2; r1 = u1 + v1, c1 = u1 - v1, r2 = u2 + v2, c2 = u2 - v2; if (r1 > r2) swap(r1, r2); if (c1 > c2) swap(c1, c2); a *= 2, b *= 2; r1 += a * 1e9, r2 += a * 1e9, c1 += b * 1e9, c2 += b * 1e9; long long ans1 = (r2 / a - (r1 - 1) / a); long long ans2 = (c2 / b - (c1 - 1) / b); cout << max(ans1, ans2); return 0; }
#include <bits/stdc++.h> using namespace std; void run() { FILE *fp = fopen("input.txt", "r"); if (fp) { fclose(fp); freopen("input.txt", "r", stdin); } string s; long a, b, x1, y1, x2, y2; cin >> a; cin >> b; cin >> x1; cin >> y1; cin >> x2; cin >> y2; long b1, b2; long a1, a2; b1 = x1 - y1; b2 = x2 - y2; long ansa, ansb; ansa = ansb = 0; long lb, sb; if (b1 >= b2) { lb = b1; sb = b2; } else { lb = b2; sb = b1; } long tb1 = lb / (2 * b); long tb2 = sb / (2 * b); ansb = tb1 - tb2; if (lb >= 0 && sb < 0) { ansb++; } a1 = x1 + y1; a2 = x2 + y2; long la, sa; if (a1 >= a2) { la = a1; sa = a2; } else { la = a2; sa = a1; } long ta1 = la / (2 * a); long ta2 = sa / (2 * a); ansa = ta1 - ta2; if (la >= 0 && sa < 0) { ansa++; } long ans = max(ansa, ansb); cout << ans << endl; } int main() { run(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B, X1, Y1, X2, Y2; cin >> A >> B >> X1 >> Y1 >> X2 >> Y2; A *= 2; B *= 2; int P1 = (X1 + Y1) / A; if (X1 + Y1 < 0) P1--; int Q1 = (X1 - Y1) / B; if (X1 - Y1 < 0) Q1--; int P2 = (X2 + Y2) / A; if (X2 + Y2 < 0) P2--; int Q2 = (X2 - Y2) / B; if (X2 - Y2 < 0) Q2--; int X = max(P1, P2) - min(P1, P2), Y = max(Q1, Q2) - min(Q1, Q2); cout << max(X, Y) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long gao(long long l1, long long r1, long long l2, long long r2) { if (l1 == l2 && r1 == r2) return 0; if (r1 <= l2) return l2 - r1 + 1; else return l1 - r2 + 1; } long long L(long long x, long long m) { if (x > 0) { long long y = x % m; return (x - y) / m; } else { x = -x; long long y = x % m; return -(x + (m - y)) / m; } } long long R(long long x, long long m) { if (x > 0) { long long y = x % m; return (x + m - y) / m; } else { x = -x; long long y = x % m; return -(x - y) / m; } } int main() { long long a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; a *= 2, b *= 2; long long u1 = R(x1 + y1, a); long long d1 = L(x1 + y1, a); long long u2 = R(x2 + y2, a); long long d2 = L(x2 + y2, a); long long ans = gao(d1, u1, d2, u2); u1 = R(y1 - x1, b); d1 = L(y1 - x1, b); u2 = R(y2 - x2, b); d2 = L(y2 - x2, b); ans = max(ans, gao(d1, u1, d2, u2)); cout << ans; }
#include <bits/stdc++.h> using namespace std; int fl(int num, int denom) { int ans = num / denom; if (num < 0 && ans * denom != num) --ans; return ans; } int main() { int a, b, x[2], y[2], n[2], m[2], i, j, ans; scanf("%d%d", &a, &b); for (i = 0; i < 2; ++i) { scanf("%d%d", x + i, y + i); n[i] = fl(x[i] + y[i], 2 * a); m[i] = fl(-x[i] + y[i], 2 * b); } ans = max(abs(n[0] - n[1]), abs(m[0] - m[1])); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; const int inf = 0x3fffffff; const double eps = 1e-8; const double pi = acos(-1.0); long long get1(long long x, long long y, long long a) { long long tmp = x + y; if (tmp < 0) { return tmp / a - 1; } return tmp / a; } long long get2(long long x, long long y, long long b) { long long tmp = y - x; if (tmp < 0) { return tmp / b - 1; } return tmp / b; } int main(int argc, char *argv[]) { long long a, b, x1, y1, x2, y2; while (scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &a, &b, &x1, &y1, &x2, &y2) != EOF) { if (a == 1 || b == 1) { long long ans = (abs(y1 - y2) + 1) / 2 + (abs(x1 - x2) + 1) / 2; printf("%I64d\n", ans); continue; } a <<= 1, b <<= 1; long long n1 = get1(x1, y1, a), n2 = get1(x2, y2, a); long long m1 = get2(x1, y1, b), m2 = get2(x2, y2, b); long long ans1 = abs(n1 - n2), ans2 = abs(m1 - m2); printf("%I64d\n", max(ans1, ans2)); } return 0; }
#include <bits/stdc++.h> using namespace std; long long A, B; struct DIEM { long long x, y; void init_value() { cin >> x >> y; } void move_45() { int x1 = x, y1 = y; x = x1 + y1; y = x1 - y1; } void xuat() { cout << x << ' ' << y << endl; } }; DIEM st, first; void file() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void init() { cin >> A >> B; st.init_value(); first.init_value(); } void process_data() { st.move_45(); first.move_45(); A *= 2; B *= 2; } long long Calc(long long s, long long mod) { return s / mod + (s > 0); } void solve() { long long x1 = Calc(st.x, A); long long x2 = Calc(first.x, A); long long y1 = Calc(st.y, B); long long y2 = Calc(first.y, B); cout << max(abs(x1 - x2), abs(y1 - y2)) << endl; } int main() { file(); init(); process_data(); solve(); }
#include <bits/stdc++.h> using namespace std; int a, b, x, y, xo, yo, xe, ye; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> a >> b >> xo >> yo >> xe >> ye; a *= 2; b *= 2; x = xo; y = yo; xo = x + y; yo = y - x; x = xe; y = ye; xe = x + y; ye = y - x; int xl = xo, xx = xe, yl = yo, yy = ye; xo = xo / a; yo = yo / b; xe = xe / a; ye = ye / b; if (yl > 0) ++yo; if (yy > 0) ++ye; if (xl > 0) ++xo; if (xx > 0) ++xe; cout << max(abs(xe - xo), abs(ye - yo)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, ans = 0; class vertex { public: long long x, y; vertex() {} vertex(long long _x, long long _y) { x = _x; y = _y; } }; long long m(long long a, long long md) { return (a % md + md) % md; } long long solve(long long p, long long q, long long mod) { if (p > q) swap(p, q); q = (q - m(q, mod)) / mod; p = (p - m(p, mod)) / mod; return q - p; } int main() { vertex n, m; cin >> a >> b >> n.x >> n.y >> m.x >> m.y; ans = max(solve(n.x + n.y, m.x + m.y, 2 * a), solve(-n.x + n.y, -m.x + m.y, 2 * b)); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int aa, bb, px, py, qx, qy; void conduct() { int r1, r2, c1, c2; r1 = (px + py) / (aa * 2); if (px + py < 0) r1--; r2 = (qx + qy) / (aa * 2); if (qx + qy < 0) r2--; c1 = (px - py) / (bb * 2); if (px - py < 0) c1--; c2 = (qx - qy) / (bb * 2); if (qx - qy < 0) c2--; printf("%d\n", max(abs(r1 - r2), abs(c1 - c2))); } int main() { while (scanf("%d%d%d%d%d%d", &aa, &bb, &px, &py, &qx, &qy) != EOF) conduct(); return 0; }
#include <bits/stdc++.h> using namespace std; int f(int a, int d) { return a / d - (a < 0); } int main() { int a, b; int x1, y1; int x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; cout << max(abs(f(x1 + y1, 2 * a) - f(x2 + y2, 2 * a)), abs(f(x1 - y1, 2 * b) - f(x2 - y2, 2 * b))); }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-6; const double PI = acos(-1.0); const int oo = 0x3f3f3f3f; long long read() { long long d, f = 1; char c; while (!isdigit(c = getchar())) if (c == '-') f = -1; d = c ^ '0'; while (isdigit(c = getchar())) d = (d * 10) + (c ^ '0'); return d * f; } int main() { long long a, b, x1, y1, x2, y2; while (cin >> a >> b >> x1 >> y1 >> x2 >> y2) { a <<= 1, b <<= 1; long long x = x1, y = y1; x1 = x + y, y1 = y - x; x = x2, y = y2; x2 = x + y, y2 = y - x; x1 = x1 / a + (x1 > 0); x2 = x2 / a + (x2 > 0); y1 = y1 / b + (y1 > 0); y2 = y2 / b + (y2 > 0); cout << max(abs(x1 - x2), abs(y1 - y2)) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; long long a1 = x1 + y1, a2 = x1 - y1, a3 = x2 + y2, a4 = x2 - y2; if (a1 < 0) a1 -= 2 * a - 1; if (a2 < 0) a2 -= 2 * b - 1; if (a3 < 0) a3 -= 2 * a - 1; if (a4 < 0) a4 -= 2 * b - 1; a1 /= (2 * a); a2 /= (2 * b); a3 /= (2 * a); a4 /= (2 * b); cout << max(abs(a1 - a3), abs(a2 - a4)); return 0; }
#include <bits/stdc++.h> using namespace std; int a, b, x, y, xx, yy, xv, xh, xxv, xxh; int main() { cin >> a >> b >> x >> y >> xx >> yy; if (x + y >= 0) xh = (x + y) / (a * 2); if (x + y < 0) xh = (x + y) / (a * 2) - 1; if (x - y <= 0) xv = (y - x) / (2 * b); if (x - y > 0) xv = (y - x) / (2 * b) - 1; if (xx + yy >= 0) xxh = (xx + yy) / (a * 2); if (xx + yy < 0) xxh = (xx + yy) / (a * 2) - 1; if (xx - yy <= 0) xxv = (yy - xx) / (2 * b); if (xx - yy > 0) xxv = (yy - xx) / (2 * b) - 1; cout << max(abs(xxh - xh), abs(xxv - xv)); }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000,100000000000") const long long INF = 2009000999; const double cp = 2 * asin(1.0); const double eps = 1e-9; const long long mod = 1000000007LL; using namespace std; int main() { int a, b, x, y, x1, x2, y1, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; x = x1; y = y1; x1 = x + y; y1 = y - x; x = x2; y = y2; x2 = x + y; y2 = y - x; a *= 2; b *= 2; x1 = x1 / a + (x1 > 0); x2 = x2 / a + (x2 > 0); y1 = y1 / b + (y1 > 0); y2 = y2 / b + (y2 > 0); cout << max(abs(x1 - x2), abs(y1 - y2)); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; long long a, b; long long X1, Y1; long long X2, Y2; int main() { cin >> a >> b; cin >> X1 >> Y1; cin >> X2 >> Y2; X1 = (X1 + Y1); Y1 = (X1 - 2 * Y1); X2 = (X2 + Y2); Y2 = (X2 - 2 * Y2); a *= 2; b *= 2; long long cnt_a = floor(max(X1, X2) / (double)a) - (ceil(min(X1, X2) / (double)a) - 1); long long cnt_b = floor(max(Y1, Y2) / (double)b) - (ceil(min(Y1, Y2) / (double)b) - 1); cout << max(cnt_a, cnt_b) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a, b, xa, ya, xb, yb; int main() { scanf("%d%d%d%d%d%d", &a, &b, &xa, &ya, &xb, &yb); a <<= 1, b <<= 1; int x1 = (xa + ya), y1 = (xa - ya), x2 = (xb + yb), y2 = (xb - yb); int sector_1_x = (x1 / a) - (x1 < 0), sector_1_y = (y1 / b) - (y1 < 0); int sector_2_x = (x2 / a) - (x2 < 0), sector_2_y = (y2 / b) - (y2 < 0); printf("%d\n", max(abs(sector_1_x - sector_2_x), abs(sector_1_y - sector_2_y))); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, x2, y1, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; int XA, YA, XB, YB; XA = x1 + y1; YA = x1 - y1; XB = x2 + y2; YB = x2 - y2; int difXA = abs(XA / (2 * a) - XB / (2 * a) + (XA > 0) - (XB > 0)); int difYA = abs(YA / (2 * b) - YB / (2 * b) + (YA > 0) - (YB > 0)); cout << max(difXA, difYA) << endl; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int main() { long long int a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; a *= 2; b *= 2; long long int ret1, ret2; long long int a1 = (x1 + y1) / a * a; if (x1 + y1 < 0) a1 -= a; long long int a2 = (x2 + y2) / a * a; if (x2 + y2 < 0) a2 -= a; ret1 = abs(a1 - a2) / a; long long int b1 = (x1 - y1) / b * b; if (x1 - y1 < 0) b1 -= b; long long int b2 = (x2 - y2) / b * b; if (x2 - y2 < 0) b2 -= b; ret2 = abs(b1 - b2) / b; cout << max(ret1, ret2) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int ts, kk = 1; template <class T> inline T _sq(T a) { return a * a; } template <class T, class X> inline T _pow(T a, X y) { T z = 1; for (int i = 1; i <= y; i++) { z *= a; } return z; } template <class T> inline T _gcd(T a, T b) { a = _abs(a); b = _abs(b); if (!b) return a; return _gcd(b, a % b); } template <class T> inline T _lcm(T a, T b) { a = _abs(a); b = _abs(b); return (a / _gcd(a, b)) * b; } template <class T> inline T _extended(T a, T b, T &x, T &y) { a = _abs(a); b = _abs(b); T g, x1, y1; if (!b) { x = 1; y = 0; g = a; return g; } g = _extended(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return g; } template <class T, class X> inline bool getbit(T a, X i) { T t = 1; return ((a & (t << i)) > 0); } template <class T, class X> inline T setbit(T a, X i) { T t = 1; return (a | (t << i)); } template <class T, class X> inline T resetbit(T a, X i) { T t = 1; return (a & (~(t << i))); } template <class T, class X> inline T togglebit(T a, X i) { T t = 1; return (a ^ (t << i)); } template <class T, class X, class Y> inline T _bigmod(T n, X m, Y mod) { unsigned long long ret = 1, a = n % mod; while (m) { if (m & 1) ret = (ret * a) % mod; m >>= 1; a = (a * a) % mod; } ret %= mod; return (T)ret; } template <class T, class Y> inline T _modinv(T n, Y mod) { return _bigmod(n, mod - 2, mod); } long long func(long long x, long long y, long long ab) { long long lft = (x); long long rgt = (y); if (lft > rgt) swap(lft, rgt); long long md = abs(lft) % (2 * ab); if (lft < 0) lft += md; else lft += (2 * ab - md); long long ret = 0; if (lft <= rgt) { ret += 1 + abs(rgt - lft) / (2 * ab); } return ret; } int main() { long long x1, x2, y1, y2, lft, rgt, md, d, ans, a, b, a1, a2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; a1 = func(x1 + y1, x2 + y2, a); a2 = func(x1 - y1, x2 - y2, b); cout << max(a1, a2) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void pp(T v) { for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) cout << *it << ' '; cout << endl; } template <class T> void pp(T v, int n) { for (int i = 0; i < (int)n; i++) cout << v[i] << ' '; cout << endl; } const int INF = 1 << 28; const double EPS = 1.0e-9; static const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; int main(void) { int a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; int add1 = x1 + y1, add2 = x2 + y2; int sub1 = x1 - y1, sub2 = x2 - y2; int add1_i = add1 / (2 * a); if (add1 < 0) add1_i--; int add2_i = add2 / (2 * a); if (add2 < 0) add2_i--; int sub1_i = sub1 / (2 * b); if (sub1 < 0) sub1_i--; int sub2_i = sub2 / (2 * b); if (sub2 < 0) sub2_i--; int dx = abs(add1_i - add2_i); int dy = abs(sub1_i - sub2_i); int ans = max(dx, dy); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int A, B, Px, Py, Qx, Qy; void process() { int t1, t2, t3, t4; t1 = (Px + Py) / (A * 2); if (Px + Py < 0) t1--; t2 = (Qx + Qy) / (A * 2); if (Qx + Qy < 0) t2--; t3 = (Px - Py) / (B * 2); if (Px - Py < 0) t3--; t4 = (Qx - Qy) / (B * 2); if (Qx - Qy < 0) t4--; cout << max(abs(t1 - t2), abs(t3 - t4)); } int main() { scanf("%d%d%d%d%d%d", &A, &B, &Px, &Py, &Qx, &Qy); process(); return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 200 + 10; long long int GCD(long long int x, long long int y) { return y == 0 ? x : GCD(y, x % y); } int main() { long long int a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; long long int gcd = GCD(2 * a, 2 * b); long long int lcm = 2 * a * 2 * b / gcd; long long int pos45tx1 = x1 - y1; long long int pos45tx2 = x2 - y2; if (pos45tx1 > pos45tx2) { long long int tmp = pos45tx1; pos45tx1 = pos45tx2; pos45tx2 = tmp; } long long int pos45num2a = pos45tx2 / (2 * a) - (pos45tx2 % (2 * a) != 0 && pos45tx2 <= 0) - (pos45tx1 / (2 * a) + (pos45tx1 % (2 * a) != 0 && pos45tx1 >= 0)) + 1; long long int pos45num2b = pos45tx2 / (2 * b) - (pos45tx2 % (2 * b) != 0 && pos45tx2 <= 0) - (pos45tx1 / (2 * b) + (pos45tx1 % (2 * b) != 0 && pos45tx1 >= 0)) + 1; long long int pos45num2a2b = pos45tx2 / (lcm) - (pos45tx2 % (lcm) != 0 && pos45tx2 <= 0) - (pos45tx1 / (lcm) + (pos45tx1 % (lcm) != 0 && pos45tx1 >= 0)) + 1; long long int manhattan1 = pos45num2b; long long int neg45tx1 = x1 + y1; long long int neg45tx2 = x2 + y2; if (neg45tx1 > neg45tx2) { long long int tmp = neg45tx1; neg45tx1 = neg45tx2; neg45tx2 = tmp; } long long int neg45num2a = neg45tx2 / (2 * a) - (neg45tx2 % (2 * a) != 0 && neg45tx2 <= 0) - (neg45tx1 / (2 * a) + (neg45tx1 % (2 * a) != 0 && neg45tx1 >= 0)) + 1; long long int neg45num2b = neg45tx2 / (2 * b) - (neg45tx2 % (2 * b) != 0 && neg45tx2 <= 0) - (neg45tx1 / (2 * b) + (neg45tx1 % (2 * b) != 0 && neg45tx1 >= 0)) + 1; long long int neg45num2a2b = neg45tx2 / (lcm) - (neg45tx2 % (lcm) != 0 && neg45tx2 <= 0) - (neg45tx1 / (lcm) + (neg45tx1 % (lcm) != 0 && neg45tx1 >= 0)) + 1; long long int manhattan2 = neg45num2a; long long int ans = manhattan1 > manhattan2 ? manhattan1 : manhattan2; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, x, x2, y, y2, res1, res2, res, xx, xx2, yy, yy2, k, k2, m, l, r; int i; int main() { cin >> a >> b >> x >> y >> x2 >> y2; l = -1000000000; r = 1000000000; while (l + 1 < r) { m = (l + r) / 2; if (x + 2 * b * m > y) r = m; else if (x + 2 * b * m < y) l = m; } k = r; l = -1000000000; r = 1000000000; while (l + 1 < r) { m = (l + r) / 2; if (x2 + 2 * b * m > y2) r = m; else if (x2 + 2 * b * m < y2) l = m; } k2 = r; if (k2 > k) res1 = k2 - k; else res1 = k - k2; l = -1000000000; r = 1000000000; while (l + 1 < r) { m = (l + r) / 2; if (2 * a * m - y > x) r = m; else if (2 * a * m - y < x) l = m; } k = r; l = -1000000000; r = 1000000000; while (l + 1 < r) { m = (l + r) / 2; if (2 * a * m - y2 > x2) r = m; else if (2 * a * m - y2 < x2) l = m; } k2 = r; if (k2 > k) res2 = k2 - k; else res2 = k - k2; res = max(res1, res2); cout << res; return 0; }
#include <bits/stdc++.h> int abs(int a) { return a > 0 ? a : -a; } int ceili(int a, int b) { int c; if (a > 0) { c = a / b; } else { c = a / b; c--; } return c; } int max(int a, int b) { return a > b ? a : b; } int main() { int a, b, x1, y1, x2, y2; scanf("%d%d%d%d%d%d", &a, &b, &x1, &y1, &x2, &y2); int xx = ceili(x1 + y1, 2 * a), yy = ceili(x1 - y1, 2 * b); int xxx = ceili(x2 + y2, 2 * a), yyy = ceili(x2 - y2, 2 * b); printf("%d\n", max(abs(xx - xxx), abs(yy - yyy))); return 0; }
#include <bits/stdc++.h> using namespace std; int A, B, X0, Y0, X1, Y1; int floor(int a, int b) { if (a > 0) return a / b; else return -(-a) / b - 1; } int main() { scanf("%d %d %d %d %d %d", &A, &B, &X0, &Y0, &X1, &Y1); int k0, h0, k1, h1; k0 = floor(Y0 + X0, 2 * A); h0 = floor(Y0 - X0, 2 * B); k1 = floor(Y1 + X1, 2 * A); h1 = floor(Y1 - X1, 2 * B); cout << max(abs(k1 - k0), abs(h1 - h0)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, x2, y1, y2; scanf("%d%d %d%d %d%d", &a, &b, &x1, &y1, &x2, &y2); int i, j, ans; int X3 = x1 + y1; int X4 = x2 + y2; i = (int)(X3) / (2 * a); j = (int)(X4) / (2 * a); if (X3 < 0) i--; if (X4 < 0) j--; ans = i - j; if (ans < 0) ans = ans * (-1); int y3 = x1 - y1; int y4 = x2 - y2; i = (int)(y3) / (2 * b); j = (int)(y4) / (2 * b); if (y3 < 0) i--; if (y4 < 0) j--; i = i - j; if (i < 0) i = -i; if (ans < i) ans = i; printf("%d", ans); }
#include <bits/stdc++.h> using namespace std; long long gcd(long long aa, long long bb) { if (aa == 0) return bb; return (bb % aa, aa); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long a, b, x1, x2, y1, y2, x11, x12, x21, x22; cin >> a >> b >> x1 >> y1 >> x2 >> y2; x11 = x1 + y1; x12 = y1 - x1; x21 = x2 + y2; x22 = y2 - x2; long long ans1, ans2; if (x11 * x21 >= 0) { if (x11 < 0) { x11 *= -1; x21 *= -1; } x11 /= 2 * a; x21 /= 2 * a; ans1 = abs(x21 - x11); } else { if (x11 < 0) x11 *= -1; if (x21 < 0) x21 *= -1; x11 /= 2 * a; x21 /= 2 * a; ans1 = abs(x11 + x21) + 1; } if (x12 * x22 > 0) { if (x12 < 0) { x12 *= -1; x22 *= -1; } x12 /= 2 * b; x22 /= 2 * b; ans2 = abs(x12 - x22); } else { if (x12 < 0) x12 *= -1; if (x22 < 0) x22 *= -1; x12 /= 2 * b; x22 /= 2 * b; ans2 = abs(x12 + x22) + 1; } long long ans = max(ans1, ans2); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { long long a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; long long Ans1 = 1, Ans2 = 1; long long x = x1 + y1; long long y = x2 + y2; if (x > y) swap(x, y); a *= 2; if (x <= 0) Ans1 -= x / a; else Ans1 -= x / a + 1; if (y < 0) Ans1 += y / a - 1; else Ans1 += y / a; b *= 2; x = x1 - y1; y = x2 - y2; if (x > y) swap(x, y); if (x <= 0) Ans2 -= x / b; else Ans2 -= x / b + 1; if (y < 0) Ans2 += y / b - 1; else Ans2 += y / b; int Ans = max(Ans1, Ans2); if (Ans1 == Ans2 && Ans1 == 0) Ans = 0; cout << Ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, x2, y1, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; int t1, t2; if (x1 >= y1) t1 = (x1 - y1) / (2 * b); else t1 = -(1 + (y1 - x1) / (2 * b)); if (x2 >= y2) t2 = (x2 - y2) / (2 * b); else t2 = -(1 + (y2 - x2) / (2 * b)); int ans1 = abs(t1 - t2); if (x1 + y1 >= 0) t1 = (x1 + y1) / (2 * a); else t1 = -(1 + (-y1 - x1) / (2 * a)); if (x2 + y2 >= 0) t2 = (x2 + y2) / (2 * a); else t2 = -(1 + (-y2 - x2) / (2 * a)); int ans2 = abs(t1 - t2); cout << max(ans1, ans2) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, x2, y1, y2, d1, d2, ans, s1, s2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; d1 = (y1 + x1) / (2 * a); d2 = (y2 + x2) / (2 * a); s1 = y1 + x1 > 0 ? 1 : -1; s2 = y2 + x2 > 0 ? 1 : -1; ans = abs(d2 - d1) + (s2 * s1 > 0 ? 0 : 1); d1 = (y1 - x1) / (2 * b); d2 = (y2 - x2) / (2 * b); s1 = y1 - x1 > 0 ? 1 : -1; s2 = y2 - x2 > 0 ? 1 : -1; ans = max(ans, abs(d1 - d2) + (s2 * s1 > 0 ? 0 : 1)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int main() { long long a, b, x1, x2, y1, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; long long s1 = x1 + y1, s2 = x2 + y2, d1 = x1 - y1, d2 = x2 - y2; if (s1 > s2) swap(s1, s2); if (d1 > d2) swap(d1, d2); long long k1 = s1 / (2 * a); --k1; while (2 * a * k1 < s1) ++k1; long long k2 = s2 / (2 * a); k2 += 2; while (2 * a * k2 > s2) --k2; long long ans1; if (k2 >= k1) ans1 = k2 - k1 + 1; else ans1 = 0; long long p1 = d1 / (2 * b); --p1; while (2 * b * p1 < d1) ++p1; long long p2 = d2 / (2 * b); p2 += 2; while (2 * b * p2 > d2) --p2; long long ans2; if (p2 >= p1) ans2 = p2 - p1 + 1; else ans2 = 0; long long ans3 = 0; long long d = b / gcd(a, b); if (k2 >= k1) { long long f1 = k1 / d; --f1; while (d * f1 < k1) ++f1; long long f2 = k2 / d; f2 += 2; while (d * f2 > k2) --f2; if (f2 >= f1) ans3 = f2 - f1 + 1; } cout << max(ans1, ans2) << endl; int x; cin >> x; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; long long Sum1 = x1 + y1; long long Sum2 = x2 + y2; long long Min1 = x1 - y1; long long Min2 = x2 - y2; if (Sum1 < 0 || Sum2 < 0) { long long minSum = min(Sum1, Sum2); minSum *= -1; Sum1 += (minSum + 2 * a) / (2 * a) * (2 * a); Sum2 += (minSum + 2 * a) / (2 * a) * (2 * a); } if (Min1 < 0 || Min2 < 0) { long long minMin = min(Min1, Min2); minMin *= -1; Min1 += (minMin + 2 * b) / (2 * b) * (2 * b); Min2 += (minMin + 2 * b) / (2 * b) * (2 * b); } long long X1 = Sum1 / (2 * a); long long X2 = Sum2 / (2 * a); long long Y1 = Min1 / (2 * b); long long Y2 = Min2 / (2 * b); long long ans = max(abs(X1 - X2), abs(Y1 - Y2)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int OO = 1e9; const double EPS = 1e-9; template <class T> void _db(const char* dbStr, T e) { cout << dbStr << " = " << e << endl; } template <class T, class... L> void _db(const char* dbStr, T e, L... r) { while (*dbStr != ',') cout << *dbStr++; cout << " = " << e << ','; _db(dbStr + 1, r...); } template <class S, class T> ostream& operator<<(ostream& o, const map<S, T>& v) { o << "["; int i = 0; for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{" << pr.first << " : " << pr.second << "}"; return o << "]"; } template <template <class, class...> class S, class T, class... L> ostream& operator<<(ostream& o, const S<T, L...>& v) { o << "["; int i = 0; for (const auto& e : v) o << (!i++ ? "" : ", ") << e; return o << "]"; } template <class S, class T> ostream& operator<<(ostream& o, const pair<S, T>& pr) { return o << "(" << pr.first << ", " << pr.second << ")"; } ostream& operator<<(ostream& o, const string& s) { for (const char& c : s) o << c; return o; } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = VV<V<T>>; using ll = long long; using pii = pair<int, int>; using vi = V<int>; using vii = V<pii>; using vvi = VV<int>; using mii = map<int, int>; using umii = unordered_map<int, int>; using si = set<int>; using usi = unordered_set<int>; int main() { ios::sync_with_stdio(false); cout.precision(10); cin.tie(0); int a, b, x_1, y_1, x_2, y_2; cin >> a >> b >> x_1 >> y_1 >> x_2 >> y_2; ll ans = 0; { ll u = x_1 + y_1, v = x_2 + y_2; if (u > v) swap(u, v); while (u <= 0) u += 1000000000LL * a, v += 1000000000LL * a; true; ans = v / (2 * a) - u / (2 * a); } { ll u = -x_1 + y_1, v = -x_2 + y_2; if (u > v) swap(u, v); while (u <= 0) u += 1000000000LL * b, v += 1000000000LL * b; true; ans = max(ans, v / (2 * b) - u / (2 * b)); } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, y1, x2, y2; scanf("%i %i %i %i %i %i", &a, &b, &x1, &y1, &x2, &y2); a *= 2; b *= 2; long long cnt1 = 0, cnt2 = 0; int d1 = x1 + y1; int d2 = x2 + y2; if (d1 > d2) swap(d1, d2); int prva = d1 % a; if (prva != 0) { int dd = d1 / a; if (d1 < 0) dd--; dd++; int sl = dd * a; if (sl > d2) { d1 = d2; } else { cnt1++; d1 = sl; } } else cnt1++; long long dif = (long long)d2 - d1; cnt1 += (long long)dif / a; d1 = x1 - y1; d2 = x2 - y2; if (d1 > d2) swap(d1, d2); prva = d1 % b; if (prva != 0) { int dd = d1 / b; if (d1 < 0) dd--; dd++; int sl = dd * b; if (sl > d2) { d1 = d2; } else { cnt2++; d1 = sl; } } else cnt2++; dif = (long long)d2 - d1; cnt2 += (long long)dif / b; printf("%lld\n", max(cnt1, cnt2)); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n, m; cin >> n >> m; vector<ll> ar(n); vector<ll> br(n); map<ll, ll> a, b; for (auto &x : ar) { cin >> x; a[x]++; } for (auto &x : br) { cin >> x; b[x]++; } ll res = 1.01e18; for (int i = 0; i < n; ++i) { ll x = (br[i] - ar[0] + m) % m; bool can = true; for (auto ex : a) { can &= (ex.second == b[(ex.first + x) % m]); } if (can) res = min(res, x); } cout << res << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; long long int A[2001], B[2001]; vector<long long int> X; int main() { long long 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]; } for (int i = 0; i < n; i++) { long long int x = (B[i] - (A[0] % m) + m) % m; X.push_back(x); } sort(B, B + n); sort(X.begin(), X.end()); for (int i = 0; i < X.size(); i++) { int fl = 0; vector<long long int> C; for (int j = 0; j < n; j++) { C.push_back((A[j] + X[i]) % m); } sort(C.begin(), C.end()); for (int j = 0; j < n; j++) { if (C[j] != B[j]) { fl = 1; } } if (fl != 1) { cout << X[i] << endl; return 0; } } 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[n - 1] - 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; bool check(long long *a, long long *b, long long n) { for (int i = 0; i < n; i++) if (a[i] != b[i]) return false; return true; } int main() { long long n, m; cin >> n >> m; 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]; } long long ans = INT_MAX; sort(a, a + n); sort(b, b + n); long long x = 0; for (int i = 0; i < n; i++) { x = b[0] - a[i]; if (x < 0) x += m; long long c[n]; for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m; sort(c, c + n); if (check(c, b, n)) ans = min(ans, x); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n, m; 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]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector<int> bm; vector<int> bf; vector<int> am; vector<int> af; int p = -1; int c = 0; for (auto x : b) { if (x == p) c++; else { if (p != -1) { bm.push_back(p); bf.push_back(c); } p = x; c = 1; } } bm.push_back(p); bf.push_back(c); p = -1; c = 0; for (auto x : a) { if (x == p) c++; else { if (p != -1) { am.push_back(p); af.push_back(c); } p = x; c = 1; } } am.push_back(p); af.push_back(c); int l = bm.size(); int ans = -1; for (int i = 0; i < l; i++) { int s = 1; int k = (bm[i] - am[0] + m) % m; for (int j = 0; j < l; j++) { if ((am[j] + k) % m != bm[(i + j) % l] || af[j] != bf[(i + j) % l]) { s = 0; break; } } if (s == 1) { ans = k; break; } } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; void shift(vector<pair<long long, long long>> &v) { pair<long long, long long> st = v[v.size() - 1]; for (int i = v.size() - 1; i > 0; i--) v[i] = v[i - 1]; v[0] = st; } bool check(vector<long long> &v1, vector<long long> &v2, long long now, long long m) { vector<long long> vv1(v1.size()); for (int i = 0; i < vv1.size(); i++) { vv1[i] = (v1[i] + now) % m; } sort(vv1.begin(), vv1.end()); for (int i = 0; i < v2.size(); i++) { if (vv1[i] != v2[i]) return false; } return true; } int main() { 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 k = 1, st = a[0]; vector<pair<long long, long long>> v1, v2; for (int i = 1; i < n; i++) { if (a[i - 1] != a[i]) { v1.push_back(make_pair(k, a[i - 1])); k = 1; } else k++; } v1.push_back(make_pair(k, a[n - 1])); k = 1; for (int i = 1; i < n; i++) { if (b[i - 1] != b[i]) { v2.push_back(make_pair(k, b[i - 1])); k = 1; } else k++; } v2.push_back(make_pair(k, b[n - 1])); k = 1; sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); int time = 0; long long ans = LLONG_MAX; int sta = v2[0].first; for (int j = 0; j < v1.size(); j++) { if (v2[j].first != sta) break; if (v1[0].second <= v2[j].second) { long long now = v2[j].second - v1[0].second; if (check(a, b, now, m)) ans = min(ans, now); } else { long long now = m - v1[0].second + v2[j].second; if (check(a, b, now, m)) ans = min(ans, now); } } cout << ans; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { a = abs(a), b = abs(b); if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; vector<vector<int> > adj; set<int> st; int n, m, a[300000 + 5], b[300000 + 5], c[300000 + 5]; map<int, int> m1, m2; vector<pair<int, int> > v1, v2; int main(void) { ios::sync_with_stdio(false), cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> a[i]; m1[a[i]] += 1; } for (int i = 0; i < n; ++i) { cin >> b[i]; m2[b[i]] += 1; } for (auto x : m1) v1.emplace_back(x.second, x.first); for (auto x : m2) v2.emplace_back(x.second, x.first); sort(v1.rbegin(), v1.rend()); sort(v2.rbegin(), v2.rend()); sort(b, b + n); int ans = (int)(1e9 + 5), x; for (int i = 0; i < (int)(v1).size(); ++i) { if (v1[0].first == v2[i].first) { x = (v2[i].second - v1[0].second + m) % m; for (int j = 0; j < n; ++j) c[j] = (a[j] + x) % m; sort(c, c + n); bool flg = true; for (int j = 0; j < n; ++j) if (c[j] != b[j]) { flg = false; } if (flg) ans = min(ans, x); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long INF = 1 << 28; const double pi = acos(-1.0); int fx[] = {1, -1, 0, 0}; int fy[] = {0, 0, 1, -1}; int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1}; int knight[8][2] = {1, 2, 1, -2, 2, 1, 2, -1, -1, 2, -1, -2, -2, 1, -2, -1}; const long double EPS = 1e-7; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } bool cmp(int a, int b) { return a < b; } int on(int mask, int st) { return mask | (1 << st); } int off(int mask, int st) { return mask & (~(1 << st)); } const int mx = 500010; long long mod = 1000000007; long long arr[mx]; long long brr[mx]; vector<long long> sizeA[mx], sizeB[mx]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%lld", &arr[i]); } for (int i = 0; i < n; i++) { scanf("%lld", &brr[i]); } sort(arr, arr + n); sort(brr, brr + n); int last = arr[0]; int countt = 1; for (int i = 1; i < n; i++) { if (last != arr[i]) { sizeA[countt].push_back(last); countt = 1; last = arr[i]; } else countt++; } sizeA[countt].push_back(last); countt = 1; last = brr[0]; for (int i = 1; i < n; i++) { if (last != brr[i]) { sizeB[countt].push_back(last); countt = 1; last = brr[i]; } else countt++; } sizeB[countt].push_back(last); int l = -1; for (int i = 1; i <= n; i++) { if (sizeA[i].size()) { l = i; break; } } long long x = sizeA[l][0]; vector<long long> v; long long ans = INT_MAX; for (int i = 0; i < sizeB[l].size(); i++) { long long y = sizeB[l][i]; long long add = (y - x + m) % m; v.clear(); for (int j = 0; j < n; j++) { v.push_back((arr[j] + add) % m); } sort(v.begin(), v.end()); int flag = 1; for (int j = 0; j < n; j++) { if (brr[j] != v[j]) { flag = 0; break; } } if (flag) { ans = min(add, ans); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; long long powermodm(long long x, long long n, long long M) { long long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long power(long long _a, long long _b) { long long _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; long long a[n + 1], b[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } map<long long, long long> mp2; for (int i = 1; i <= n; i++) { cin >> b[i]; mp2[b[i]]++; } set<long long> s; for (int i = 1; i <= n; i++) { long long x = a[i]; long long y; if (x == b[1]) { y = 0; } else if (x > b[1]) { y = m - x + b[1]; } else { y = b[1] - x; } long long c[n + 1]; bool f = 0; map<long long, long long> mp1; for (int j = 1; j <= n; j++) { c[j] = (a[j] + y) % m; mp1[c[j]]++; } for (auto it = mp2.begin(); it != mp2.end(); it++) { long long x1 = it->first; if (mp2[x1] == mp1[x1]) { } else { f = 1; break; } } if (f == 0) { s.insert(y); } } cout << *s.begin() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; inline void INP() {} const long long inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; long long n, m, k, q; string s; vector<long long> adj[(long long)2e5 + 5]; long long vis[(long long)2e5 + 5], b[(long long)2e5 + 5], a[(long long)2e5 + 5]; void go() { cin >> n >> m; 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 = inf; for (long long i = 0; i < n; i++) { long long xx = (b[n - 1] - (a[i] % m)); xx = (xx + m) % m; vector<long long> temp; for (long long j = 0; j < n; j++) temp.push_back((a[j] + xx) % m); long long ok = 0; sort(begin(temp), end(temp)); for (long long j = 0; j < n; j++) { if (temp[j] != b[j]) { ok = 1; break; } } if (ok) continue; ans = min(ans, xx); } cout << ans << '\n'; } int32_t main() { INP(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t = 1; while (t--) go(); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); 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 = LONG_LONG_MAX, x = 0; for (int i = 0; i < n; i++) { x = (b[i] - a[0]); if (x < 0) x += m; int j; for (j = 1; j < n; j++) if ((a[j] + x - b[(i + j) % n] + m) % m != 0) break; if (j == n) ans = min(ans, x); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long int t, test, temp; long long int n, m, k, kount; long long int a, b, c, ans, ara[2020], bra[2020], cra[2020]; long long int x, y, z; int main() { while (scanf("%lld %lld", &n, &m) == 2) { for (long long int i = 0; i < n; i++) scanf("%lld", &ara[i]); for (long long int i = 0; i < n; i++) scanf("%lld", &bra[i]); sort(bra, bra + n); ans = LONG_LONG_MAX; for (long long int i = 0; i < n; i++) { x = bra[i] - ara[0]; if (x < 0) x += m; else x %= m; for (long long int j = 0; j < n; j++) cra[j] = (ara[j] + x) % m; sort(cra, cra + n); bool ok = true; for (long long int j = 0; j < n; j++) if (cra[j] != bra[j]) ok = false; if (ok) ans = min(ans, x); } printf("%lld\n", ans); } }
#include <bits/stdc++.h> using namespace std; signed 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(a.begin(), a.end()); sort(b.begin(), b.end()); long long res = 1e9 + 2; for (long long i = 0; i < n; i++) { long long x = a[0] <= b[i] ? b[i] - a[0] : m - (a[0] - b[i]); bool ok = true; for (long long j = 1; j < n; j++) { long long next = (i + j) % n; if (((a[j] + x) % m) != b[next]) { ok = false; break; } } if (ok) { res = min(res, x); } } cout << res << endl; return 0; }
#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 = 2e10; for (int i = 0; i < n; i++) { int mn = 2e9, mx = 0; for (int j = 0; j < n; j++) { int x = (b[(j + i) % n] - a[j] + m) % m; mn = min(mn, x); mx = max(mx, x); } if (mn == mx) { ans = min(ans, mn); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, a[2005], b[2005], c[2005], m; vector<long long> x; int main() { cin >> n >> m; for (long i = long(1); i <= long(n); i++) cin >> a[i]; for (long i = long(1); i <= long(n); i++) cin >> b[i]; sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n); a[0] = -1; for (long i = long(1); i <= long(n); i++) if (a[i] != a[i - 1]) x.push_back((b[1] - a[i] + m) % m); sort(x.begin(), x.end()); for (long i = long(0); i <= long(x.size()); i++) { for (long j = long(1); j <= long(n); j++) c[j] = (a[j] + x[i] + m) % m; sort(c + 1, c + 1 + n); for (long j = long(1); j <= long(n); j++) if (c[j] != b[j]) { goto en; } cout << x[i]; return 0; en: continue; } return 0; }
#include <bits/stdc++.h> using namespace std; long long int po(long long int x, long long int n) { if (n == 0) return 1; else if (n % 2 == 0) return po((x * x) % 1000000007, n / 2); else return (x * po((x * x) % 1000000007, (n - 1) / 2)) % 1000000007; } long long int GCD(long long int A, long long int B) { if (B == 0) return A; else return GCD(B, A % B); } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int n, m; cin >> n >> m; long long int a[n]; vector<long long int> 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()); long long int an = -1; for (int i = 0; i < n; ++i) { long long int d = (-a[0] + b[i] + m) % m; vector<long long int> aa; for (int j = 0; j < n; ++j) aa.push_back((a[j] + d) % m); sort(aa.begin(), aa.end()); if (aa == b) { if (an == -1) an = d; else an = min(an, d); } } cout << an; }
#include <bits/stdc++.h> using namespace std; int manga[30000], mangb[3000], manga1[5000]; void Sapxep(int mang1[], int l1, int r1) { if (l1 <= r1) { int mid = mang1[(l1 + r1) / 2]; int i1 = l1; int j1 = r1; while (i1 <= j1) { while (mang1[i1] < mid) i1++; while (mang1[j1] > mid) j1--; if (i1 <= j1) { int tam = mang1[i1]; mang1[i1] = mang1[j1]; mang1[j1] = tam; i1++; j1--; } } if (l1 < j1) Sapxep(mang1, l1, j1); if (r1 > i1) Sapxep(mang1, i1, r1); } } void Duplicate(int mang21[], int mang22[], int n2) { int i2, tam, dem; tam = 1; for (dem = 1; dem <= 2; dem++) for (i2 = 1; i2 <= n2; i2++) { mang22[tam] = mang21[i2]; tam++; } } int Hieu(int n31, int n32, int n33) { if ((n31 - n32) < 0) { return ((n31 + n33) - n32); } return (n31 - n32); } int main() { int i, n, m, x, kq, min; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> manga[i]; } for (i = 1; i <= n; i++) { cin >> mangb[i]; } int tam; for (i = 1; i <= n; i++) { tam = manga[i] % m; manga[i] = tam; } Sapxep(manga, 1, n); cout << endl; Sapxep(mangb, 1, n); Duplicate(manga, manga1, n); int kt = 0; min = 0; for (i = 1; i <= n; i++) { kq = 1; int t1 = Hieu(mangb[1], manga1[i], m); for (int it = 1; it < n; it++) { if (Hieu(mangb[1 + it], manga1[i + it], m) == t1) kq = kq * 1; else kq = kq * 0; } if (kq == 1) { if (kt == 0) { min = t1; kt = 1; } else if (t1 < min) min = t1; } } cout << endl; cout << min; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } map<int, int> cntb; for (int i = 0; i < n; i++) { cin >> b[i]; cntb[b[i]] += 1; } int ans = INT_MAX; for (int i = 0; i < n; i++) { int s; if (b[i] >= a[0]) s = b[i] - a[0]; else s = m - a[0] + b[i]; map<int, int> cnta; for (int j = 0; j < n; j++) { cnta[(a[j] + s) % m] += 1; } bool ok = true; for (map<int, int>::iterator it = cntb.begin(); it != cntb.end(); it++) { int p = it->first, c = it->second; if (cnta[p] != c) { ok = false; break; } } if (ok) { ans = min(ans, s); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9; int main() { cin.tie(0); ios_base::sync_with_stdio(0); long long n, m; cin >> n >> m; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<long long> b(n); for (int i = 0; i < n; i++) cin >> b[i]; sort(begin(b), end(b)); long long ans = inf; for (int i = 0; i < n; i++) { long long diff = ((b[i] - a[0] % m) + m) % m; vector<long long> c(n); for (int j = 0; j < n; j++) c[j] = (a[j] + diff) % m; sort(begin(c), end(c)); if (equal(begin(c), end(c), begin(b))) { ans = min(ans, diff); } } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<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]; int ans = INT_MAX; sort(a.begin(), a.end()); sort(b.begin(), b.end()); for (int i = 0; i < n; i++) { int x = (b[i] - a[0] + m) % m; 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 << endl; }
#include <bits/stdc++.h> bool FLAG = 1; using namespace std; const long long MOD = (long long)1e9 + 7, N = (long long)2e6 + 222; const long long INF = (long long)1e18; long long n, k, p[N], a[N], b[N], b1[N]; string s; void clear() { for (long long i = 1; i <= n; ++i) { p[i] = a[i]; } } long long m; void solve() { cin >> n >> m; long long mx = 0; for (long long i = 1; i <= n; ++i) { cin >> a[i]; mx = max(mx, a[i]); } for (long long i = 1; i <= n; ++i) { cin >> b[i]; } sort(b + 1, b + 1 + n); for (long long i = 1; i <= n; ++i) { b1[i] = b[i] + m; } long long mn = INF; clear(); for (long long i = 1; i <= n; ++i) { long long d; if (b1[i] - mx >= 0) { d = b1[i] - mx; for (long long j = 1; j <= n; ++j) { p[j] += d; p[j] %= m; } sort(p + 1, p + 1 + n); bool ok = 0; for (long long j = 1; j <= n; ++j) { if (p[j] != b[j]) ok = 1; } if (ok == 0) { mn = min(mn, d); } clear(); } if (b[i] - mx >= 0) { d = b[i] - mx; for (long long j = 1; j <= n; ++j) { p[j] += d; p[j] %= m; } sort(p + 1, p + 1 + n); bool ok = 0; for (long long j = 1; j <= n; ++j) { if (p[j] != b[j]) ok = 1; } if (ok == 0) { mn = min(mn, d); } clear(); } } cout << mn; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; clock_t time_p = clock(); void rtime() { time_p = clock() - time_p; cerr << "Time Taken : " << fixed << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } bool isPrime(long long n) { for (long long i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } long long factorial(long long n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } long long power(long long x, long long y) { long long res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res % mod; } long long ncr(long long n, long long r) { long long res = 1; if (r > n) return 0; if (r > n - r) r = n - r; for (long long i = 0; i < r; i++) { res *= (n - i); res /= (i + 1); } return res; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } long long max(long long a, long long b) { long long ans = a > b ? a : b; return ans; } long long min(long long a, long long b) { long long ans = a < b ? a : b; return ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t, n, m; t = 1; while (t--) { 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 flag; set<long long> s; for (long long i = 0; i < n; i++) { s.insert((b[0] - a[i] + m) % m); } long long c[n]; for (auto v : s) { long long x = v; for (long long i = 0; i < n; i++) { c[i] = (a[i] + x) % m; } sort(c, c + n); flag = 1; for (long long i = 0; i < n; i++) { if (c[i] != b[i]) { flag = 0; } } if (flag == 1) { cout << x << "\n"; break; } } } rtime(); }
#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]; long long int ans = INT_MAX; sort(b, b + n); for (long long int i = 0; i < n; i++) { long long int val = (m + b[i] - a[0]) % m; long long int c[n]; bool poss = true; for (long long int j = 0; j < n; j++) c[j] = (a[j] + val) % m; sort(c, c + n); for (long long int j = 0; j < n; j++) { if (c[j] != b[j]) poss = false; } if (poss) { ans = min(ans, val); } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long md = 1000000007) { long long res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x = (x * x) % md; y = y >> 1; } return res % md; } bool check(vector<long long> a, vector<long long> b) { for (long long i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 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 x, ans = INT_MAX; vector<long long> temp(n); for (long long i = 0; i < n; i++) { if (a[i] > b[0]) x = m - a[i] + b[0]; else x = b[0] - a[i]; for (long long j = 0; j < n; j++) temp[j] = (a[j] + x) % m; sort(temp.begin(), temp.end()); if (check(temp, b)) ans = min(ans, x); } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e3 + 3; long long n, m, i, j, a[N], b[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); 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); long long ans = 1e12; for (i = 0; i < n; i++) { long long x = (b[i] - a[0] + m) % m; for (j = 0; j < n; j++) { if ((a[j] + x) % m != b[(i + j) % n]) { break; } } if (j == n) { ans = min(ans, x); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long INF = 0x3f3f3f3f3f3f3f3f; const int inf = 0x3f3f3f3f; const int maxn = 1e6 + 10; const double eps = 1e-8; const double pi = acos(-1.0); long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long poww(long long x, long long y, long long p) { long long ans = 1; while (y) { if (y & 1) ans = ans * x % p; x = x * x % p; y >>= 1; } return ans % p; } bool isp(long long x) { if (x < 2) return 0; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) return 0; } return 1; } long long n, m; long long a[2005], b[2005]; bool check(int x, long long y) { for (int i = 1; i < n; i++) { if ((a[i] + y) % m != b[(i + x) % n]) return 0; } return 1; } void solve() { sort(a, a + n); sort(b, b + n); long long ans = INF; for (int i = 0; i < n; i++) { long long x = (b[i] - a[0] + m) % m; if (check(i, x)) { ans = min(ans, x); } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = (0); i <= (n - 1); i++) cin >> a[i]; for (int i = (0); i <= (n - 1); i++) cin >> b[i]; solve(); }
#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]); unordered_map<int, int> mp, mp1; for (int i = 0; i < n; i++) { mp[p[i]]++, mp1[q[i]]++; } unordered_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))]++; ; } } } } int ans = 1100000000; for (auto it : MAP) { if (it.second == (int)(mp).size()) { ans = min(ans, it.first); } } printf("%d\n", ans); } int main() { int tc = 1; while (tc--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int inf = 9e18; const long double pi = 2 * acos(0.0); long long int t, n, x = inf, m; vector<long long int> arr, brr, temp; void solve() { cin >> n >> m; arr.resize(n), brr.resize(n); ; for (int i = 0; i < n; i++) cin >> arr[i]; ; for (int i = 0; i < n; i++) cin >> brr[i]; sort(arr.begin(), arr.end()); sort(brr.begin(), brr.end()); ; for (int i = 0; i < n; i++) { bool valid = true; long long int curr = brr[0] - arr[i], ind = 1; if (curr < 0) curr += m; temp.clear(); ; for (int j = 0; j < n; j++) { temp.push_back((arr[j] + curr) % m); } sort(temp.begin(), temp.end()); ; for (int j = 0; j < n; j++) { if (temp[j] != brr[j]) valid = false; } if (valid) x = min(x, curr); } cout << x << "\n"; } int32_t main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<long long int> a(n); vector<long long int> 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 flag = 0; long long int base = a[0]; long long int ans = INT_MAX; long long int x; for (int j = 0; j < n; j++) { if (base > b[j]) { x = m - base + b[j]; } else { x = b[j] - base; } if (x >= ans) { continue; } flag = 1; for (int i = 0; i < n; i++) { if ((a[i] + x) % m != (b[(i + j) % n])) { flag = 0; break; } } if (flag) { ans = min(x, ans); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; void fast() { cin.tie(0); ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); } signed main() { fast(); long long n, m; cin >> n >> m; vector<long long> m1(n); for (long long i = 0; i < n; i++) { cin >> m1[i]; } vector<long long> m2(n); for (long long i = 0; i < n; i++) { cin >> m2[i]; } sort(m1.begin(), m1.end()); sort(m2.begin(), m2.end()); long long ans = INF; vector<long long> cur = m1; for (long long i = 0; i < n; i++) { long long ch; if (m1[i] > m2[0]) { ch = m - m1[i] + m2[0]; } else { ch = m2[0] - m1[i]; } for (int j = 0; j < n; j++) { cur[j] = (m1[j] + ch) % m; } sort(cur.begin(), cur.end()); bool f = true; for (int j = 0; j < n; j++) { f &= cur[j] == m2[j]; } if (f) { ans = min(ans, ch); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3 + 5; int a[MAXN], b[MAXN], c[MAXN]; int n, m; int x[MAXN]; int main() { int i, j; cin >> n >> m; for (i = (1); i <= (n); i++) { cin >> a[i]; } for (i = (1); i <= (n); i++) { cin >> b[i]; } sort(b + 1, b + 1 + n); for (i = (1); i <= (n); i++) { x[i] = ((b[1] - a[i]) % m + m) % m; } sort(x + 1, x + 1 + n); for (i = (1); i <= (n); i++) { for (j = (1); j <= (n); j++) { c[j] = (a[j] + x[i]) % m; } sort(c + 1, c + 1 + n); for (j = (1); j <= (n); j++) { if (b[j] != c[j]) break; } if (j > n) { cout << x[i] << endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n, m; cin >> n >> m; vector<int> a(n), b(n); set<int> s; 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()); sort((a).begin(), (a).end()); int val, temp = a[0]; for (int i = 0; i < n; ++i) { if (temp > b[i]) s.insert(m - (temp - b[i])); else s.insert(b[i] - temp); } std::vector<int> ans; for (auto x : s) { for (int i = 0; i < n; ++i) { ans.push_back((a[i] + x) % m); } sort((ans).begin(), (ans).end()); if (ans == b) { cout << x << '\n'; break; } ans.clear(); } 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]; int ans = 1e9; for (int i = 0; i < n; i++) { int x; if (b[i] >= a[0]) { x = b[i] - a[0]; } else { x = m + b[i] - a[0]; } map<int, int> mp; for (int j = 0; j < n; j++) { mp[b[j]]++; } for (int j = 0; j < n; j++) { mp[(a[j] + x) % m]--; } int flag = 1; for (auto x : mp) { flag = flag & (x.second == 0); } if (flag) { ans = min(ans, x); } } cout << ans << endl; } int main() { int t = 1; for (int i = 0; i < t; i++) { solve(); } }
#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(b.begin(), b.end()); int x; for (int i = 0; i < n; i++) { x = (b[i] - a[0]); if (x < 0) { x += ((a[0] - b[i] - 1) / m + 1) * m; } vector<int> t(n); for (int i = 0; i < n; i++) t[i] = (a[i] + x) % m; sort(t.begin(), t.end()); if (t == b) break; } cout << x << endl; }
#include <bits/stdc++.h> using namespace std; long long a[2010], b[2010]; map<long long, int> na, nb; class map_finder { public: map_finder(int _v) : v(_v) {} bool operator()(const std::map<long long, int>::value_type &pair) { return pair.second == this->v; } private: int v; }; int main() { int n; long long m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; na[a[i]]++; } for (int i = 1; i <= n; i++) { cin >> b[i]; nb[b[i]]++; } vector<int> ans; map<long long, int>::iterator it = na.begin(); map<long long, int>::iterator begin = nb.begin(); while (begin != nb.end()) { begin = find_if(begin, nb.end(), map_finder(it->second)); if (begin == nb.end()) break; ans.push_back((begin->first - it->first + m) % m); begin++; } sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { int flag = 0; for (map<long long, int>::iterator itt = na.begin(); itt != na.end(); itt++) { if (nb[(itt->first + ans[i] + m) % m] != itt->second) { flag = 1; break; } } if (!flag) { cout << (ans[i] + m) % m << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n, 0), b(n, 0); for (int i = 0; i < n; i++) { int aux; scanf("%d", &aux); a[i] = aux; } for (int i = 0; i < n; i++) { int aux; scanf("%d", &aux); b[i] = aux; } sort((a).begin(), (a).end()); sort((b).begin(), (b).end()); int ans = 1000000000; for (int i = 0; i < n; i++) { int x = b[0] - a[i]; x = (x + m) % m; bool deuruim = false; for (int d = 0; d < n; d++) { int j = (i + d) % n; if ((a[j] + x) % m != b[d]) { deuruim = true; break; } } if (!deuruim) { ans = min(ans, x); } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const long long MOD = 4294967296; const long long NO_OPERTATION = -1; int main() { ios::sync_with_stdio(0); cin.tie(0); long long n; long long m; cin >> n >> m; vector<long long> v(n); vector<long long> v2(n); for (auto& qw : (v)) cin >> qw; ; for (auto& qw : (v2)) cin >> qw; ; sort(v2.begin(), v2.end()); long long resp = LINF; for (int i = 0; i < n; i++) { if (v[0] <= v2[i]) { long long auxiliar = v2[i] - v[0]; vector<long long> v3 = v; for (int j = 0; j < n; j++) { v3[j] += auxiliar; v3[j] %= m; } sort(v3.begin(), v3.end()); bool confere = true; for (int j = 0; j < n; j++) { if (v3[j] != v2[j]) { confere = false; } } if (confere == true) { resp = min(resp, auxiliar); } } else { long long auxiliar = m + v2[i] - v[0]; vector<long long> v3 = v; for (int j = 0; j < n; j++) { v3[j] += auxiliar; v3[j] %= m; } sort(v3.begin(), v3.end()); bool confere = true; for (int j = 0; j < n; j++) { if (v3[j] != v2[j]) { confere = false; } } if (confere == true) { resp = min(resp, auxiliar); } } } cout << resp << "\n"; ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; long long m; cin >> n >> m; long long 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]; for (int i = 0; i < n; i++) { d[i] = a[i]; c[i] = (b[0] - a[i] + m) % m; } sort(a, a + n); sort(b, b + n); sort(c, c + n); int flag = 0, value = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) d[j] = a[j]; for (int j = 0; j < n; j++) { d[j] = (d[j] + c[i]) % m; } sort(d, d + n); for (int j = 0; j < n; j++) { if (b[j] != d[j]) break; if (j == n - 1) { flag = 1; value = c[i]; } } if (flag == 1) break; } cout << value << "\n"; }
#include <bits/stdc++.h> using namespace std; int const N = 5e5 + 5; int const mod = 1e9 + 7; long double min(long double x, long double y) { if (x < y) return x; else return y; } long double max(long double x, long double y) { if (x > y) return x; else return y; } long long gcd(long long x, long long y) { if (x == 0) return y; else return gcd(y % x, x); } bool compare(const pair<int, int> &a, const pair<int, int> &b) { if (a.first < b.first) return true; else if (a.first == b.first) return (a.second > b.second); else return false; } bool sortbysec(const int &a, const int &b) { if (a > b) return true; else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long tt = 1; while (tt--) { 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()); if (a == b) cout << 0 << endl; else { vector<long long> temp; temp = a; long long ans = 1e9; for (int i = 0; i < n; i++) { temp = a; long long diff = (b[i] - a[0] + m) % m; for (int j = 0; j < n; j++) { temp[j] = (temp[j] + diff) % m; } sort(temp.begin(), temp.end()); if (temp == b) { ans = min(diff, ans); } } cout << ans << endl; } } }
#include <bits/stdc++.h> using namespace std; const long long MOD2 = 998244353; const long long MOD = 1e9 + 7; const long long MAX = 1e5 + 7; const long long inf = 1e18L + 5; const double pi = 3.14159265358979323846; long long fpow(long long n, long long m) { long long res = 1; while (m > 0) { if (m & 1) { res *= n; } m /= 2; n *= n; } return res; } const long long nax = 1e5 + 5; void solve() { 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 x, ans = 1e10; for (long long i = 0; i < n; i++) { if (a[0] <= b[i]) x = b[i] - a[0]; else x = m - a[0] + b[i]; for (long long j = 0; j < n; j++) { if ((a[j] + x) % m != b[(j + i) % n]) { break; } if (j == n - 1) { ans = min(ans, x); } } } if (ans == 1e10) cout << 0; else cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, k, x; vector<int> va, vb; map<int, int> ma, mb, tmmp; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x; va.push_back(x); ma[x]++; } for (int i = 0; i < n; i++) { cin >> x; vb.push_back(x); mb[x]++; } if (ma == mb) { cout << 0 << endl; exit(0); } int ans = INT_MAX; for (int i = 0; i < n; i++) { tmmp.clear(); int cnt; if (vb[i] >= va[0]) cnt = vb[i] - va[0]; else cnt = k - va[0] + vb[i]; for (int j = 0; j < n; j++) { int lol = va[j] + cnt; lol %= k; tmmp[lol]++; } if (tmmp == mb) ans = min(ans, cnt); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; long long a[40005], b[20005]; signed main() { long long n, m; 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 + n + 1); sort(b + 1, b + n + 1); for (long long i = n + 1; i <= 2 * n; i++) { a[i] = a[i - n]; } long long ans = 1e18; for (long long i = 1; i <= n; i++) { set<long long> v; for (long long j = i, k = 1; j <= i + n - 1; j++, k++) { v.insert((b[k] - a[j] + m) % m); } if (v.size() > 1) continue; if (v.size() == 1) { ans = min(ans, *v.begin()); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; long long int m; cin >> n >> m; vector<long long int> a; for (int i = 0; i < n; i++) { a.push_back(0); cin >> a[i]; } vector<long long int> b; for (int i = 0; i < n; i++) { b.push_back(0); cin >> b[i]; } vector<vector<long long int> > athing; vector<vector<long long int> > bthing; vector<vector<long long int> > things; sort(a.begin(), a.end()); sort(b.begin(), b.end()); for (int i = 0; i < n; i++) { athing.push_back(vector<long long int>()); bthing.push_back(vector<long long int>()); } int p = 0; int x = 0; while (p < n) { x = 1; while (p + x < n && a[p] == a[p + x]) { x++; } athing[x - 1].push_back(a[p]); p += x; } p = 0; x = 0; while (p < n) { x = 1; while (p + x < n && b[p] == b[p + x]) { x++; } bthing[x - 1].push_back(b[p]); p += x; } for (int i = 0; i < n; i++) { if (athing[i].size() != 0 && bthing[i].size() != 0) { if (athing[i].size() == 1) { things.push_back(vector<long long int>()); things.back().push_back((m + bthing[i][0] - athing[i][0]) % m); } else { things.push_back(vector<long long int>()); long long int best = m; long long int thing = (m + bthing[i][0] - athing[i][0]) % m; bool good = true; for (int j = 0; j < athing[i].size(); j++) { athing[i].push_back(athing[i][0]); athing[i].erase(athing[i].begin()); thing = (m + bthing[i][0] - athing[i][0]) % m; good = true; for (int k = 0; k < athing[i].size(); k++) { if ((athing[i][k] + thing) % m != bthing[i][k]) { good = false; break; } } if (good) { best = min(best, thing); things.back().push_back(thing); } } } } } map<long long int, long long int> mp; for (int i = 0; i < things.size(); i++) { for (int j = 0; j < things[i].size(); j++) { mp[things[i][j]]++; } } long long int small = m; for (auto x : mp) { if (x.second == things.size()) { small = min(small, x.first); } } cout << small << endl; cin.sync(); cin.get(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<int, int>; using PIL = pair<int, ll>; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& first : v) { if (!first) { res += ", "; } first = false; res += to_string(first); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } int bs_biggest(int l, int r, function<bool(int)> check) { assert(l <= r); while (l < r) { int mid = (l + r + 1) / 2; if (check(mid)) l = mid; else r = mid - 1; } return l; } int bs_smallest(int l, int r, function<bool(int)> check) { assert(l <= r); while (l < r) { int mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid + 1; } return l; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } void my_main() { int n, m; cin >> n >> m; auto a = vector<int>(n + 1); auto b = vector<int>(n + 1); for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) cin >> a[i]; for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) cin >> b[i]; sort(a.begin() + 1, a.end()); sort(b.begin() + 1, b.end()); auto check = [&]() -> int { set<int> S; for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) S.insert(b[i] - a[i]); if ((int)S.size() == 1) { if (*S.begin() >= 0) return *S.begin(); return m + *S.begin(); } else { return -1; } }; ll ans = LLONG_MAX; ll cur = 0; for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) { int v = check(); if (v != -1) ans = min(ans, cur + v); int add = m - a[n]; cur += add; for (int j = (1), j_end_ = (n + 1); j < j_end_; ++j) a[j] = (a[j] + add) % m; sort(a.begin() + 1, a.end()); } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); my_main(); return 0; }
#include <bits/stdc++.h> using namespace std; bool solve(long long int n, long long int start, vector<long long int> &a, vector<long long int> &b, long long int x, long long int m) { bool flag = true; for (long long int d = 0; d < n; d++) { long long int pos = (start + d) % n; long long int diff = b[d] - a[pos]; if (diff < 0) { diff += m; if (diff != x) { flag = false; break; } } else { long long int diff2 = m - diff; if (diff != x && diff2 != x) { flag = false; break; } } } return flag; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int m; cin >> m; vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; ; vector<long long int> b(n); 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 start = 0; long long int x_min = m + 1; while (start < n) { if (a[start] > b[0]) { long long int x = m - (a[start] - b[0]); bool flag = solve(n, start, a, b, x, m); if (flag) x_min = min(x_min, x); } else { long long int x = b[0] - a[start]; long long int y = m - x; bool flag = solve(n, start, a, b, x, m); if (flag) x_min = min(x_min, x); flag = solve(n, start, a, b, y, m); if (flag) x_min = min(x_min, y); } start++; } cout << x_min << '\n'; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long MAXN = 5e5 + 1, INF = 1e9 + 100; mt19937 rnd(0); signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; unordered_map<int, int> a, b; a.rehash(8000); b.rehash(8000); cin >> n >> m; for (int i = 0; i < n; ++i) { int k; cin >> k; ++a[k]; } for (int i = 0; i < n; ++i) { int k; cin >> k; ++b[k]; } int ans = INF; int k = a.begin()->first; for (auto i : b) { int x = (m + i.first - k) % m; int first = 1; for (auto j : a) { first &= (b[(j.first + x) % m] == j.second); if (!first) { break; } } if (first) { ans = min(ans, x); } } cout << (ans == INF ? -1 : ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int arr1[a]; int arr11[a]; int arr2[a]; int arr22[a]; for (int i = 0; i < a; i++) cin >> arr1[i]; for (int i = 0; i < a; i++) cin >> arr2[i]; sort(arr1, arr1 + a); sort(arr2, arr2 + a); for (int i = 0; i < a; i++) { arr11[i] = arr1[i]; arr22[i] = arr2[i]; } int xiao = INT_MAX; for (int i = 0; i < a; i++) { int cha; if (arr11[i] > arr22[0]) cha = b - arr11[i] + arr22[0]; else cha = -1 * arr11[i] + arr22[0]; for (int j = 0; j < a; j++) arr1[j] = (arr11[j] + cha) % b; sort(arr1, arr1 + a); int flag = 0; for (int j = 0; j < a; j++) { if (arr1[j] != arr2[j]) { flag = 1; break; } } if (!flag) xiao = min(xiao, cha); } cout << xiao << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, a[2001], b[2001], ans = INT_MAX; cin >> n >> m; 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 x; sort(b, b + n); for (long long int i = 0; i < n; i++) { x = (b[i] - a[0] + m) % m; long long int c[n]; for (long long int j = 0; j < n; j++) { c[j] = (a[j] + x) % m; } sort(c, c + n); long long int flag = 0; for (long long int j = 0; j < n; j++) { if (c[j] != b[j]) { flag = 1; break; } } if (flag == 0) { ans = min(ans, x); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m; cin >> n >> m; vector<long long int> arr1(n), arr2(n); for (int i = 0; i < n; i++) { cin >> arr1[i]; } for (int i = 0; i < n; i++) { cin >> arr2[i]; } sort(arr2.begin(), arr2.end()); sort(arr1.begin(), arr1.end()); int ans = m; for (int i = 0; i < n; i++) { bool flag = true; int a = m - (arr1[i] - arr2[0]); a = (a % m); for (int j = 0; j < n; j++) { if ((arr1[(i + j) % n] + a) % m != arr2[j]) { flag = false; break; } } if (flag) { ans = min(ans, a); } } cout << ans << endl; } int 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; const int N = 1e5 + 5; long long debt[N], lend[N]; void each_case(int t) { int n, m, i, j, k, tp; cin >> n >> m; int a[n], b[n]; for (i = 0; i <= n - 1; i++) cin >> a[i]; for (i = 0; i <= n - 1; i++) cin >> b[i]; sort(a, a + n); sort(b, b + n); int mn = 1e9 + 5; for (i = 0; i <= n - 1; i++) { int eq = (b[0] - a[i] + m) % m; k = (i + 1) % n; bool f = 1; for (j = 1; j <= n - 1; j++) { if (eq != (b[j] - a[k] + m) % m) { f = 0; break; } k = (k + 1) % n; } if (f) mn = min(mn, eq); } cout << mn << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1, i; for (i = 1; i <= t; i++) each_case(i); return 0; }