# Codeforces Round #670 (Div. 2)

### B. Maximum Product

Code
// Author : heyuhhh
// Created Time : 2020/09/12 22:07:43
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
int n;
cin >> n;
vector<vector<int>> G(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u, --v;
G[u].push_back(v);
G[v].push_back(u);
}
vector<int> size(n), up(n);
int Max = INF, x = -1, y = -1;
function<void(int, int)> dfs = [&] (int u, int fa) {
up[u] = fa;
size[u] = 1;
for (auto& v : G[u]) if (v != fa) {
dfs(v, u);
size[u] += size[v];
}
int tot = 0;
int maxv = 0;
for (auto& v : G[u]) if (v != fa) {
tot += size[v];
maxv = max(maxv, size[v]);
}
maxv = max(maxv, n - 1 - tot);
if (Max > maxv) {
Max = maxv;
x = u, y = -1;
} else if (Max == maxv) {
y = u;
}
};
dfs(0, -1);
if (n & 1 || y == -1) {
cout << 2 << ' ' << up[1] + 1 << '\n';
cout << 2 << ' ' << up[1] + 1 << '\n';
return;
}

int lf;
for (auto v : G[x]) {
if (v != y) {
lf = v;
break;
}
}

cout << lf + 1 << ' ' << up[lf] + 1 << '\n';
cout << lf + 1 << ' ' << y + 1 << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### D. Three Sequences

Code
// Author : heyuhhh
// Created Time : 2020/09/13 09:42:29
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<ll> d(n);
ll sum = 0;
for (int i = 1; i < n; i++) {
d[i] = a[i] - a[i - 1];
if (d[i] >= 0)
sum += d[i];
}
auto get = [&] () {
ll res = sum + a[0];
if (res > 0)
return (res + 1) / 2;
return res / 2;
};
auto add = [&] (int p, int v) {
if (d[p] > 0) sum -= d[p];
d[p] += v;
if (d[p] > 0) sum += d[p];
};
cout << get() << '\n';
int q;
cin >> q;
while (q--) {
int l, r, x;
cin >> l >> r >> x;
--l, --r;
if (l)
else
a[0] += x;
if (r + 1 < n)
cout << get() << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}


### E. Deleting Numbers

• $$A\ a$$:表示询问集合中有多少个数为 $$a$$ 的倍数；
• $$B\ b$$:表示询问集合中有多少个数为 $$a$$ 的倍数，并且在集合中把这些数删去。
• $$C\ x$$，用于回答 $$x$$ 是多少。

Code
// Author : heyuhhh
// Created Time : 2020/09/13 10:20:40
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

int primes[N], tot;
bool vis[N];
void init(int n) {
for (int i = 2; i <= n; i++) {
if (!vis[i]) {
primes[++tot] = i;
}
for (int j = 1; j <= tot && primes[j] * i <= n; j++) {
vis[primes[j] * i] = true;
if (i % primes[j] == 0) {
break;
}
}
}
}

void run() {
int n;
cin >> n;
init(n);
int t = 1;
while (t * t <= n) ++t;

auto query = [&] (ll x, int op = 1) {
if (x > n) return 0;
if (op == 1)
cout << "B " << x << endl;
else
cout << "A " << x << endl;
cout << endl;
int y; cin >> y;
return y;
};

int p = 1;
int ans = 1;
for (; p <= tot && primes[p] < t; p++) {
query(primes[p]);
int x = query(primes[p]);
if (x) {
int now = primes[p] * primes[p];
for (int i = 2; now / primes[p] <= n; now *= primes[p]) {
if (!query(now)) {
ans *= now / primes[p];
break;
}
}
}
}
if (ans != 1) {
for (; p <= tot; p++) {
int x = query((ll)ans * primes[p]);
if (x > 0) {
ans *= primes[p];
break;
}
}
} else {
int B = 95;
int last = query(1, 2);
for (int cnt = 0; p <= tot; ++p) {
query(primes[p]);
if (++cnt == B || p == tot) {
int now = query(1, 2);
if (last - now != cnt) {
for (int i = p - cnt + 1; i <= p; i++) {
int x = query(primes[i]);
if (x) {
cout << "C " << ans * primes[i] << endl;
return;
}
}
}
last = now;
cnt = 0;
}
}
}

cout << "C " << ans << endl;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}

posted @ 2020-09-14 12:41  heyuhhh  阅读(229)  评论(0编辑  收藏  举报