QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#551857 | #9251. Graph Changing | ucup-team4435# | WA | 0ms | 3636kb | C++20 | 8.4kb | 2024-09-07 18:33:59 | 2024-09-07 18:34:00 |
Judging History
answer
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
const ll INF = 2e18;
const int INFi = 1e9;
const int LG = 49;
const int N = 1e4 + 4;
template<typename T>
int normalize(T value, int mod) {
if (value < -mod || value >= 2 * mod) value %= mod;
if (value < 0) value += mod;
if (value >= mod) value -= mod;
return value;
}
template<int mod>
struct static_modular_int {
using mint = static_modular_int<mod>;
int value;
static_modular_int() : value(0) {}
static_modular_int(const mint &x) : value(x.value) {}
template<typename T, typename U = std::enable_if_t<std::is_integral<T>::value>>
static_modular_int(T value) : value(normalize(value, mod)) {}
template<typename T>
mint power(T degree) const {
degree = normalize(degree, mod - 1);
mint prod = 1, a = *this;
for (; degree > 0; degree >>= 1, a *= a)
if (degree & 1)
prod *= a;
return prod;
}
mint inv() const {
return power(-1);
}
mint &operator=(const mint &x) {
value = x.value;
return *this;
}
mint &operator+=(const mint &x) {
value += x.value;
if (value >= mod) value -= mod;
return *this;
}
mint &operator-=(const mint &x) {
value -= x.value;
if (value < 0) value += mod;
return *this;
}
mint &operator*=(const mint &x) {
value = int64_t(value) * x.value % mod;
return *this;
}
mint &operator/=(const mint &x) {
return *this *= x.inv();
}
friend mint operator+(const mint &x, const mint &y) {
return mint(x) += y;
}
friend mint operator-(const mint &x, const mint &y) {
return mint(x) -= y;
}
friend mint operator*(const mint &x, const mint &y) {
return mint(x) *= y;
}
friend mint operator/(const mint &x, const mint &y) {
return mint(x) /= y;
}
mint &operator++() {
++value;
if (value == mod) value = 0;
return *this;
}
mint &operator--() {
--value;
if (value == -1) value = mod - 1;
return *this;
}
mint operator++(int) {
mint prev = *this;
value++;
if (value == mod) value = 0;
return prev;
}
mint operator--(int) {
mint prev = *this;
value--;
if (value == -1) value = mod - 1;
return prev;
}
mint operator-() const {
return mint(0) - *this;
}
bool operator==(const mint &x) const {
return value == x.value;
}
bool operator!=(const mint &x) const {
return value != x.value;
}
bool operator<(const mint &x) const {
return value < x.value;
}
template<typename T>
explicit operator T() {
return value;
}
friend std::istream &operator>>(std::istream &in, mint &x) {
std::string s;
in >> s;
x = 0;
for (const auto c: s)
x = x * 10 + (c - '0');
return in;
}
friend std::ostream &operator<<(std::ostream &out, const mint &x) {
return out << x.value;
}
static int primitive_root() {
if constexpr (mod == 1'000'000'007) return 5;
if constexpr (mod == 998'244'353) return 3;
if constexpr (mod == 786433) return 10;
static int root = -1;
if (root != -1)
return root;
std::vector<int> primes;
int value = mod - 1;
for (int i = 2; i * i <= value; i++)
if (value % i == 0) {
primes.push_back(i);
while (value % i == 0)
value /= i;
}
if (value != 1) primes.push_back(value);
for (int r = 2;; r++) {
bool ok = true;
for (auto p: primes) {
if ((mint(r).power((mod - 1) / p)).value == 1) {
ok = false;
break;
}
}
if (ok) return root = r;
}
}
};
constexpr int MOD = 1'000'000'007;
// constexpr int MOD = 998'244'353;
using mint = static_modular_int<MOD>;
vector<vi> GetNext(const vector<vi> &d, int k) {
int n = d.size();
vector<vi> g(n, vi(n, INFi));
rep(i, n) {
rep(j, n) {
if (d[i][j] >= k) {
g[i][j] = 1;
}
}
}
rep(t, n) {
rep(i, n) {
rep(j, n) {
g[i][j] = min(g[i][j], g[i][t] + g[t][j]);
}
}
}
rep(i, n)rep(j, n) if (g[i][j] == INFi) g[i][j] = -1;
return g;
}
void solve() {
int q;
cin >> q;
vector<vector<vvi>> ans;
{
ans.emplace_back();
ans.emplace_back();
int k = 3;
for (int n = 2; n < 3 * k - 1; n++) {
vector<vi> start(n, vi(n, -1));
rep(i, n - 1) start[i][i + 1] = start[i + 1][i] = 1;
ans.emplace_back();
for (int t = 0;; ++t) {
bool ok = false;
rep(i, n)rep(j, n) if (start[i][j] != -1) ok = true;
if (!ok) break;
ans.back().push_back(start);
start = GetNext(start, k);
}
}
}
rep(_, q) {
ll t, n, k, x, y;
cin >> t >> n >> k >> x >> y;
x--;
y--;
if (k == 2 && n >= 4) {
t %= 2;
}
if (t == 0) {
cout << abs(x - y) << '\n';
continue;
}
if (x - k < 0 && x + k >= n) {
cout << "-1\n";
continue;
}
if (y - k < 0 && y + k >= n) {
cout << "-1\n";
continue;
}
if (t == 1) {
if (abs(x - y) >= k) {
cout << "1\n";
} else if (x >= k && y >= k) {
cout << "2\n";
} else if (x + k < n && y + k < n) {
cout << "2\n";
} else {
cout << "3\n";
}
continue;
}
assert(t >= 2);
if (k == 1) {
cout << "1\n";
continue;
}
if (k == 2) {
assert(n <= 3);
cout << "-1\n";
continue;
}
assert(k > 2);
if (k >= 4) {
cout << "-1\n";
continue;
}
assert(k == 3);
if (n >= 3 * k - 1) {
cout << "-1\n";
continue;
}
if (t >= ans[n].size()) {
cout << "-1\n";
continue;
}
cout << ans[n][t][x][y] << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
int t = 1;
// cin >> t;
rep(i, t) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
input:
5 1 5 3 2 4 1 10 4 2 4 2 10 5 2 4 1 3 2 1 3 1 3 2 1 2
output:
3 2 -1 1 -1
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
30 1 2 1 1 2 1 2 2 1 2 1 2 3 1 2 1 2 4 1 2 1 2 5 1 2 1 2 6 1 2 2 2 1 1 2 2 2 2 1 2 2 2 3 1 2 2 2 4 1 2 2 2 5 1 2 2 2 6 1 2 3 2 1 1 2 3 2 2 1 2 3 2 3 1 2 3 2 4 1 2 3 2 5 1 2 3 2 6 1 2 4 2 1 1 2 4 2 2 1 2 4 2 3 1 2 4 2 4 1 2 4 2 5 1 2 4 2 6 1 2 5 2 1 1 2 5 2 2 1 2 5 2 3 1 2 5 2 4 1 2 5 2 5 1 2 5 2 6 1 2
output:
1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1
result:
ok 30 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
90 1 3 1 1 2 1 3 1 1 3 1 3 1 2 3 1 3 2 1 2 1 3 2 1 3 1 3 2 2 3 1 3 3 1 2 1 3 3 1 3 1 3 3 2 3 1 3 4 1 2 1 3 4 1 3 1 3 4 2 3 1 3 5 1 2 1 3 5 1 3 1 3 5 2 3 1 3 6 1 2 1 3 6 1 3 1 3 6 2 3 2 3 1 1 2 2 3 1 1 3 2 3 1 2 3 2 3 2 1 2 2 3 2 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