text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int x;
int qzh_a[2008];
int qzh_b[2008];
int a[2008];
int b[2008];
int f[2008];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
scanf("%d", &b[i]);
}
scanf("%d", &x);
for (int i = 1; i <= n; i++) {
qzh_a[i] = qzh_a[i - 1] + a[i];
}
for (int i = 1; i <= m; i++) {
qzh_b[i] = qzh_b[i - 1] + b[i];
}
for (int i = 1; i <= m; i++) f[i] = 1e9;
for (int l = 1; l <= m; l++) {
for (int r = l; r <= m; r++) {
f[r - l + 1] = min(f[r - l + 1], qzh_b[r] - qzh_b[l - 1]);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
int MAX = x / (qzh_a[j] - qzh_a[i - 1]);
int g = upper_bound(f + 1, f + 1 + m, MAX) - f - 1;
ans = max(ans, g * (j - i + 1));
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int m, n, k;
long long int sum = 0;
long long int x;
cin >> m >> n;
long long int a[m];
for (long long int i = 0; i < m; i++) {
cin >> a[i];
}
long long int b[n];
sum = 0;
for (long long int j = 0; j < n; j++) {
cin >> b[j];
}
cin >> k;
map<long long int, long long int> m1;
map<long long int, long long int> m2;
for (long long int i = 0; i < m; i++) {
x = 0;
for (long long int j = i; j < m; j++) {
x += a[j];
if (m1.find(x) != m1.end())
m1[x] = max(m1[x], j - i);
else
m1[x] = j - i;
}
}
long long int dis = 0;
while (!m1.empty()) {
auto itr = m1.begin();
long long int u = itr->first;
long long int v = itr->second;
m1.erase(itr);
dis = max(dis, v);
m2[u] = dis;
}
long long int ans = 0;
for (long long int i = 0; i < n; i++) {
x = 0;
for (long long int j = i; j < n; j++) {
x += b[j];
if (k >= x) {
long long int u = k / x;
auto itr = m2.upper_bound(u);
if (itr != m2.begin()) {
itr--;
if (itr->first <= u) {
long long int o = itr->second;
ans = max(ans, ((o + 1) * (j - i + 1)));
}
}
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int L = 1e2 + 6;
const int N = 1e5 + 6;
const long long INF = 1e18 + 6;
struct base {
int x, y;
};
long long a[N], b[N], f1[N], f2[N], ans = 0;
void read(long long &now) {
now = 0;
char s;
int flag = 1;
while ((s > '9' || s < '0') && (s != '-')) s = getchar();
if (s == '-') flag = -1, s = getchar();
while (s >= '0' && s <= '9') {
now *= 10;
now += s - '0';
s = getchar();
}
now = now * flag;
}
void write(long long now) {
int get[20];
get[0] = 0;
if (now < 0) {
putchar('-');
now = -now;
}
if (now == 0) {
putchar('0');
return;
}
while (now) get[++get[0]] = now % 10, now = now / 10;
for (int i = get[0]; i >= 1; i--) putchar(get[i] + '0');
}
void writeln(long long now) {
write(now);
puts("");
}
int main() {
long long n, m;
read(n);
read(m);
a[0] = 0;
b[0] = 0;
for (int i = 1; i <= n; i++) read(a[i]), a[i] += a[i - 1];
for (int i = 1; i <= m; i++) read(b[i]), b[i] += b[i - 1];
long long x;
read(x);
for (int i = 1; i <= n; i++) f1[i] = INF;
for (int i = 1; i <= m; i++) f2[i] = INF;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
f1[j - i + 1] = min(f1[j - i + 1], a[j] - a[i - 1]);
for (int i = 1; i <= m; i++)
for (int j = i; j <= m; j++)
f2[j - i + 1] = min(f2[j - i + 1], b[j] - b[i - 1]);
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= m; j++)
if ((long double)f1[i] * (long double)f2[j] <= (long double)x)
ans = max(ans, i * j);
write(ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
vector<pair<long long, long long> > bs;
long long a[maxn], b[maxn], n, m, x;
long long pref[maxn * maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
cin >> b[i];
long long sum = 0LL;
for (int j = i; j >= 1; j--) {
sum += b[j];
bs.push_back(make_pair(sum, i - j + 1));
}
}
cin >> x;
sort(bs.begin(), bs.end());
for (int i = 0; i < bs.size(); i++) {
pref[i] = max(pref[i], bs[i].second);
pref[i + 1] = pref[i];
}
long long result = 0LL;
for (int i = 1; i <= n; i++) {
long long sum = 0LL;
for (int j = i; j >= 1; j--) {
sum += a[j];
if (sum > x) continue;
long long index = 0LL;
for (int cekor = int(bs.size()) / 2; cekor > 0; cekor /= 2) {
while (index + cekor < bs.size() && sum * bs[index + cekor].first <= x)
index += cekor;
}
if (bs[index].first * sum > x) continue;
result = max(result, 1LL * (i - j + 1) * pref[index]);
}
}
cout << result << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void dynamic() {
int n, m;
cin >> n >> m;
int a[n], b[m];
int sa[n], sb[m];
int z = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
z += a[i];
sa[i] = z;
}
z = 0;
for (int i = 0; i < m; i++) {
cin >> b[i];
z += b[i];
sb[i] = z;
}
long long int x;
cin >> x;
int ans = 0;
vector<pair<int, int> > v;
for (int i = 0; i < m; i++) {
for (int j = i; j < m; j++) {
int tmp = 0;
if (i - 1 >= 0) tmp = sb[i - 1];
int sum = sb[j] - tmp;
v.push_back({sum, j - i + 1});
}
}
sort(v.begin(), v.end());
int n1 = v.size();
for (int i = 1; i < n1; i++) {
v[i].second = max(v[i].second, v[i - 1].second);
}
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int tmp = 0;
if (i - 1 >= 0) tmp = sa[i - 1];
int sum = sa[j] - tmp;
int y = x / sum;
pair<int, int> p;
p = {y, 1e6};
int it = (upper_bound(v.begin(), v.end(), p) - v.begin());
it--;
if (it >= 0) {
ans = max(ans, (j - i + 1) * v[it].second);
}
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
while (t--) {
dynamic();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2001;
int a[MAXN], b[MAXN], ma[MAXN], mb[MAXN];
int main() {
int x, n, m;
cin >> n >> m;
memset(ma, 0x3f, sizeof(ma));
memset(mb, 0x3f, sizeof(mb));
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) cin >> b[i];
cin >> x;
for (int i = 1; i <= n; i++) {
int s = 0;
for (int j = i; j <= n; j++) {
s += a[j];
ma[j - i + 1] = min(ma[j - i + 1], s);
}
}
for (int i = 1; i <= m; i++) {
int s = 0;
for (int j = i; j <= m; j++) {
s += b[j];
mb[j - i + 1] = min(mb[j - i + 1], s);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (1LL * ma[i] * mb[j] <= 1LL * x) {
ans = max(ans, i * j);
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long dp1[100005], dp2[100005];
long long ans1[100005], ans2[100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
std::vector<long long> a(n + 1), b(m + 1);
long long x;
for (long long int i = 1; i < n + 1; i++) {
cin >> a[i];
dp1[i] = dp1[i - 1] + a[i];
}
for (long long int i = 1; i < m + 1; i++) {
cin >> b[i];
dp2[i] = dp2[i - 1] + b[i];
}
cin >> x;
for (long long int i = 1; i < n + 1; i++) {
long long cur = 1e18;
for (long long int j = i; j < n + 1; j++) {
long long z1 = dp1[j] - dp1[j - i];
cur = min(cur, z1);
}
ans1[i] = cur;
}
for (long long int i = 1; i < m + 1; i++) {
long long cur = 1e18;
for (long long int j = i; j < m + 1; j++) {
long long z1 = dp2[j] - dp2[j - i];
cur = min(cur, z1);
}
ans2[i] = cur;
}
long long ans = -1;
for (long long int i = 1; i < n + 1; i++) {
for (long long int j = 1; j < m + 1; j++) {
if ((ans1[i] * ans2[j]) <= x) {
ans = max(ans, i * j);
}
}
}
if (ans != -1)
cout << ans;
else
cout << "0";
}
|
#include <bits/stdc++.h>
using namespace std;
inline void boost() {
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
}
const long long maxn = 1e5 + 123;
const long long inf = 1e9 + 123;
const long long mod = 1e9 + 7;
const double pi = acos(-1);
int main() {
boost();
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
int n, d, y, x;
cin >> n >> x >> y >> d;
int mn = inf;
if (abs(y - x) % d == 0) {
mn = min(mn, abs(y - x) / d);
}
if (abs(y - 1) % d == 0 && abs(x - 1) % d == 0) {
mn = min(abs(x - 1) / d + abs(y - 1) / d, mn);
}
if (abs(y - 1) % d == 0 && abs(x - 1) % d != 0) {
mn = min(abs(x - 1) / d + abs(y - 1) / d + 1, mn);
}
if (abs(y - n) % d == 0 && abs(x - n) % d == 0) {
mn = min(abs(x - n) / d + abs(y - n) / d, mn);
}
if (abs(y - n) % d == 0 && abs(x - n) % d != 0) {
mn = min(abs(x - n) / d + abs(y - n) / d + 1, mn);
}
if (mn == inf) {
cout << "-1\n";
} else {
cout << mn << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, x, y, d;
scanf("%d%d%d%d", &n, &x, &y, &d);
if (abs(x - y) % d == 0)
printf("%d\n", abs(x - y) / d);
else {
int minn = 0x3f3f3f3f;
if ((y - 1) % d == 0) {
int t = (x - 1) / d;
if ((x - 1) % d != 0) t++;
minn = min(minn, t + (y - 1) / d);
}
if ((n - y) % d == 0) {
int t = (n - x) / d;
if ((n - x) % d != 0) t++;
minn = min(minn, t + (n - y) / d);
}
if (minn == 0x3f3f3f3f)
printf("-1\n");
else
printf("%d\n", minn);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int ans = 1e9;
if (abs(y - x) % d == 0) {
ans = abs(y - x) / d;
}
if ((y - 1) % d == 0) {
ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
}
if ((n - y) % d == 0) {
ans = min(ans, (n - x + d - 1) / d + (n - y) / d);
}
cout << (ans == 1e9 ? -1 : ans);
if (t) {
cout << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int i, j, k;
int read() {
int tot = 0, fh = 1;
char c = getchar();
while ((c < '0') || (c > '9')) {
if (c == '-') fh = -1;
c = getchar();
}
while ((c >= '0') && (c <= '9')) {
tot = tot * 10 + c - '0';
c = getchar();
}
return tot * fh;
}
long long n, s, t, d, t1, t2, T, opt;
int main() {
T = read();
for (opt = 1; opt <= T; opt++) {
n = read();
s = read();
t = read();
d = read();
if (abs(s - t) % d == 0) {
printf("%I64d\n", abs(s - t) / d);
} else {
t1 = 1e18;
if (abs(1 - t) % d == 0) {
t1 = abs(1 - t) / d;
t1 = t1 + (s - 1) / d + (((s - 1) % d) != 0);
}
t2 = 1e18;
if (abs(n - t) % d == 0) {
t2 = abs(n - t) / d;
t2 = t2 + (n - s) / d + (((n - s) % d) != 0);
}
if ((t1 != 1e18) || (t2 != 1e18)) {
printf("%I64d\n", min(t1, t2));
} else
printf("-1\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
const ll inf = (1e18);
const ll mod = 998244353;
int GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
__INIT() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(5);
}
} __init;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void pitsu() {
ll n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << "\n";
} else {
if ((y - 1) % d != 0 && (n - y) % d != 0) {
cout << -1 << "\n";
} else if ((y - 1) % d == 0 && (n - y) % d == 0) {
cout << min((x + d - 1) / d + (y - 1) / d,
(n - x + d - 1) / d + (n - y) / d)
<< "\n";
} else if ((y - 1) % d == 0) {
cout << (x + d - 1) / d + (y - 1) / d << "\n";
} else {
cout << (n - x + d - 1) / d + (n - y) / d << "\n";
}
}
}
int main() {
int t;
cin >> t;
while (t--) pitsu();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << '\n';
} else if (abs(y - 1) % d == 0 && abs(n - y) % d != 0) {
int move = abs(x - 1) / d + 1 + abs(y - 1) / d;
cout << move << '\n';
} else if (abs(y - 1) % d != 0 && abs(n - y) % d == 0) {
int move = abs(n - x) / d + 1 + abs(n - y) / d;
cout << move << '\n';
} else if (abs(y - 1) % d == 0 && abs(n - y) % d == 0) {
int move1 = abs(x - 1) / d + 1 + abs(y - 1) / d;
int move2 = abs(n - x) / d + 1 + abs(n - y) / d;
cout << min(move1, move2) << '\n';
} else {
cout << "-1\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long powmod(long long a, long long b, long long p) {
if (b == 0) return 1;
if (b == 1) return a;
long long temp = powmod(a, b / 2, p);
temp *= temp;
temp %= p;
if (b % 2 == 0) return temp;
temp = temp * a;
temp %= p;
return temp;
}
void solve() {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
;
return;
}
long long ans = 1e18;
if (abs(y - 1) % d == 0) {
ans = (y - 1) / d;
ans += (x - 1 + d - 1) / d;
}
if ((n - y) % d == 0) {
long long ans2 = (n - y) / d;
ans2 += (n - x + d - 1) / d;
ans = min(ans, ans2);
}
if (ans > 1e10)
cout << -1 << endl;
else
cout << ans << endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y, d;
int t;
int main() {
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
int ans = 0x7fffffff;
if (((y - x) % d) && ((y - 1) % d) && ((n - y) % d)) {
cout << -1 << endl;
continue;
}
if (!((y - x) % d)) ans = min(ans, abs(y - x) / d);
if (!((y - 1) % d)) ans = min(ans, (y - 1) / d + ((x - 2 + d) / d));
if (!((n - y) % d)) ans = min(ans, (n - y) / d + (n - x + d - 1) / d);
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
bool isPoTwo(long long int x) { return x && (!(x & (x - 1))); }
long long int power(long long int x, unsigned long long int y,
long long int p) {
long long int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void solve() {
long long int n, x, y, d;
cin >> n >> x >> y >> d;
long long int ans = INT_MAX, ans1 = INT_MAX, ans2 = INT_MAX;
if ((y - x) % d == 0) ans = (abs(y - x) / d);
if ((y - 1) % d == 0 || (n - y) % d == 0) {
if ((y - 1) % d == 0) {
ans1 = (y - 1) / d;
ans1 += ((x - 1) / d) + 1;
}
if ((n - y) % d == 0) {
ans2 = (n - y) / d;
ans2 += ((n - x) / d) + 1;
}
ans = min(ans, ans2);
ans = min(ans, ans1);
}
if (ans == INT_MAX) ans = -1;
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
int n, x, y, d;
scanf("%d %d %d %d", &n, &x, &y, &d);
if (abs(x - y) % d == 0)
printf("%d\n", abs(x - y) / d);
else if ((y - 1) % d != 0 && (n - y) % d != 0)
printf("-1\n");
else {
int res = (-1) ^ (1 << 31);
if ((y - 1) % d == 0) res = min(res, (x + d - 1) / d + y / d);
if ((n - y) % d == 0) res = min(res, (n - x + d - 1) / d + (n - y) / d);
printf("%d\n", res);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int A(int a) { return a > 0 ? a : -a; }
int main() {
int T, mx = INT_MAX;
scanf("%d", &T);
while (T--) {
int ans = mx, n, x, y, d;
scanf("%d%d%d%d", &n, &x, &y, &d);
if (A(x - y) % d == 0) {
printf("%d\n", A(x - y) / d);
continue;
}
if ((y - 1) % d == 0)
ans = min(ans, (y - 1) / d + (x - 1) / d + ((x - 1) % d == 0 ? 0 : 1));
if ((n - y) % d == 0)
ans = min(ans, (n - y) / d + (n - x) / d + ((n - x) % d == 0 ? 0 : 1));
printf("%d\n", ans == mx ? -1 : ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using VI = vector<int>;
int n, d, t, x, y;
int main() {
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
if (x % d == y % d)
cout << abs(x - y) / d << endl;
else {
int ans = -1;
if (y % d == 1 % d) ans = (x - 1 + d - 1) / d + (y - 1 + d - 1) / d;
if (y % d == n % d) {
int tmp = (n - x + d - 1) / d + (n - y + d - 1) / d;
if (ans == -1 || ans > tmp) ans = tmp;
}
cout << ans << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
inline int sbt(int x) { return __builtin_popcount(x); }
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
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...);
}
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
int res = 1000000007;
cin >> n >> x >> y >> d;
int dff = abs(x - y);
if (!(dff % d)) res = min(res, dff / d);
if (!((y - 1) % d))
res = min(res, (y - 1) / d + (x - 1) / d + (((x - 1) % d) > 0));
if (!((n - y) % d))
res = min(res, (n - x) / d + (n - y) / d + (((n - x) % d) > 0));
if (res == 1000000007)
cout << -1;
else
cout << res;
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int ans = 0x3f3f3f3f;
int ans1 = -1, ans2 = -1, ans3 = -1;
if ((x - y) % d == 0) ans1 = abs(x - y) / d;
if ((y - 1) % d == 0) ans2 = ceil(1.0 * (x - 1) / d) + (y - 1) / d;
if ((n - y) % d == 0) ans3 = ceil(1.0 * (n - x) / d) + (n - y) / d;
if (ans1 < 0 && ans2 < 0 && ans3 < 0)
cout << -1 << endl;
else {
if (ans1 >= 0) ans = (ans1) > (ans) ? (ans) : (ans1);
if (ans2 >= 0) ans = (ans2) > (ans) ? (ans) : (ans2);
if (ans3 >= 0) ans = (ans3) > (ans) ? (ans) : (ans3);
cout << ans << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, s, e, d;
cin >> n >> s >> e >> d;
int ans = 2 * n;
if (abs(s - e) % d == 0)
ans = abs(s - e) / d;
else {
if (abs(1 - e) % d == 0)
ans = abs(1 - e) / d + ceil((double)abs(1 - s) / d);
if (abs(n - e) % d == 0)
ans = min(ans, abs(n - e) / d + (int)ceil((double)abs(n - s) / d));
if (ans == 2 * n) ans = -1;
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f, NegOO = -1 * OO, N = 1e6 + 5, mod = 1e9 + 7;
long long gcd(long long x, long long y) {
if (y == 0) return x;
return gcd(y, x % y);
}
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (x == y) {
cout << 0 << "\n";
continue;
} else if (x < y) {
int mx = 1e9 + 5;
int ans1 = mx, ans2 = mx, ans3 = mx;
if ((y - x) % d == 0) ans1 = (y - x) / d;
if ((y - 1) % d == 0)
ans2 = (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d;
if ((n - y) % d == 0)
ans3 = (n - x) / d + ((n - x) % d != 0) + (n - y) / d;
int ans = min(min(ans1, ans2), ans3);
cout << ((ans != mx) ? ans : -1) << "\n";
} else {
int mx = 1e9 + 5;
int ans1 = mx, ans2 = mx, ans3 = mx;
if ((x - y) % d == 0) ans1 = (x - y) / d;
if ((y - 1) % d == 0)
ans2 = (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d;
if ((n - y) % d == 0)
ans3 = (n - x) / d + ((n - x) % d != 0) + (n - y) / d;
int ans = min(min(ans1, ans2), ans3);
cout << ((ans != mx) ? ans : -1) << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int main() {
int t;
for (cin >> t; t--;) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int an = inf;
if (abs(x - y) % d == 0) {
an = abs(x - y) / d;
} else {
if ((y - 1) % d == 0) {
an = min(an, (x - 1) / d + 1 + (y - 1) / d);
}
if ((n - y) % d == 0) {
an = min(an, (n - x) / d + 1 + (n - y) / d);
}
}
if (an == inf) an = -1;
cout << an << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long xceil(long long a, long long b) {
if (a % b == 0)
return a / b;
else
return 1 + a / b;
}
int main() {
long long t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long ans = 1e18, a = 1e18, b = 1e18;
if (abs(y - x) % d == 0)
ans = abs(y - x) / d;
else {
if ((y - 1) % d == 0) a = xceil(x - 1, d) + (y - 1) / d;
if ((n - y) % d == 0) b = xceil(n - x, d) + (n - y) / d;
}
ans = min(ans, min(a, b));
if (ans == 1e18) ans = -1;
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n, x, y, d;
int a, b, c;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &x, &y, &d);
a = b = c = INT_MAX;
if (abs(x - y) % d == 0) a = abs(x - y) / d;
if (abs(1 - y) % d == 0) {
b = abs(1 - x) / d;
if (abs(1 - x) % d != 0) b++;
b += abs(1 - y) / d;
}
if (abs(n - y) % d == 0) {
c = abs(n - x) / d;
if (abs(n - x) % d != 0) c++;
c += abs(n - y) / d;
}
if ((abs(x - y) < d && (y == 1 || y == n))) a = 1;
if (a == b && b == c && a == INT_MAX)
puts("-1");
else
printf("%d\n", min(a, min(b, c)));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldb = long double;
struct init {
init() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
}
~init() {
cerr << "Time elapsed: " << static_cast<double>(clock()) / CLOCKS_PER_SEC
<< "s.\n";
}
} init;
const long long N = 100500 * 5, MOD = 1000 * 1000 * 1000 + 7;
const ldb EPS = 5e-12, HALF = 0.5;
signed main() {
long long t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long ans = 1e18;
if (abs(x - y) % d == 0) {
ans = abs(x - y) / d;
}
if (abs(n - y) % d == 0) {
ans = min(ans, abs(n - y) / d + (n - x + d - 1) / d);
}
if ((y - 1) % d == 0) {
ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
}
if (ans == 1e18) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
template <typename A, typename B>
istream& operator>>(istream& input, pair<A, B>& x) {
input >> x.first >> x.second;
return input;
}
template <typename A>
istream& operator>>(istream& input, vector<A>& x) {
for (auto& i : x) input >> i;
return input;
}
void solve() {
long long n, d, x, y;
cin >> n >> x >> y >> d;
if (x % d == y % d)
cout << abs(x - y) / d << '\n';
else if (y % d == 1 % d and y % d != n % d)
cout << (x - 1 + d - 1) / d + (y - 1) / d << '\n';
else if (y % d != 1 % d and y % d == n % d)
cout << (n - x + d - 1) / d + (n - y) / d << '\n';
else if (y % d == 1 % d and y % d == n % d)
cout << min((x - 1 + d - 1) / d + (y - 1) / d,
(n - x + d - 1) / d + (n - y) / d)
<< '\n';
else
cout << -1 << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
long long int min(long long res, long long temp) {
if (res > temp)
return temp;
else
return res;
}
int main() {
int t, flag = 0;
long long int n, x, y, d, res, temp, a;
scanf("%d", &t);
while (t--) {
scanf("%lld %lld %lld %lld", &n, &x, &y, &d);
a = abs(x - y);
flag = 0;
res = 1000000095;
temp = res;
if (a % d == 0) {
flag = 1;
res = a / d;
res = min(res, temp);
temp = res;
}
if ((y - 1) % d == 0) {
flag = 2;
if ((x - 1) % d == 0) {
res = ((x - 1) / d) + ((y - 1) / d);
res = min(res, temp);
temp = res;
} else {
res = 1 + ((x - 1) / d) + ((y - 1) / d);
res = min(res, temp);
temp = res;
}
}
if ((n - y) % d == 0) {
flag = 3;
if ((n - x) % d == 0) {
res = ((n - x) / d) + ((n - y) / d);
res = min(res, temp);
temp = res;
} else {
res = 1 + ((n - x) / d) + ((n - y) / d);
res = min(res, temp);
temp = res;
}
}
if (flag == 0) {
res = -1;
}
printf("%lld\n", res);
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int m, n, b, c, t, aa, bb, cc, ans;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &m, &n, &b, &c);
int qq = (n - 1) % c;
int pp = (m - n) % c;
int yy = abs(n - b) % c;
int tt = abs(b - 1) % c;
int oo = abs(m - b) % c;
if (yy != 0 && tt != 0 && oo != 0) {
cout << "-1" << endl;
} else if (yy == 0) {
cout << abs(n - b) / c << endl;
} else if (tt == 0 || oo == 0) {
if (tt == 0 && oo == 0) {
long long int ans1 = 0, ans2 = 0;
ans1 = abs(n - 1) / c + abs(b - 1) / c;
if (qq != 0) {
ans1 += 1;
} else {
ans1 += 0;
}
ans2 = abs(m - n) / c + abs(m - b) / c;
if (pp != 0) {
ans2 += 1;
} else {
ans2 += 0;
}
cout << min(ans1, ans2) << endl;
} else if (tt == 0) {
long long int ans1 = 0, ans2 = 0;
ans1 = abs(n - 1) / c + abs(b - 1) / c;
if (qq != 0) {
ans1 += 1;
} else {
ans1 += 0;
}
cout << ans1 << endl;
} else if (oo == 0) {
long long int ans2 = 0;
ans2 = abs(m - n) / c + abs(m - b) / c;
if (pp != 0) {
ans2 += 1;
} else {
ans2 += 0;
}
cout << ans2 << endl;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long t, x, y, n, d;
int main() {
cin >> t;
for (int i = 1; i <= t; i++) {
int tmp, flag = -1;
long long res = 1000000000;
cin >> n >> x >> y >> d;
if ((y - 1) % d == 0) {
flag = 1;
tmp = (x - 1) / d;
if ((x - 1) % d != 0) tmp++;
res = min(res, tmp + (y - 1) / d);
}
if ((n - y) % d == 0) {
flag = 1;
tmp = (n - x) / d;
if ((n - x) % d != 0) tmp++;
res = min(res, tmp + (n - y) / d);
}
if (abs(x - y) % d == 0) {
flag = 1;
res = min(res, abs(x - y) / d);
}
if (flag != -1)
cout << res << endl;
else
cout << -1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int inp;
void sc(long long &x) {
scanf("%d", &inp);
x = inp;
}
void sc(long long &x, long long &y) {
scanf("%d", &inp);
x = inp;
scanf("%d", &inp);
y = inp;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void setbit(int &m, int ind, int val) {
if (val)
m = (m | (1 << ind));
else
m = (m & ~(1 << ind));
}
bool getbit(int m, int ind) {
int a = (1 << ind);
a = (a & m);
return a;
}
int cntbits(int m) {
int ret = 0;
while (m) {
ret += (m % 2);
m /= 2;
}
return ret;
}
bool smlltr(char c) {
if (c >= 'a' && c <= 'z') return 1;
return 0;
}
bool capltr(char c) {
if (c >= 'A' && c <= 'Z') return 1;
return 0;
}
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long long mod3 = 1e9 + 9;
long long pow_mod(long long a, long long b) {
if (b == 0) return 1;
if (b % 2) return (a * pow_mod((a * a) % mod, b / 2)) % mod;
return pow_mod((a * a) % mod, b / 2);
}
int n, m, tmp, d, x, y, z, k, sol, maxi, mini, sum, T, k1, k2;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--) {
cin >> n >> x >> y >> d;
tmp = abs(x - y);
if (tmp % d == 0) {
cout << tmp / d << endl;
continue;
}
sol = 2147483645;
k1 = (x - 1) / d;
if ((x - 1) % d != 0) k1++;
k2 = (n - x) / d;
if ((n - x) % d != 0) k2++;
if (y % d == 1) sol = (y / d) + k1;
if ((n - y) % d == 0) sol = min(sol, (n - y) / d + k2);
if (sol == 2147483645)
cout << -1 << endl;
else
cout << sol << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
long long int x, y, d, n, ans = LLONG_MAX;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
cin >> n >> x >> y >> d;
ans = LLONG_MAX;
int a = x - d;
if (a < 1) a = 1;
int b = x + d;
if (b > n) b = n;
if (abs(x - y) % d == 0) {
ans = abs(x - y) / d;
}
if (abs(a - y) % d == 0) {
ans = min(abs(a - y) / d + 1, ans);
}
if (abs(1 - y) % d == 0) {
int c = ceil((double)abs(x - 1) / d);
ans = min(abs(1 - y) / d + c, ans);
}
if (abs(b - y) % d == 0) {
ans = min(abs(b - y) / d + 1, ans);
}
if (abs(n - y) % d == 0) {
int c = ceil((double)abs(x - n) / d);
ans = min(abs(n - y) / d + c, ans);
}
if (ans == LLONG_MAX) ans = -1;
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 20000000000LL;
long long n, x, y, d;
long long f() {
if (abs((y - x)) % d == 0) return abs((y - x)) / d;
long long ret = inf;
if ((y - 1) % d == 0) {
ret = (y - 1) / d + max((long long)ceil(1.0 * (x - 1) / d), 0LL);
}
if ((n - y) % d == 0) {
ret = min(ret, (n - y) / d + max(0LL, (long long)ceil(1.0 * (n - x) / d)));
}
if (ret == inf) ret = -1;
return ret;
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
cout << f() << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
inline void solve() {
int q;
cin >> q;
while (q--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
int otv = INT32_MAX;
if ((c - 1) % d == 0)
otv = min(otv, (b - 1) / d + ((b - 1) % d == 0 ? 0 : 1) + (c - 1) / d);
if ((a - c) % d == 0)
otv = min(otv, (a - b) / d + ((a - b) % d == 0 ? 0 : 1) + (a - c) / d);
if (abs(b - c) % d == 0) otv = min(otv, abs(b - c) / d);
if (otv == INT32_MAX)
cout << -1;
else
cout << otv;
cout << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y, d;
long long const MAX = 4 * 1e6;
int techo(int a, int b) {
if (a % b) return a / b + 1;
return a / b;
}
int main() {
cin.tie(0), ios_base::sync_with_stdio(0);
;
int t;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0)
cout << abs(y - x) / d << endl;
else if ((y - 1) % d == 0) {
if ((n - y) % d == 0) {
cout << min(techo(x, d) + (y - 1) / d, techo(n - x, d) + (n - y) / d)
<< endl;
} else
cout << techo(x, d) + (y - 1) / d << endl;
} else if ((n - y) % d == 0) {
cout << techo(n - x, d) + (n - y) / d << endl;
} else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
int n, x, y, d;
while (t--) {
cin >> n >> x >> y >> d;
int ans;
if (abs(x - y) % d == 0) {
ans = abs(x - y) / d;
cout << ans << endl;
continue;
}
int fans = INT_MAX;
if (abs(1 - y) % d == 0) {
ans = (abs(x - 1) + d - 1) / d + abs(1 - y) / d;
fans = min(fans, ans);
}
if (abs(n - y) % d == 0) {
ans = (abs(x - n) + d - 1) / d + abs(n - y) / d;
fans = min(fans, ans);
}
if (fans != INT_MAX) {
cout << fans << endl;
} else {
cout << -1 << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int a = (y - 1), b = (n - y), c = abs(x - y), f = (n - x), g = (x - 1);
if (c % d == 0) {
cout << c / d << endl;
continue;
}
int k = g / d + (g % d != 0) + a / d, m = f / d + (f % d != 0) + b / d;
if (a % d == 0) {
if (b % d == 0)
cout << min(k, m) << endl;
else
cout << k << endl;
continue;
} else {
if (b % d == 0)
cout << m << endl;
else
cout << -1 << endl;
continue;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int t;
const long long inf = 1e18;
void solve() {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long a, b, c;
if ((abs(y - x)) % d == 0)
a = abs(y - x) / d;
else
a = inf;
if ((n - y) % d == 0)
b = (max(0ll, (n - x - 1) / d + 1)) + (n - y) / d;
else
b = inf;
if ((y - 1) % d == 0)
c = (max((x - 1 - 1) / d + 1, 0ll)) + (y - 1) / d;
else
c = inf;
long long ans = min(a, b);
ans = min(ans, c);
if (ans == inf)
cout << -1 << endl;
else
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
for (int i = (0); i != (t); ++i) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
int t;
void solve() {
while (t--) {
int n, d, x, y;
cin >> n >> x >> y >> d;
if ((y - x) % d == 0) {
cout << abs(y - x) / d << endl;
continue;
}
int res1 = -1, res2 = -1;
if ((y - 1) % d == 0) {
res1 = (abs(x - 1) / d);
if ((x - 1) % d) res1++;
res1 += (y - 1) / d;
}
if ((n - y) % d == 0) {
res2 = (abs(n - x) / d);
if ((n - x) % d) res2++;
res2 += (n - y) / d;
}
if (res1 == -1 && res2 == -1) {
cout << -1 << endl;
} else {
if (res1 == -1)
cout << res2 << endl;
else if (res2 == -1)
cout << res1 << endl;
else
cout << min(res1, res2) << endl;
}
}
}
int main() {
ios::sync_with_stdio(false);
while (cin >> t) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
long long n, x, y, d;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
} else if ((y - 1) % d != 0 && (n - y) % d != 0) {
cout << "-1" << endl;
} else {
long long b = n;
if ((y - 1) % d == 0) {
b = (y - 1) / d + ceil(x * 1.0 / d);
}
if ((n - y) % d == 0) {
long long b1 = ((n - y) / d + ceil((n - x) * 1.0 / d));
b = min(b, b1);
}
cout << b << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d, x, y, t;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
long long ans = 2e18;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
} else {
if ((y - 1) % d == 0) {
long long num = 0;
if ((x - 1) % d) num++;
num += (x - 1) / d;
num += (y - 1) / d;
ans = min(num, ans);
}
if ((n - y) % d == 0) {
long long num = 0;
if ((n - x) % d) num++;
num += (n - x) / d;
num += (n - y) / d;
ans = min(num, ans);
}
if (ans == 2e18)
cout << -1 << endl;
else
cout << ans << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n, x, y, ans1, ans2, ans, d;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
if ((abs(x - y)) % d == 0)
ans = (abs(x - y)) / d;
else if ((y - 1) % d == 0) {
if ((n - y) % d == 0) {
if ((x - 1) % d == 0)
ans1 = (x - 1) / d + (y - 1) / d;
else
ans1 = (x - 1) / d + 1 + (y - 1) / d;
if ((n - x) % d == 0)
ans2 = (n - x) / d + (n - y) / d;
else
ans2 = (n - x) / d + 1 + (n - y) / d;
ans = min(ans1, ans2);
} else {
if ((x - 1) % d == 0)
ans = (x - 1) / d + (y - 1) / d;
else
ans = (x - 1) / d + 1 + (y - 1) / d;
}
} else if ((n - y) % d == 0) {
if ((n - x) % d == 0)
ans = (n - x) / d + (n - y) / d;
else
ans = (n - x) / d + 1 + (n - y) / d;
} else
ans = -1;
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int proper(long long int x, long long int y, long long int d) {
long long int c = abs(x - y);
if (c % d == 0)
return c / d;
else
return 100000000000ll;
}
long long int improper(long long int x, long long int y, long long int d) {
long long int c = abs(x - y);
long long int ans = c / d;
if (c % d != 0) ans++;
return ans;
}
int main() {
int t;
cin >> t;
while (t--) {
long long int n, x, y, d;
cin >> n >> x >> y >> d;
long long int ans = 100000000000ll;
ans = min(ans, proper(x, y, d));
ans = min(ans, (improper(x, 1, d) + proper(1, y, d)));
ans = min(ans, (improper(x, n, d) + proper(n, y, d)));
if (ans >= 100000000000ll)
cout << "-1\n";
else
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, x, y, i, d, min1 = 1000000000, min2 = 1000000000;
int mas[10000];
cin >> t;
for (i = 0; i < t; i++) {
min1 = 1000000000;
min2 = 1000000000;
cin >> n >> x >> y >> d;
if ((abs(y - x)) % d == 0)
mas[i] = (abs(y - x)) / d;
else {
if (((n - y) % d == 0 || (y - 1) % d == 0) && n - y != 0 && y - 1 != 0) {
if ((n - y) % d == 0 && n - y != 0) {
min1 = ceil((n - x) / (d * 1.0)) + (n - y) / d;
}
if ((y - 1) % d == 0 && y - 1 != 0) {
min2 = ceil((x - 1) / (d * 1.0)) + (y - 1) / d;
}
mas[i] = min(min1, min2);
} else {
mas[i] = -1;
}
}
if (y == n || y == 1) {
mas[i] = ceil(abs(y - x) / (d * 1.0));
}
}
for (i = 0; i < t; i++) {
cout << mas[i] << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int q, n, x, y, d, s = 0, st = 0, en = 0;
cin >> q;
for (int i = 0; i < q; i++) {
cin >> n >> x >> y >> d;
if ((y - x) % d != 0 && (y - 1) % d != 0 && (n - y) % d != 0) {
cout << -1 << endl;
} else {
if ((y - x) % d == 0)
cout << abs((y - x) / d) << endl;
else if ((y - 1) % d == 0 && (n - y) % d == 0) {
cout << abs(min((n - y) / d + (n - x + d - 1) / d,
(y - 1) / d + (x - 1 + d - 1) / d))
<< endl;
} else if ((y - n) % d == 0) {
cout << abs((n - y) / d + (n - x + d - 1) / d) << endl;
;
} else
cout << abs((y - 1) / d + (x - 1 + d - 1) / d) << endl;
}
}
}
|
#include <bits/stdc++.h>
const int maxn = 10000;
using namespace std;
long long n, d;
long long qiu1(long long x, long long y) {
long long ans = -1;
if ((y - 1) % d == 0) {
ans = (y - 1) / d;
ans += (x - 1) / d;
if ((x - 1) % d != 0) ans++;
}
return ans;
}
long long qiu2(long long x, long long y) {
long long ans = -1;
if ((n - y) % d == 0) {
ans = (n - y) / d;
ans += (n - x) / d;
if ((n - x) % d != 0) ans++;
}
return ans;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long x, y;
cin >> n >> x >> y >> d;
long long t = y - x;
if (y < x) {
t = x - y;
}
if (t % d == 0) {
cout << t / d << endl;
continue;
}
long long ans1 = -1, ans2 = -1;
ans1 = qiu1(x, y);
ans2 = qiu2(x, y);
if (ans1 == -1 && ans2 == -1) {
printf("-1");
} else {
if (ans1 == -1) {
cout << ans2;
} else {
if (ans2 == -1)
cout << ans1;
else
cout << min(ans1, ans2);
}
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int setBit(int n, int p) { return n | (1 << p); }
bool checkBit(int n, int p) { return (bool)(n & (1 << p)); }
int fx[] = {0, -1, 0, 1};
int fy[] = {1, 0, -1, 0};
long long bigMod(long long x, long long n) {
if (n == 0) return 1;
long long t = bigMod(x, n / 2);
t = ((t % 1000000007) * (t % 1000000007)) % 1000000007;
if (n % 2) {
t = ((x % 1000000007) * (t % 1000000007)) % 1000000007;
}
return t;
}
int main() {
int t;
cin >> t;
while (t--) {
long long n, x, y, d, p, q, r, ans = 100000000000000;
cin >> n >> x >> y >> d;
long long temp = abs(y - x);
if (temp % d == 0) {
cout << temp / d << endl;
} else {
temp = abs(x - 1);
if (temp == 0) {
p = 0;
} else {
p = (temp - 1) / d + 1;
}
if ((y - 1) % d == 0) {
ans = p + (y - 1) / d;
}
temp = abs(n - x);
if (temp == 0) {
q = 0;
} else {
q = (temp - 1) / d + 1;
}
if ((n - y) % d == 0) {
ans = min(ans, q + (n - y) / d);
}
if (ans == 100000000000000)
cout << -1 << endl;
else
cout << ans << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int diff = abs(y - x);
if (diff % d == 0) {
cout << (diff / d) << endl;
} else {
int d1 = (y - 1);
int d2 = (n - y);
int steps = 2 * n;
if (d1 % d == 0) {
int tmp = d1 / d;
if (x > 1) tmp += (x - 2) / d + 1;
steps = min(steps, tmp);
}
if (d2 % d == 0) {
int tmp = d2 / d;
if (x < n) tmp += (n - x - 1) / d + 1;
steps = min(steps, tmp);
}
if (steps < 2 * n)
cout << steps << endl;
else
cout << -1 << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
int main() {
int tn;
scanf("%d", &tn);
while (tn--) {
int n, x, y, d, ans = INT_MAX;
scanf("%d%d%d%d", &n, &x, &y, &d);
if (abs(x - y) % d == 0) ans = min(ans, abs(x - y) / d);
if ((y - 1) % d == 0) ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
if ((n - y) % d == 0) ans = min(ans, (n - x + d - 1) / d + (n - y) / d);
if (ans == INT_MAX) ans = -1;
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 100001;
void snum(long long &a) { scanf("%lld", &a); }
void snum(long long &a, long long &b) { scanf("%lld %lld", &a, &b); }
void snum(long long &a, long long &b, long long &c) {
scanf("%lld %lld %lld", &a, &b, &c);
}
void snum(long long &a, long long &b, long long &c, long long &d) {
scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
}
long long A[N];
void solve() {
long long n, x, y, d;
snum(n, x, y, d);
long long mn = 1e10;
if ((abs(y - x)) % d == 0) {
mn = min(mn, (abs(y - x)) / d);
}
if ((y - 1) % d == 0) {
long long aux = (x - 1) / d;
if ((x - 1) % d != 0) {
aux++;
}
mn = min(mn, (y - 1) / d + aux);
}
if ((n - y) % d == 0) {
long long aux = (n - x) / d;
if ((n - x) % d != 0) {
aux++;
}
mn = min(mn, (n - y) / d + aux);
}
if (mn == 1e10) {
printf("-1\n");
} else {
printf("%lld\n", mn);
}
return;
}
int main() {
long long q;
snum(q);
for (long long i = 0; i < q; i++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
inline bool checkBit(long long n, int i) { return n & (1LL << i); }
inline long long setBit(long long n, int i) {
return n | (1LL << i);
;
}
inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long &a) {
a %= 1000000007;
(a < 0) && (a += 1000000007);
}
inline long long modMul(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long modPow(long long b, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1;
}
return r;
}
inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }
inline long long modDiv(long long a, long long b) {
return modMul(a, modInverse(b));
}
inline long long power(long long bs, long long k) {
long long x = 1LL, y = bs;
if (k == 0) return 1LL;
while (k > 0) {
if (k % 2) x *= y;
y *= y;
k /= 2;
}
return x;
}
template <typename first, typename second>
ostream &operator<<(ostream &os, const pair<first, second> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
string to_str(T t) {
stringstream ss;
ss << t;
return ss.str();
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template <typename first, typename second>
ostream &operator<<(ostream &os, const map<first, second> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
clock_t tStart = clock();
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
cerr << arg << ' ';
faltu(rest...);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cout.unsetf(ios::floatfield);
cout.precision(10);
cout.setf(ios::fixed, ios::floatfield);
;
long long tst, n, d, x, y;
cin >> tst;
++tst;
while (--tst) {
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) {
cout << abs(y - x) / d << '\n';
continue;
}
long long mx1 = infLL, mx2 = infLL;
if ((y - 1) % d == 0) {
mx1 = ((y - 1) / d) + ((x - 1 + d - 1) / d);
}
if ((n - y) % d == 0) mx2 = ((n - y) / d) + ((n - x + d - 1) / d);
mx1 = min(mx1, mx2);
if (mx1 != infLL)
cout << mx1 << '\n';
else
cout << -1 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 1e9;
int main() {
int t, n, x, y, d, sum;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &x, &y, &d);
sum = inf;
if (abs(x - y) % d == 0) sum = min(sum, abs(x - y) / d);
if (abs(y - 1) % d == 0)
sum = min(sum,
abs(y - 1) / d + abs(x - 1) / d + (int)(abs(x - 1) % d != 0));
if (abs(y - n) % d == 0)
sum = min(sum,
abs(y - n) / d + abs(x - n) / d + (int)(abs(x - n) % d != 0));
if (sum == inf) sum = -1;
printf("%d\n", sum);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void sol() {
long long n, x, y, d, ans = 10000000000;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) ans = min(ans, abs(y - x) / d);
if ((y - 1) % d == 0)
ans = min(ans, ((x - 1) / d) + ((x - 1) % d != 0) + ((y - 1) / d));
if ((n - y) % d == 0)
ans = min(ans, ((n - x) / d) + ((n - x) % d != 0) + ((n - y) / d));
cout << ((ans != 10000000000) ? ans : -1) << endl;
}
signed main() {
long long t;
cin >> t;
while (t--) sol();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int ans;
if (abs(y - x) % d == 0)
ans = abs(y - x) / d;
else {
if ((y - 1) % d == 0 || (n - y) % d == 0) {
int a = INT_MAX, b = INT_MAX;
if ((y - 1) % d == 0) a = int(ceil(1.0 * (x - 1) / d)) + (y - 1) / d;
if ((n - y) % d == 0) b = int(ceil(1.0 * (n - x) / d)) + (n - y) / d;
ans = min(a, b);
} else
ans = -1;
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if ((((x - y) % (d)) != 0) && (((n - y) % (d)) != 0) &&
(((y - 1) % (d)) != 0)) {
cout << -1 << '\n';
continue;
}
if (!(((x - y) % (d)) != 0)) {
cout << abs(x - y) / d << '\n';
continue;
}
int ans_front =
(((y - 1) % (d)) != 0) ? INT_MAX : (x - 1 + d - 1) / d + (y - 1) / d;
int ans_back =
(((n - y) % (d)) != 0) ? INT_MAX : (n - x + d - 1) / d + (n - y) / d;
cout << min(ans_back, ans_front) << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = int(2e9) + 99;
int n, x, y, d;
int dist(int x, int y) { return (abs(x - y) + (d - 1)) / d; }
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> x >> y >> d;
int len = abs(x - y);
int res = INF;
if (len % d == 0) {
res = min(res, dist(x, y));
}
len = y - 1;
if (len % d == 0) {
res = min(res, dist(x, 1) + dist(1, y));
}
len = n - y;
if (len % d == 0) {
res = min(res, dist(x, n) + dist(n, y));
}
if (res == INF) {
res = -1;
}
cout << res << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int ppr = 257;
const long long INF = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 9;
const int N = 1e6 + 123;
const long double pi = 3.141592653589793238462643;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
long long t;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long mn = inf;
if (abs(x - y) % d == 0) mn = abs(x - y) / d;
if (abs(1 - y) % d == 0) mn = min(mn, abs(y - 1) / d + x / d + (x % d > 0));
if (abs(n - y) % d == 0)
mn = min(mn, abs(n - y) / d + (n - x) / d + ((n - x) % d > 0));
if (mn == inf) mn = -1;
cout << mn << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long t, n, x, y, d, Ve1, Ven;
int main() {
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
long long res = 1e18;
if (abs(x - y) % d == 0) res = min(res, abs(x - y) / d);
if (abs(1 - x) % d == 0)
Ve1 = abs(1 - x) / d;
else
Ve1 = abs(1 - x) / d + 1;
if (abs(n - x) % d == 0)
Ven = abs(n - x) / d;
else
Ven = abs(n - x) / d + 1;
if (abs(1 - y) % d == 0) res = min(res, Ve1 + abs(1 - y) / d);
if (abs(n - y) % d == 0) res = min(res, Ven + abs(n - y) / d);
if (res == 1e18)
cout << "-1\n";
else
cout << res << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long int TESTS;
cin >> TESTS;
while (TESTS--) {
long long int n, x, y, d;
cin >> n >> x >> y >> d;
long long int req1 = n % d;
long long int req2 = x / d;
long long int req3 = y / d;
long long int ans1 = 1e17;
long long int ans2 = 1e17, ans;
if (x % d == y % d) {
ans = abs(x - y) / d;
} else if (y % d == req1 || y % d == 1) {
if (y % d == 1) {
ans1 = (x - 1) / d + (y - 1) / d;
if ((x - 1) % d != 0) ans1++;
}
if (y % d == req1) {
ans2 = (n - x) / d + (n - y) / d;
if ((n - x) % d != 0) ans2++;
}
ans = min(ans1, ans2);
} else {
ans = -1;
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int moth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
const int mod = 1e9 + 7;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, x, y, k;
cin >> n >> x >> y >> k;
if (x < y && (y - x) % k == 0) {
cout << (y - x) / k << endl;
continue;
} else if (x >= y && (x - y) % k == 0) {
cout << (x - y) / k << endl;
continue;
}
if ((y - 1) % k == 0 || (n - y) % k == 0) {
int a = 0x3f3f3f3f, b = 0x3f3f3f3f;
if ((y - 1) % k == 0)
a = (y - 1) / k + ((x - 1) % k == 0 ? (x - 1) / k : (x - 1) / k + 1);
if ((n - y) % k == 0)
b = (n - y) / k + ((n - x) % k == 0 ? (n - x) / k : (n - x) / k + 1);
cout << min(a, b) << endl;
} else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y, d, t, mn = 2e9;
int main() {
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%d%d%d%d", &n, &x, &y, &d);
if (abs(y - x) % d == 0)
printf("%d\n", abs(y - x) / d);
else if (((y - 1) % d == 0) || ((n - y) % d == 0)) {
int k = 2e9, j = 2e9;
if ((y - 1) % d == 0) {
k = 0;
k = x / d;
if (x % d) k++;
k += (y - 1) / d;
}
if ((n - y) % d == 0) {
j = 0;
int g = n - x;
j = g / d;
if (g % d) j++;
j += (n - y) / d;
}
mn = min(j, k);
printf("%d\n", mn);
} else
printf("-1\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1;
const int MOD = 1e9 + 7;
const int bm = 1;
int n, m;
void sinh() {
freopen(".inp", "w", stdout);
srand(time(0));
}
int main() {
ios_base::sync_with_stdio(0);
int t, first, second, d;
cin >> t;
while (t--) {
cin >> n >> first >> second >> d;
int MIN = MOD;
if ((second - first) % d == 0) MIN = abs(second - first) / d;
if (second % d == 1) MIN = min((first - 1) / d + (second - 1) / d + 1, MIN);
if ((n - second) % d == 0)
MIN =
min((n - first) / d + (n - second) / d + ((n - first) % d != 0), MIN);
if (MIN == MOD)
cout << -1;
else
cout << MIN;
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<string> v, v1;
map<string, long long> mp, mp1, mp2;
char final[100000];
long long a[10000];
bool compare(string &s1, string &s2) { return s1.size() < s2.size(); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, x, y, x1, y1, test, d, move, right, diff1, diff2, min1, min2,
min0;
cin >> test;
while (test--) {
cin >> n >> x >> y >> d;
x1 = min(x, y);
y1 = max(x, y);
if (x1 == y1) {
cout << "0\n";
} else {
right = (y1 - x1) / d;
if (d * right == y1 - x1) {
cout << right << "\n";
continue;
} else {
diff1 = x - 1;
diff2 = n - x;
min1 = -1;
min2 = -1;
move = ceil((double)diff1 / d);
x = 1;
right = (y - x) / d;
if (d * right == y - x) {
min1 = move + right;
}
move = ceil((double)diff2 / d);
x = y;
y = n;
right = (y - x) / d;
if (d * right == y - x) {
min2 = move + right;
}
if (min1 == -1 && min2 == -1) {
cout << "-1\n";
} else {
if (min1 != -1 && min2 != -1) {
cout << min(min1, min2) << "\n";
} else {
if (min1 != -1) {
cout << min1 << "\n";
} else {
cout << min2 << "\n";
}
}
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long tc;
cin >> tc;
long long n, x, y, d;
while (tc--) {
cin >> n >> x >> y >> d;
long long ans = 2ll * int(1e9);
if (x == y) {
ans = 0L;
}
if (abs(x - y) % d == 0ll) {
ans = min(ans, abs(x - y) / d);
}
if (abs(y - 1) % d == 0ll) {
long long val = (abs(y - 1) / d) + ceil((x - 1) / (double)d);
ans = min(ans, val);
}
if (abs(y - n) % d == 0ll) {
long long val = (abs(y - n) / d) + ceil(abs(x - n) / (double)d);
ans = min(ans, val);
}
if (ans == 2ll * int(1e9)) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n, x, y, d;
scanf("%d%d%d%d", &n, &x, &y, &d);
int ans = INF;
if (abs(y - x) % d == 0)
ans = abs(y - x) / d;
else {
if ((y - 1) % d == 0)
ans = min(ans, (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d);
if ((n - y) % d == 0)
ans = min(ans, (n - x) / d + ((n - x) % d != 0) + (n - y) / d);
}
if (ans == INF) ans = -1;
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int det = abs(x - y);
if (det % d == 0) {
cout << det / d << endl;
continue;
} else if (y == 1) {
cout << (x - 1 + d - 1) / d << endl;
continue;
} else if (y == n) {
cout << (n - x + d - 1) / d << endl;
continue;
}
if ((y - 1) % d != 0 && (n - y) % d != 0) {
cout << -1 << endl;
} else if ((y - 1) % d != 0) {
cout << (n - x + d - 1) / d + (n - y) / d << endl;
} else if ((n - y) % d != 0) {
cout << (x - 1 + d - 1) / d + (y - 1) / d << endl;
} else
cout << min((n - x + d - 1) / d + (n - y) / d,
(x - 1 + d - 1) / d + (y - 1) / d)
<< endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> vprime;
void SieveOfEratosthenes(int n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (int p = 2; p <= n; p++)
if (prime[p]) vprime.push_back(p);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long a, b, c, d, e, g, h, k, l, m, n, t, f,
f1 = 0, flag = 0, flag1 = 0, co = 0, co1 = 0, co2 = 0, sum = 0, sum1 = 0,
ma = 0, ma1 = 0, ma2 = 0, mi = 100000000000, mi1 = 100000000000,
mi2 = 100, mi3 = 1000;
long long co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0, mul = 1,
sum2 = 0;
long long arr[100001], arr1[100001] = {0};
long long a1, a2, b1, b2, c1, c2, a3, a4, b3, b4, b5, b6, m1, m2, k1, l1, m3,
m4, d1, d2;
double pi = 2 * acos(0.0);
char ch;
string str, str1, str2, str3;
set<long long> s1;
set<long long> s2;
set<long long> s3;
map<char, long long> mp1;
stack<long long> st;
int x[] = {1, -1, 1, -1, 2, 2, -2, -2};
int y[] = {2, 2, -2, -2, 1, -1, 1, -1};
cin >> t;
while (t--) {
cin >> a >> b >> c >> d;
if (abs(b - 1) % d == 0)
f = abs(b - 1) / d;
else
f = (abs(b - 1) / d) + 1;
if (abs(a - b) % d == 0)
g = abs(a - b) / d;
else
g = (abs(a - b) / d) + 1;
h = (abs(c - 1) / d) + f;
k = (abs(a - c) / d) + g;
e = abs(b - c);
if (e % d == 0) {
cout << e / d << endl;
} else if (abs(c - 1) % d == 0 && abs(a - c) % d == 0) {
cout << min(h, k) << endl;
} else if (abs(c - 1) % d == 0) {
cout << (abs(c - 1) / d) + f << endl;
} else if (abs(a - c) % d == 0) {
cout << (abs(a - c) / d) + g << endl;
} else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t, n, x, y, d, k, i, j;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0 || y == n || y == 1)
cout << (abs(x - y) + d - 1) / d << endl;
else if ((abs(y - n) % d == 0) && (abs(y - 1) % d == 0))
cout << min((abs(n - x) + d - 1) / d + abs(n - y) / d,
(abs(x - 1) + d - 1) / d + abs(y - 1) / d)
<< endl;
else if (abs(y - n) % d == 0)
cout << (abs(n - x) + d - 1) / d + abs(n - y) / d << endl;
else if (abs(y - 1) % d == 0)
cout << (abs(x - 1) + d - 1) / d + abs(y - 1) / d << endl;
else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int dis = abs(x - y);
if (dis % d == 0)
cout << dis / d << "\n";
else {
int mn = INT_MAX;
if (abs(y - 1) % d == 0) {
mn = min(mn, abs(y - 1) / d + (x + d - 1) / d);
}
if (abs(y - n) % d == 0) {
mn = min(mn, abs(y - n) / d + ((n - x) + d - 1) / d);
}
if (mn != INT_MAX)
cout << mn << "\n";
else
cout << -1 << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
long long x, y, n, d;
while (q--) {
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
continue;
}
int cnt = INT_MAX;
if ((y - 1) % d == 0) {
cnt = y / d + (x / d) + (x % d == 0 ? 0 : 1);
}
if ((n - y) % d == 0) {
cnt = min((n - y) / d + (n - x) / d + ((n - x) % d == 0 ? 0 : 1),
(long long)cnt);
}
if (cnt == INT_MAX)
cout << -1 << endl;
else
cout << cnt << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const long long INF = 0x3f3f3f3f3f3f3f;
int t;
long long n, x, y, d, m;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%I64d%I64d%I64d%I64d", &n, &x, &y, &d);
if (labs(x - y) % d == 0) {
cout << labs(x - y) / d << endl;
} else {
int l = (x - 1) / d;
if ((x - 1) % d) l++;
int r = (n - x) / d;
if ((n - x) % d) r++;
long long ans = INF;
if ((y - 1) % d == 0) {
ans = min(ans, l + (y - 1) / d);
}
if ((n - y) % d == 0) {
ans = min(ans, r + (n - y) / d);
}
if (ans != INF) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d, x, y;
int t;
cin >> t;
while (t--) {
cin >> n;
cin >> x;
cin >> y;
cin >> d;
long long e = n + 1;
if ((y - x) % d == 0) {
e = abs(y - x) / d;
}
long long c = n + 1, a = n + 1;
if ((y - 1) % d == 0) c = (x - 1 + d - 1) / d + (y - 1) / d;
if ((n - y) % d == 0) a = (n - y) / d + (n - x + d - 1) / d;
if (a == n + 1 && c == n + 1 && e == n + 1)
cout << -1 << endl;
else
cout << min(min(a, c), e) << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int q;
cin >> q;
while (q--) {
long long int n, a, b, d;
cin >> n >> a >> b >> d;
long long int z, x, y;
z = abs(a - b);
if (z % d == 0) {
z = z / d;
} else {
z = -1;
}
if ((b - 1) % d == 0) {
if ((a - 1) % d == 0) {
x = (a - 1) / d;
} else {
x = (a - 1) / d + 1;
}
x = x + (b - 1) / d;
} else {
x = -1;
}
if ((n - b) % d == 0) {
if ((n - a) % d == 0) {
y = (n - a) / d;
} else {
y = (n - a) / d + 1;
}
y = y + (n - b) / d;
} else {
y = -1;
}
if (z < 0 && x < 0 && y < 0) {
cout << -1;
} else if (z >= 0 && x >= 0 && y >= 0) {
cout << min(z, min(x, y));
} else if (z >= 0 && x >= 0) {
cout << min(z, x);
} else if (x >= 0 && y >= 0) {
cout << min(x, y);
} else if (z >= 0 && y >= 0) {
cout << min(z, y);
} else if (z >= 0) {
cout << z;
} else if (x >= 0) {
cout << x;
} else if (y >= 0) {
cout << y;
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, x, y, d;
cin >> t;
while (t--) {
int r = INT_MAX;
cin >> n >> x >> y >> d;
int a = (x - 1) % d == 0 ? (x - 1) / d : (x - 1) / d + 1;
int b = (n - x) % d == 0 ? (n - x) / d : (n - x) / d + 1;
if (abs(x - y) % d == 0) r = abs(x - y) / d;
if ((y - 1) % d == 0) r = min(r, a + (y - 1) / d);
if ((n - y) % d == 0) r = min(r, b + (n - y) / d);
if (r == INT_MAX)
cout << -1;
else
cout << r;
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long ceil(long long x, long long y) {
if (x % y)
return (x / y) + 1;
else
return (x / y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long t, n, x, y, d, temp;
cin >> t;
long long ans = LONG_MAX;
long long flag = 0;
while (t--) {
cin >> n >> x >> y >> d;
ans = LONG_MAX;
flag = 0;
temp = ceil(x - 1, d);
if (!((y - 1) % d)) {
temp += (y - 1) / d;
ans = min(temp, ans);
flag = 1;
}
if (!(abs(y - x) % d)) {
ans = min(ans, abs(y - x) / d);
flag = 1;
}
temp = ceil(n - x, d);
if (!((n - y) % d)) {
temp += (n - y) / d;
ans = min(temp, ans);
flag = 1;
}
if (flag == 0)
cout << -1 << endl;
else
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int mySolve(int n, int x, int y, int d) {
if (abs(y - x) % d == 0) {
return (abs(y - x)) / d;
} else {
long long temp1 = 2e9 + 123;
long long temp2 = 2e9 + 123;
bool one = false;
bool two = false;
if ((n - y) % d != 0) {
one = true;
} else {
temp1 = ((n - x) / d) + 1;
temp1 += (n - y) / d;
}
if ((y - 1) % d != 0) {
two = true;
} else {
temp2 = ((x - 1) / d) + 1;
temp2 += (y - 1) / d;
}
if (one && two) {
return -1;
}
return min(temp1, temp2);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n, x, y, d;
cin >> n >> x >> y >> d;
cout << mySolve(n, x, y, d) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, x, y, d, res1, res2, res3, f = 0;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
res1 = INT_MAX;
res2 = INT_MAX;
res3 = INT_MAX;
f = 0;
if (abs(x - y) % d == 0) {
res1 = abs(x - y) / d;
f = 1;
}
if ((y - 1) % d == 0) {
res2 = ((y - 1) / d) + (((x - 1) / d) + 1);
f = 1;
}
if ((n - y) % d == 0) {
res3 = ((n - y) / d) + (((n - x) / d) + 1);
f = 1;
}
if (f == 0) {
cout << -1 << endl;
} else {
cout << min(res1, min(res2, res3)) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 111;
const int maxn = 2e5 + 10;
const int maxq = 1e6 + 10;
const int alf = 26;
const int dlm = 1e9 + 7;
const int del = 10001;
const int manfi = 1e5;
string O[] = {"Yes", "No", "YES", "NO", "IMPOSSIBLE"};
int ON = 0;
vector<long long> st[10];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (x % d == y % d) {
cout << abs(x - y) / d;
} else {
if (1 == n % d && 1 == y % d) {
int a = (x - 1) / d + (((x - 1) % d) > 0);
int b = (n - x) / d + (((n - x) % d) > 0);
cout << min(a + abs(y - 1) / d, b + abs(y - n) / d);
} else if (y % d == 1) {
int a = (x - 1) / d + (((x - 1) % d) > 0);
cout << a + abs(y - 1) / d;
} else if (y % d == n % d) {
int b = (n - x) / d + (((n - x) % d) > 0);
cout << b + abs(y - n) / d;
} else {
cout << -1;
}
}
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
cin >> TESTS;
while (TESTS--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) {
cout << abs(y - x) / d << "\n";
continue;
}
long long l = 1000000007, r = 1000000007;
if ((n - y) % d == 0) {
l = (n - y) / d + (n - x + d - 1) / d;
}
if ((y - 1) % d == 0) {
r = (y - 1) / d + (x - 1 + d - 1) / d;
}
long long ans = min(l, r);
if (ans != 1000000007)
cout << ans << "\n";
else
cout << "-1\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int T, n, x, y, d, ans;
int main() {
cin >> T;
while (T--) {
cin >> n >> x >> y >> d;
ans = inf;
if (abs(y - x) % d == 0)
ans = abs(y - x) / d;
else {
if ((y - 1) % d == 0) ans = min(ans, (x - 1) / d + 1 + (y - 1) / d);
if ((n - y) % d == 0) ans = min(ans, (n - x) / d + 1 + (n - y) / d);
}
if (ans == inf) ans = -1;
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (long long i = 0; i < v.size(); ++i) os << v[i] << " ";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
for (auto it : v) os << it << " ";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
os << v.first << " " << v.second;
return os;
}
const long long mod = 1e9 + 7;
const long long inf = 2e18;
const long long ninf = -2e18;
long long pow(long long a, long long b, long long m) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
time_t t1, t2;
t1 = clock();
long long t;
cin >> t;
for (long long i = 0; i < t; i++) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long can = false;
long long ans = -1;
if (x > y) {
if ((x - y) % d == 0) {
can = true;
ans = (x - y) / d;
}
} else {
if ((y - x) % d == 0) {
can = true;
ans = (y - x) / d;
}
}
if (!can) {
if ((n - y) % d == 0) {
can = true;
if ((n - x) % d == 0) {
ans = (n - x) / d;
} else
ans = (n - x) / d + 1;
ans += (n - y) / d;
}
if ((y - 1) % d == 0) {
long long tans;
if ((x - 1) % d == 0) {
tans = (x - 1) / d;
} else {
tans = (x - 1) / d + 1;
}
tans += (y - 1) / d;
if (can) {
if (tans < ans) ans = tans;
} else
ans = tans;
}
}
cout << ans << endl;
}
t2 = clock();
cerr << endl << t2 - t1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, d, x, y;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0)
cout << abs(y - x) / d << endl;
else {
int ans1, ans2;
if ((y - 1) % d == 0)
ans1 = (y - 1) / d + (x - 1) / d + ((x - 1) % d != 0);
else
ans1 = inf;
if ((n - y) % d == 0)
ans2 = (n - y) / d + (n - x) / d + ((n - x) % d != 0);
else
ans2 = inf;
int ans = min(ans1, ans2);
if (ans == inf) ans = -1;
cout << ans << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long int n, x, y, d, m, g, h;
cin >> m;
for (int q = 1; q <= m; q++) {
cin >> n >> x >> y >> d;
g = (n - y) / d;
h = (y - 1) / d;
if ((y - x) % d == 0) {
cout << abs(y - x) / d << endl;
} else if ((y - 1) % d == 0 && (n - y) % d == 0) {
cout << min((n - x) / d + g + 1, (x - 1) / d + h + 1) << endl;
} else if ((y - 1) % d == 0 && (n - y) % d != 0) {
cout << (x - 1) / d + h + 1 << endl;
} else if ((y - 1) % d != 0 && (n - y) % d == 0) {
cout << (n - x) / d + g + 1 << endl;
} else {
cout << -1 << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int n, x, y, d;
int solve() {
int ret = INT_MAX;
if ((x - y) % d == 0) ret = min(ret, abs(x - y) / d);
if (y % d == 0) ret = min(ret, (x + d - 1) / d + y / d);
if (y % d == (n - 1) % d)
ret = min(ret, (n - 1 - x + d - 1) / d + (n - 1 - y) / d);
return ret == INT_MAX ? -1 : ret;
}
void run() {
scanf("%d%d%d%d", &n, &x, &y, &d), --x, --y;
printf("%d\n", solve());
}
int main() {
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); ++i) run();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
continue;
}
if (y % d != 1 && y % d != n % d) {
cout << -1 << endl;
continue;
}
int res1 = 2000000000;
int res2 = 2000000000;
if (y % d == 1) {
res1 = (x - 1 + d - 1) / d + (y - 1) / d;
}
if (y % d == n % d) {
res2 = (n - x + d - 1) / d + (n - y) / d;
}
cout << min(res1, res2) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int q, w, e, r;
scanf("%d %d %d %d", &q, &w, &e, &r);
int ch = 1234567890;
int xx = abs(e - w);
if (xx % r == 0) {
ch = min(ch, xx / r);
}
int gap = 0;
gap = (w - 1) / r;
if ((w - 1) % r) gap++;
if ((e - 1) % r == 0) {
ch = min(ch, gap + (e - 1) / r);
}
gap = (q - w) / r;
if ((q - w) % r) gap++;
if ((q - e) % r == 0) ch = min(ch, gap + (q - e) / r);
if (ch == 1234567890)
puts("-1");
else
printf("%d\n", ch);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, x, y, d, i, m, mint, t;
cin >> m;
for (i = 1; i <= m; i++) {
mint = 2000900003;
t = 0;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0 && abs(x - y) / d < mint) {
mint = abs(x - y) / d;
t = 1;
}
if ((y - 1) % d == 0 &&
(y - 1) / d + abs(x - 1) / d + min(abs(x - 1) % d, 1) < mint) {
mint = (y - 1) / d + abs(x - 1) / d + min(abs(x - 1) % d, 1);
t = 1;
}
if ((n - y) % d == 0 &&
(n - y) / d + abs(n - x) / d + min(abs(n - x) % d, 1) < mint) {
mint = (n - y) / d + abs(n - x) / d + min(abs(n - x) % d, 1);
t = 1;
}
if (t == 1)
cout << mint << '\n';
else
cout << -1 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, ans;
int main() {
int t, n, x, y, d;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
int ans = 1 << 30;
if (abs(x - y) % d == 0) {
ans = abs(x - y) / d;
}
if ((n - y) % d == 0) {
ans = min(ans, (n - y) / d + (n - x + d - 1) / d);
}
if ((y - 1) % d == 0) {
ans = min(ans, (y - 1) / d + (x - 1 + d - 1) / d);
}
if (ans == 1 << 30)
cout << "-1" << endl;
else
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long a = abs(x - y);
if (a % d == 0)
cout << a / d << "\n";
else {
long long ans = 0;
if ((y - 1) % d != 0 && (n - y) % d != 0)
cout << "-1"
<< "\n";
else if ((y - 1) % d != 0) {
if ((n - x) % d == 0)
ans += (n - x) / d;
else {
ans += (n - x) / d;
ans++;
}
ans += (n - y) / d;
cout << ans << "\n";
} else if ((n - y) % d != 0) {
if ((x - 1) % d == 0)
ans += (x - 1) / d;
else {
ans += (x - 1) / d;
ans++;
}
ans += (y - 1) / d;
cout << ans << "\n";
} else {
long long ans1 = 0;
if ((x - 1) % d == 0)
ans1 += (x - 1) / d;
else {
ans1 += (x - 1) / d;
ans1++;
}
ans1 += (y - 1) / d;
if ((n - x) % d == 0)
ans += (n - x) / d;
else {
ans += (n - x) / d;
ans++;
}
ans += (n - y) / d;
cout << min(ans, ans1) << "\n";
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int sum = 0, sum1 = 0;
int n, x, y, d;
cin >> n >> x >> y >> d;
if ((y - x) % d == 0 || (x - y) % d == 0) {
cout << abs(y - x) / d << endl;
continue;
} else {
if ((y - 1) % d && (n - y) % d) {
cout << -1 << endl;
continue;
} else {
if ((x - 1) % d) {
sum++;
}
sum += (x - 1) / d;
sum += (y - 1) / d;
if ((n - x) % d) {
sum1++;
}
sum1 += (n - x) / d;
sum1 += (n - y) / d;
if ((y - 1) % d == 0 && (n - y) % d == 0)
cout << min(sum, sum1) << endl;
else if ((y - 1) % d == 0)
cout << sum << endl;
else
cout << sum1 << endl;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int result = y % d;
if (x % d == result) {
cout << abs(y - x) / d << '\n';
} else if ((1 % d) == result or (n % d) == result) {
int temp_1 = INT_MAX, temp_2 = INT_MAX;
if ((1 % d) == result) {
if (x % d <= 1) {
temp_1 = x / d + (y - 1) / d;
} else {
temp_1 = 1 + x / d + (y - 1) / d;
}
}
if ((n % d) == result) {
if ((n - x) % d == 0) {
temp_2 = (n - x) / d + (n - y) / d;
} else {
temp_2 = 1 + (n - x) / d + (n - y) / d;
}
}
cout << min(temp_1, temp_2) << '\n';
} else {
cout << -1 << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
continue;
}
int opt = -1;
if ((y - 1) % d == 0) {
opt = (y - 1) / d + (x - 1) / d + ((x - 1) % d != 0);
}
if ((n - y) % d == 0) {
if (opt == -1)
opt = ((n - y) / d) + (n - x) / d + ((n - x) % d != 0);
else
opt = min(((n - y) / d + (n - x) / d + (int)((n - x) % d != 0)), opt);
}
cout << opt << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
long long n, x, y, d, t;
long long k(long long x, long long y) {
long long flag;
if (abs(x - y) % d == 0) {
flag = abs(x - y) / d;
} else
flag = abs(x - y) / d + 1;
return flag;
}
long long min(long long a, long long b) {
long long t = a;
if (a > b) t = b;
return t;
}
int main() {
long long flag = -1;
long long a = 0, b = 0;
scanf("%ld", &t);
while (t--) {
a = 9999999999;
b = 9999999999;
flag = -1;
scanf("%ld %ld %ld %ld", &n, &x, &y, &d);
if (abs(y - x) % d == 0) {
flag = abs(y - x) / d;
} else {
if ((y - 1) % d == 0) {
a = (y - 1) / d + k(x, 1);
}
if ((n - y) % d == 0) {
b = (n - y) / d + k(x, n);
}
flag = min(a, b);
}
if (flag == 9999999999)
printf("-1\n");
else
printf("%ld\n", flag);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x, y, d, t;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
long long ans = 2e18;
if (abs(y - x) % d == 0)
ans = min(abs(y - x) / d, ans), cout << ans << endl;
else {
long long cnt2 = 2e18, cnt1 = 2e18;
if (abs(y - 1) % d == 0) {
cnt1 = (x / d + (x % d == 0 ? 0 : 1) + abs(y - 1) / d);
}
if (abs(n - y) % d == 0) {
cnt2 = ((n - x) / d + ((n - x) % d == 0 ? 0 : 1) + abs(n - y) / d);
}
if (cnt1 == 2e18 && cnt1 == cnt2)
cout << -1 << endl;
else
cout << min(cnt1, cnt2) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t, n, x, y, d;
cin >> t;
while (t--) {
long long ans, left = 0, right = 0, x_left, x_right, leftres, rightres;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) {
ans = abs(y - x) / d;
} else {
if (abs(y - 1) % d == 0) {
left = 1;
leftres = abs(y - 1) / d + abs(x - 1) / d + 1;
}
if (abs(n - y) % d == 0) {
right = 1;
rightres = abs(n - y) / d + abs(n - x) / d + 1;
}
if (left) {
if (right == 0) {
ans = leftres;
} else {
ans = min(leftres, rightres);
}
} else {
if (right == 0) {
ans = -1;
} else {
ans = rightres;
}
}
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int num_cases;
scanf("%d", &num_cases);
for (; num_cases > 0; num_cases--) {
int n, x, y, d;
scanf("%d %d %d %d", &n, &x, &y, &d);
if (!(abs(y - x) % d)) {
printf("%d\n", abs(y - x) / d);
continue;
}
int l, r;
l = (y - 1) % d ? 0 : (y - 1) / d + ceil((double)(x - 1) / d);
r = (n - y) % d ? 0 : (n - y) / d + ceil((double)(n - x) / d);
if (l && r)
printf("%d\n", (l > r) ? r : l);
else if (l || r)
printf("%d\n", l ? l : r);
else
printf("%d\n", -1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6 + 1;
int n, x, y, d;
void solve() {
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) {
cout << abs(y - x) / d;
return;
}
int m = min((y - 1) % d == 0 ? (x - 1) / d + (y - 1) / d + 1 : INT_MAX,
(n - y) % d == 0 ? (n - x) / d + (n - y) / d + 1 : INT_MAX);
cout << (m == INT_MAX ? -1 : m);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
solve();
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x, y, d, t;
scanf("%lld", &t);
while (t--) {
scanf("%lld %lld %lld %lld", &n, &x, &y, &d);
if (abs(y - x) % d != 0 && abs(y - 1) % d != 0 && abs(n - y) % d != 0)
printf("-1\n");
else {
if (abs(y - x) % d == 0)
printf("%lld\n", abs(y - x) / d);
else {
if (abs(y - 1) % d == 0 && abs(n - y) % d == 0) {
printf("%lld\n",
min((x - 1) / d + ((x - 1) % d == 0 ? 0 : 1) + (y - 1) / d,
(n - x) / d + ((n - x) % d == 0 ? 0 : 1) + (n - y) / d));
} else if (abs(n - y) % d == 0)
printf("%lld\n",
(n - x) / d + ((n - x) % d == 0 ? 0 : 1) + (n - y) / d);
else {
printf("%lld\n",
(x - 1) / d + ((x - 1) % d == 0 ? 0 : 1) + (y - 1) / d);
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INFll = 1ll * 1000000100 * 1000000100;
const long double PI =
3.141592653589793238462643383279502884197169399375105820974944;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long ans = 1000000100;
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long temp;
long long diff = abs(y - x);
if (diff % d == 0) {
temp = diff / d;
ans = min(ans, temp);
}
long long diff2 = abs(n - y);
if (diff2 % d == 0) {
temp = (n - x + d - 1) / d;
temp += diff2 / d;
ans = min(ans, temp);
}
long long diff3 = abs(y - 1);
if (diff3 % d == 0) {
temp = (x - 1 + d - 1) / d;
temp += diff3 / d;
ans = min(ans, temp);
}
if (ans == 1000000100) {
cout << -1 << endl;
} else
cout << ans << endl;
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.