CodeForces Round #951(Div. 2) 补题记录(A~E)
A
容易发现对于任意一个长度为
很显然 Bob 希望自己选取的数的
因此只需要枚举所有长度为
B
考虑打表猜结论。从低到高枚举
因为
时间复杂度为
auto main() [[O3]] -> signed {
// ios_base::sync_with_stdio(0);
// cin.tie(0);
int T;
cin >> T;
while (T--) {
int x, y;
cin >> x >> y;
F(i, 0, inf) {
if ((x >> i & 1) != (y >> i & 1)) {
cout << (1ll << i) << '\n';
break;
}
}
}
}
C
又是猜结论题。设
时间复杂度为
auto main() [[O3]] -> signed {
// ios_base::sync_with_stdio(0);
// cin.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
F(i, 1, n) cin >> a[i].x, a[i].id = i;
// stable_sort(a + 1, a + n + 1, [&](auto l, auto r) {
// return l.x < r.x;
// });
int res = a[1].x;
F(i, 2, n) res = ll(res, a[i].x);
F(i, 1, n) b[i] = res / a[i].x;
int s = accumulate(b + 1, b + n + 1, 0ll);
bool ok = true;
F(i, 1, n) {
if (s >= b[i] * a[i].x) {
ok = false;
break;
}
}
if (ok) {
F(i, 1, n) {
cout << b[i] << ' ';
}
cout << '\n';
} else {
cout << "-1\n";
}
}
}
D
首先考虑暴力求解。枚举每一个
考虑优化上面的暴力。首先观察得到上面的这个操作其实就是让字符串
容易发现这个字符串其实可以被分割为
然后再观察
容易发现一个
,以此类推。令生成的这个字符串为 。 ,以此类推。令生成的这个字符串为 。
然后就可以去做这个题目啦。首先考虑字符串的前半部分
此时还(可能)在第二个字符串前缀中剩下
容易发现
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define em emplace_back
#define F(i,x,y) for(int i=x;i<=y;i++)
#define G(i,x,y) for(int i=x;i>=y;i--)
#define W(G,i,x) for(auto&i:G[x])
#define W_(G,i,j,x) for(auto&[i,j]:G[x])
#define add(x,y) z[x].em(y)
#define add_(x,y) add(x,y),add(y,x)
#define Add(x,y,d) z[x].em(y,d)
#define Add_(x,y,z) Add(x,y,z),Add(y,x,z);
#ifdef int
#define inf (7611257611378358090ll/2)
#else
#define inf 0x3f3f3f3f
#endif
using namespace std;
int ll(int a, int b) {
return a / __gcd(a, b) * b;
}
const int N = 400100;
int s1[N], s2[N];
struct Hash {
int h1, h2, h3, h4;
bool operator==(const Hash &r) const {
return h1 == r.h1 && h2 == r.h2 && h3 == r.h3 && h4 == r.h4;
}
} ;
const int m1 = 1e9 + 9, m2 = 998244353, m3 = 998244853, m4 = 19260817;
Hash hh1[N], hh2[N], hh3[N], hh4[N];
int bit1[N], bit2[N], bit3[N], bit4[N];
char ss[N], st[N];
Hash get(Hash *h, int l, int r) {
int v1, v2, v3, v4;
v1 = h[r].h1 - h[l - 1].h1 * bit1[r - l + 1] % m1, v1 %= m1; v1 += m1; v1 %= m1;
v2 = h[r].h2 - h[l - 1].h2 * bit2[r - l + 1] % m2, v2 %= m2; v2 += m2; v2 %= m2;
v3 = h[r].h3 - h[l - 1].h3 * bit3[r - l + 1] % m3, v3 %= m3; v3 += m3; v3 %= m3;
v4 = h[r].h4 - h[l - 1].h4 * bit4[r - l + 1] % m4, v4 %= m4; v4 += m4; v4 %= m4;
return {v1, v2, v3, v4};
}
signed main() {
int T;
cin >> T;
bit1[0] = bit2[0] = bit3[0] = bit4[0] = 1;
F(i, 1, N - 1) {
bit1[i] = bit1[i - 1] * 131 % m1;
bit2[i] = bit2[i - 1] * 1331 % m2;
bit3[i] = bit3[i - 1] * 13331 % m3;
bit4[i] = bit4[i - 1] * 133331 % m4;
}
while (T--) {
int n, m;
cin >> n >> m;
string s;
cin >> s;
s = ' ' + s;
for (int i = 0; i <= n + 5; i++) {
hh1[i].h1 = hh1[i].h2 = hh1[i].h3 = hh1[i].h4 = hh2[i].h1 = hh2[i].h2 = hh2[i].h3 = hh2[i].h4 = hh3[i].h1 = hh3[i].h2 = hh3[i].h3 = hh3[i].h4 = hh4[i].h1 = hh4[i].h2 = hh4[i].h3 = hh4[i].h4 = s1[i] = s2[i] = 0;
}
for (int i = 1; i <= n; i++) {
if ((i - 1) % (2 * m) < m) {
ss[i] = '1';
st[i] = '0';
} else {
ss[i] = '0';
st[i] = '1';
}
}
for (int i = 1; i <= n; i++) {
s1[i] = s1[i - 1];
if (s[i] == '1') {
s1[i]++;
}
hh1[i].h1 = hh1[i - 1].h1 * 131 + s[i]; hh1[i].h1 %= m1;
hh1[i].h2 = hh1[i - 1].h2 * 1331 + s[i]; hh1[i].h2 %= m2;
hh1[i].h3 = hh1[i - 1].h3 * 13331 + s[i]; hh1[i].h3 %= m3;
hh1[i].h4 = hh1[i - 1].h4 * 133331 + s[i]; hh1[i].h4 %= m4;
}
for (int i = n; i; i--) {
s2[i] = s2[i + 1];
if (s[i] == '1') {
s2[i]++;
}
hh2[n - i + 1].h1 = hh2[n - i + 1 - 1].h1 * 131 + s[i]; hh2[n - i + 1].h1 %= m1;
hh2[n - i + 1].h2 = hh2[n - i + 1 - 1].h2 * 1331 + s[i]; hh2[n - i + 1].h2 %= m2;
hh2[n - i + 1].h3 = hh2[n - i + 1 - 1].h3 * 13331 + s[i]; hh2[n - i + 1].h3 %= m3;
hh2[n - i + 1].h4 = hh2[n - i + 1 - 1].h4 * 133331 + s[i]; hh2[n - i + 1].h4 %= m4;
}
for (int i = 1; i <= n; i++) {
hh3[i].h1 = hh3[i - 1].h1 * 131 + ss[i]; hh3[i].h1 %= m1;
hh3[i].h2 = hh3[i - 1].h2 * 1331 + ss[i]; hh3[i].h2 %= m2;
hh3[i].h3 = hh3[i - 1].h3 * 13331 + ss[i]; hh3[i].h3 %= m3;
hh3[i].h4 = hh3[i - 1].h4 * 133331 + ss[i]; hh3[i].h4 %= m4;
hh4[i].h1 = hh4[i - 1].h1 * 131 + st[i]; hh4[i].h1 %= m1;
hh4[i].h2 = hh4[i - 1].h2 * 1331 + st[i]; hh4[i].h2 %= m2;
hh4[i].h3 = hh4[i - 1].h3 * 13331 + st[i]; hh4[i].h3 %= m3;
hh4[i].h4 = hh4[i - 1].h4 * 133331 + st[i]; hh4[i].h4 %= m4;
}
int id = -1;
for (int i = 1; i <= n; i++) {
if (i == n || s[i + 1] == '1') {
int len = n - i;
if (get(hh1, i + 1, n) == get(hh3, 1, len)) {
int remainlen = (m - len % m) % m;
if (remainlen && ss[len + 1] == '1' || !remainlen && ss[len + 1] == '0') {
if (s1[i] - s1[i - remainlen] == remainlen) {
if (get(hh2, n - (i - remainlen) + 1, n) == get(hh4, 1, i - remainlen)) {
id = i;
break;
}
}
} else {
if (s1[i] - s1[i - remainlen] == 0) {
if (get(hh2, n - (i - remainlen) + 1, n) == get(hh3, 1, i - remainlen)) {
id = i;
break;
}
}
}
}
} else {
int len = n - i;
if (get(hh1, i + 1, n) == get(hh4, 1, len)) {
int remainlen = (m - len % m) % m;
if (st[len] == '1') {
if (s1[i] - s1[i - remainlen] == remainlen) {
if (get(hh2, n - (i - remainlen) + 1, n) == get(hh4, 1, i - remainlen)) {
id = i;
break;
}
}
} else {
if (s1[i] - s1[i - remainlen] == 0) {
if (get(hh2, n - (i - remainlen) + 1, n) == get(hh3, 1, i - remainlen)) {
id = i;
break;
}
}
}
}
}
}
cout << id << '\n';
}
}
E
首先根据一场 ABC E 题的套路,将曼哈顿距离转化为切比雪夫距离:
- 若当前点的坐标为
,则新的点的坐标为 。
容易证明这样的转化是正确的。此时
于是问题就变成了:找出三个点的坐标
- 有两对点的
坐标的距离为 ,另外一对点的 坐标的距离为 。 - 有两对点的
坐标的距离为 ,另外一对点的 坐标的距离为 。
此时必须要有两个点的
考虑枚举相同的
将所有的 set
里。查询的时候枚举 set
中是否存在
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define em emplace_back
#define F(i,x,y) for(int i=x;i<=y;i++)
#define G(i,x,y) for(int i=x;i>=y;i--)
#define W(G,i,x) for(auto&i:G[x])
#define W_(G,i,j,x) for(auto&[i,j]:G[x])
#define add(x,y) z[x].em(y)
#define add_(x,y) add(x,y),add(y,x)
#define Add(x,y,d) z[x].em(y,d)
#define Add_(x,y,z) Add(x,y,z),Add(y,x,z);
#ifdef int
#define inf (7611257611378358090ll/2)
#else
#define inf 0x3f3f3f3f
#endif
using namespace std;
int ll(int a, int b) {
return a / __gcd(a, b) * b;
}
const int N = 400100;
struct Point {
int x, y;
} a[N];
void $() {
int n, d;
cin >> n >> d;
F(i, 1, n) {
int x, y;
cin >> x >> y;
a[i] = {x + y, x - y};
}
map<int, set<pair<int, int>>> mp;
F(i, 1, n) {
mp[a[i].x].insert({a[i].y, i});
}
for (auto &[x, se] : mp) {
if (mp.count(x + d)) {
for (auto &[y, id] : se) {
auto it = se.lower_bound({y + d, 0ll});
if (it != se.end() && it->first == y + d) {
auto it_ = mp[x + d].lower_bound({y, 0ll});
if (it_ != mp[x + d].end() && it_->first <= y + d) {
cout << id << ' ' << it->second << ' ' << it_->second << '\n';
return;
}
}
}
}
if (mp.count(x - d)) {
for (auto &[y, id] : se) {
auto it = se.lower_bound({y + d, 0ll});
if (it != se.end() && it->first == y + d) {
auto it_ = mp[x - d].lower_bound({y, 0ll});
if (it_ != mp[x - d].end() && it_->first <= y + d) {
cout << id << ' ' << it->second << ' ' << it_->second << '\n';
return;
}
}
}
}
}
for (int i = 1; i <= n; i++) {
swap(a[i].x, a[i].y);
}
mp.clear();
F(i, 1, n) {
mp[a[i].x].insert({a[i].y, i});
}
for (auto &[x, se] : mp) {
if (mp.count(x + d)) {
for (auto &[y, id] : se) {
auto it = se.lower_bound({y + d, 0ll});
if (it != se.end() && it->first == y + d) {
auto it_ = mp[x + d].lower_bound({y, 0ll});
if (it_ != mp[x + d].end() && it_->first <= y + d) {
cout << id << ' ' << it->second << ' ' << it_->second << '\n';
return;
}
}
}
}
if (mp.count(x - d)) {
for (auto &[y, id] : se) {
auto it = se.lower_bound({y + d, 0ll});
if (it != se.end() && it->first == y + d) {
auto it_ = mp[x - d].lower_bound({y, 0ll});
if (it_ != mp[x - d].end() && it_->first <= y + d) {
cout << id << ' ' << it->second << ' ' << it_->second << '\n';
return;
}
}
}
}
}
cout << "0 0 0\n";
}
auto main() [[O3]] -> signed {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
$();
}
}
本文来自博客园,作者:yhbqwq,转载请注明原文链接:https://www.cnblogs.com/yhbqwq/p/18236581,谢谢QwQ
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· 深度对比:PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版