Codeforces Round #720 (Div. 2)
Codeforces Round #720 (Div. 2)
A - Nastia and Nearly Good Numbers
int main() {
IOS;
for (cin >> _; _; --_) {
ll a, b; cin >> a >> b;
if (b == 1) cout << "NO\n";
else if (b != 2) cout << "YES\n" << a << ' ' << a * (b - 1) << ' ' << a * b << '\n';
else cout << "YES\n" << a << ' ' << a * 3 << ' ' << 2 * a * b << '\n';
}
return 0;
}
B - Nastia and a Good Array
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n;
rep (i, 1, n) cin >> a[i]; cout << n - 1 << '\n';
int k = min_element(a + 1, a + 1 + n) - a; a[0] = a[k];
rep (i, 1, k - 2) cout << i << ' ' << k << ' ' << a[i - 1] + 1 << ' ' << a[k] << '\n', a[i] = a[i - 1] + 1;
if (k - 1) {
a[k - 1] = a[k - 2];
while (__gcd(a[k - 1], a[k - 2]) != 1 || __gcd(a[k - 1], a[k]) != 1) ++a[k - 1];
cout << k - 1 << ' ' << k << ' ' << a[k - 1] << ' ' << a[k] << '\n';
}
rep (i, k + 1, n) cout << i << ' ' << k << ' ' << a[i - 1] + 1 << ' ' << a[k] << '\n', a[i] = a[i - 1] + 1;
}
return 0;
}
C - Nastia and a Hidden Permutation
先用\(\left \lfloor \frac{n}{2} \right \rfloor + 1\) 次找到1
再用\(n - 1\) 把剩下的数找出来
故题目给你\(\left \lfloor \frac{3 \times n}{2} \right \rfloor\)
int ask(int x, int y, int z, bool f) {
if (!f) cout << "? 1 " << x << ' ' << y << ' ' << z << endl;
else cout << "? 2 " << x << ' ' << y << ' ' << z << endl;
cin >> x; return x;
}
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; int id;
for (int i = 1; i <= n; i += 2)
if (i + 1 <= n) {
int c = ask(i, i + 1, 1, 1);
if (c == 1) { a[i] = 1; id = i, break; }
else if (c == 2 && ask(i + 1, i, 1, 1) == 1) { a[i + 1] = 1; id = i + 1; break; }
}
else { id = i; break; }
rep (j, 1, n) if (id ^ j) a[j] = ask(id, j, n - 1, 0);
cout << "! ";
rep (i, 1, n) cout << a[i] << ' '; cout << endl;
}
return 0;
}
D - Nastia Plays with a Tree
dfs遍历, 节点有1个孩子不用拆, 有两个拆成下来, 成一条链, 有多个拆成两个在拆下来
VI h[N];
vector<PII> a, b;
int dfs(int x, int fa) {
int l = 0, r = 0;
for (auto &y : h[x]) if (y ^ fa) {
int cur = dfs(y, x);
if (!cur) continue;
if (r) a.pb(x, y), b.pb(y, cur);
else if (!l) l = cur;
else if (!r) r = cur;
}
return l ? r && fa ? a.pb(fa, x), b.pb(l, r), 0 : l : x;
}
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; vector<PII>().swap(a); vector<PII>().swap(b);
rep(i, 1, n) VI().swap(h[i]);
rep(i, 1, n - 1) { int u, v; cin >> u >> v; h[u].pb(v); h[v].pb(u); }
m = dfs(1, 0); cout << a.size() << '\n';
rep (i, 0, (int)a.size() - 1)
cout << a[i].fi << ' ' << a[i].se << ' ' << m << ' ' << b[i].fi << '\n', m = b[i].se;
}
return 0;
}