Codeforces Round #651 (Div. 2)

C. Number Game

$$n$$拆分为$$p_1^{q_1}\cdot p_2^{q_2}\cdots p_k^{q_k}$$

F2. The Hidden Pair (Hard Version)

$$F1$$就可以轻松A掉了，$$F2$$限制比较严格，我们需要减少二分的次数，首先我们容易发现$$l=\lceil\frac{len}{2}\rceil$$，那么可以提高下限。之后利用数据特点，$$l$$较小的情况$$r$$不可能大，$$r$$较大$$l$$不会小，所以$$r=min(r,len)$$。这样把二分区间上下限范围缩一下就行。

A. Maximum GCD
/*
* Author:  heyuhhh
* Created Time:  2020/6/21 7:30:37
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#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
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

void run() {
int n; cin >> n;
cout << n / 2 << '\n';
}

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

B. GCD Compression
/*
* Author:  heyuhhh
* Created Time:  2020/6/21 7:35:28
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#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
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e3 + 5;

void run() {
int n; cin >> n;
vector <int> a(2 * n);
for (int i = 0; i < 2 * n; i++) {
cin >> a[i];
}
for (int x = 2; x < N; x++) {
vector <vector<int>> v(N);
vector <pii> ans;
for (int i = 0; i < 2 * n; i++) {
int t = (a[i] % x);
if (sz(v[(x - t) % x])) {
ans.push_back(MP(v[(x - t) % x].back(), i + 1));
v[(x - t) % x].pop_back();
} else {
v[t].push_back(i + 1);
}
}
int cnt = 0;
for (int i = 0; i < N; i++) {
cnt += sz(v[i]);
}
if (cnt == 0 || cnt == 2) {
if (cnt == 0) ans.pop_back();
for (auto it : ans) {
cout << it.fi << ' ' << it.se << '\n';
}
return;
}
}
}

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

C. Number Game
/*
* Author:  heyuhhh
* Created Time:  2020/6/21 8:00:35
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#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
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

void first() {
cout << "Ashishgup" << '\n';
}
void second() {
cout << "FastestFinger" << '\n';
}

void run() {
int n; cin >> n;
if (n == 1) {
second();
return;
}
if (n & 1) {
first();
return;
}
int cnt = 0, cnt2 = 0;
while (n % 2 == 0) {
n /= 2;
++cnt;
}
for (int i = 3; 1ll * i * i <= n; i++) {
while (n % i == 0) {
n /= i;
++cnt2;
}
}
if (n > 1) ++cnt2;
if (cnt == 1) {
if (cnt2 != 1) {
first();
} else {
second();
}
} else {
if (cnt2) {
first();
} else {
second();
}
}
}

int main() {
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. Odd-Even Subsequence
/*
* Author:  heyuhhh
* Created Time:  2020/6/21 8:15:28
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#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
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

void run() {
int n, k; cin >> n >> k;
vector <int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}

auto check = [&] (int s, int x, int m) {
int cnt = 0;
for (int i = s; i < n; i++) {
if (a[i] <= x) {
++cnt, ++i;
if (cnt >= m) {
if (s && (k & 1)) {
if (i < n) return true;
return false;
}
if (!s && !(k & 1)) {
if (i < n) return true;
return false;
}
return true;
}
}
}
return false;
};
auto solve = [&] (int s, int m) {
//choose from s
int l = 1, r = INF, mid;
while (l < r) {
mid = (l + r) >> 1;
if (check(s, mid, m)) r = mid;
else l = mid + 1;
}
return r;
};
int ans = min(solve(0, (k + 1) / 2), solve(1, k / 2));
cout << ans << '\n';
}

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

E. Binary Subsequence Rotation
/*
* Author:  heyuhhh
* Created Time:  2020/6/21 8:59:29
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#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
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

void run() {
int n; cin >> n;
string s, t;
cin >> s >> t;
string ss = "";
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
ss += s[i];
}
}
sort(all(s)), sort(all(t));
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
cout << -1 << '\n';
return;
}
}
n = ss.length();
int ans = 0;
int p0 = 0, p1 = 0;
int ok0 = 0, ok1 = 0;
for (int i = 0; i < n; i++) {
if (ss[i] == '1') {
if (p0) {
++ok0;
--p0;
} else {
++p1;
if (ok1) {
--ok1;
} else {
++ans;
}
}
} else {
if (p1) {
++ok1;
--p1;
} else {
++p0;
if (ok0) {
--ok0;
} else {
++ans;
}
}
}
}
cout << ans << '\n';
}

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

F1. The Hidden Pair (Easy Version)
/*
* Author:  heyuhhh
* Created Time:  2020/6/21 11:36:18
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#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
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e3 + 5;

int n;
vector <int> G[N];

pii query(vector <int>& nodes) {
if (sz(nodes) == 0) {
while(true);
}
cout << "?" << ' ' << sz(nodes);
for (auto it : nodes) {
cout << ' ' << it;
}
cout << endl;
int x, d; cin >> x >> d;
return MP(x, d);
};

void answer(int s, int f) {
cout << "!" << ' ' << s << ' ' << f << endl;
string ss;
cin >> ss;
if (ss == "Incorrect") {
while(true);
}
}

vector <int> dep[N];
int deep[N];
int len;

void dfs(int u, int fa) {
deep[u] = deep[fa] + 1;
dep[deep[u]].push_back(u);
for (auto v : G[u]) if (v != fa) {
dfs(v, u);
}
}

pii get(int x) {
if (sz(dep[x]) == 0) {
return MP(-1, -1);
}
vector <int> nodes;
for (auto it : dep[x]) {
nodes.push_back(it);
}
return query(nodes);
}

void run() {
cin >> n;
for (int i = 1; i <= n; i++) {
G[i].clear();
}
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
vector <int> nodes;
for (int i = 1; i <= n; i++) {
nodes.push_back(i);
}
pii now = query(nodes);
int rt = now.fi;
len = now.se;
for (int i = 0; i <= n; i++) {
dep[i].clear();
}
deep[0] = -1;
dfs(rt, 0);
int l = 0, r = n + 1, mid;
int s, f;
while (l < r) {
mid = (l + r) >> 1;
pii now = get(mid);
if (now.se == len) {
l = mid + 1;
s = now.fi;
} else {
r = mid;
}
}
for (int i = 0; i <= n; i++) {
dep[i].clear();
}
dfs(s, 0);
now = get(len);
f = now.fi;
}

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

F2. The Hidden Pair (Hard Version)
/*
* Author:  heyuhhh
* Created Time:  2020/6/21 11:36:18
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#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
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e3 + 5;

int n;
vector <int> G[N];

pii query(vector <int>& nodes) {
if (sz(nodes) == 0) {
exit(0);
}
cout << "?" << ' ' << sz(nodes);
for (auto it : nodes) {
cout << ' ' << it;
}
cout << endl;
int x, d; cin >> x >> d;
return MP(x, d);
};

void answer(int s, int f) {
cout << "!" << ' ' << s << ' ' << f << endl;
string ss;
cin >> ss;
if (ss == "Incorrect") {
while(true);
}
}

vector <int> dep[N];
int deep[N], Max;
int len;

void dfs(int u, int fa) {
deep[u] = deep[fa] + 1;
Max = max(deep[u], Max);
dep[deep[u]].push_back(u);
for (auto v : G[u]) if (v != fa) {
dfs(v, u);
}
}

pii get(int x) {
if (sz(dep[x]) == 0) {
return MP(-1, -1);
}
vector <int> nodes;
for (auto it : dep[x]) {
nodes.push_back(it);
}
return query(nodes);
}

void run() {
cin >> n;
for (int i = 1; i <= n; i++) {
G[i].clear();
}
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
vector <int> nodes;
for (int i = 1; i <= n; i++) {
nodes.push_back(i);
}
pii now = query(nodes);
int rt = now.fi;
len = now.se;
for (int i = 0; i <= n; i++) {
dep[i].clear();
}
deep[0] = -1;
Max = 0;
int s, f;
dfs(rt, 0);
int l = (len + 1) / 2, r = min(len, Max) + 1, mid;
while (l < r) {
mid = (l + r) >> 1;
pii now = get(mid);
if (now.se == len) {
l = mid + 1;
s = now.fi;
} else {
r = mid;
}
}
for (int i = 0; i <= n; i++) {
dep[i].clear();
}
dfs(s, 0);
now = get(len);
f = now.fi;