AtCoder Beginner Contest 347

A - Divisible (abc347 A)

题目大意

给定n个数ai以及k,输出是 k的倍数的ai整除以 k的值。

解题思路

按照题意判断取模和求整除即可。

神奇的代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
while (n--) {
int a;
cin >> a;
if (a % k == 0)
cout << a / k << ' ';
}
cout << '\n';
return 0;
}


B - Substring (abc347 B)

题目大意

给定字符串s,问子串种类数。

解题思路

|s|只有 100,直接 O(n2)枚举子串丢进 set里,答案就是 set的大小。

长度大的话得后缀自动机。

神奇的代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
set<string> ans;
for (int i = 0; i < s.size(); ++i)
for (int j = i; j < s.size(); ++j) {
ans.insert(s.substr(i, j - i + 1));
}
cout << ans.size() << '\n';
return 0;
}


C - Ideal Holidays (abc347 C)

题目大意

一周前a天假期,后 b天工作日。

给定 n天的安排,问第一个安排定在哪天,使得每个安排都在假期。

解题思路

先让安排日期对a+b取模,剩下的问题就是, [0,a+b1]数轴上有一堆点,问能否有一个长度为 a的区间覆盖了所有点。

枚举覆盖的左端点,看与最右边的点的距离是否超过 a即可。

注意计算a+b+di会超 int范围。

神奇的代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
LL n, a, b;
cin >> n >> a >> b;
LL tot = a + b;
vector<LL> d;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
--x;
x %= tot;
d.push_back(x);
d.push_back(x + tot);
}
ranges::sort(d);
LL dis = numeric_limits<LL>::max();
for (int i = 0; i < n; i++) {
dis = min(dis, d[i + n - 1] - d[i] + 1);
}
if (dis <= a)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}


D - Popcount and XOR (abc347 D)

题目大意

给定a,b,c,输出一对 x,y,满足:

  • x<260,y<260
  • popcount(x)=a
  • popcount(y)=b
  • xy=c

popcount(x)即返回 x在二进制下1的个数。 是异或。

解题思路

构造题。

假设c在二进制下 1的个数为 cnt

那么这 cnt1要分配在 x,y里。假设分配了 l1xr1y,其中 l+r=cnt

此时 x还剩下 al1y还剩下 br1要分配,这些 1的分配需要在 时抵消掉。

所以应有 al=br。结合 l+r=cnt可以解得 l,r值。

over=a+bcnt2是分配给x,y1,以在 时抵消。

剩下的aoverbover是分配给 x,y,以凑成 c

因此就逐位遍历 c,如果是 1,则分配给 xy(看a,b>0),如果是 0,则都分配给 x,y(如果 over>0)

至于无解条件,一个是 a+b<c,一个是 a+bcnt不是偶数,还有的情况难以言说,比如 a=60,b=60,cnt=60,因为限定了x<260,y<260,没有多余的位置分配给用于抵消1。所以就最后再判断一下构造出来符不符合要求。

神奇的代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b;
LL c;
cin >> a >> b >> c;
int aa = a, bb = b;
auto popcount = [](LL x) {
int ret = 0;
while (x) {
ret += x & 1;
x >>= 1;
}
return ret;
};
int cc = popcount(c);
if (cc > a + b || ((a + b - cc) & 1)) {
cout << -1 << '\n';
return 0;
}
int over = (a + b - cc) / 2;
a -= over;
b -= over;
if (a < 0 || b < 0) {
cout << -1 << '\n';
return 0;
}
LL A = 0, B = 0;
for (int i = 0; i < 60; i++) {
if (c & (1LL << i)) {
if (a) {
A |= 1LL << i;
a--;
} else if (b) {
B |= 1LL << i;
b--;
} else {
assert(0);
}
} else if (over) {
A |= 1LL << i;
B |= 1LL << i;
over--;
}
}
if (popcount(A) != aa || popcount(B) != bb) {
cout << -1 << '\n';
return 0;
}
cout << A << ' ' << B << '\n';
return 0;
}


E - Set Add Query (abc347 E)

题目大意

初始n0的数组ai和空集合s。进行以下 q次操作。

每次操作给定一个 x

  • 如果 x在集合里,移除它,否则放入它。
  • 对于js,aj+=|s|

解题思路

朴素的模拟的复杂度是O(nq),考虑在这个过程,每个操作后,都要遍历一下集合里的元素,给数组a的对应位置相加。

当一个数x在集合 s里时,每个操作之后都会对 ax作贡献,直到它被移除集合。

每次操作都计算贡献的话,就是朴素的模拟,复杂度上限就是 O(nq)。要优化,就不能每次操作算贡献了。

注意到一个数 x做出的贡献是一个连续的操作区间,贡献值就是这个操作区间的 |s|的和。那我们可以维护一个关于操作顺序的 |s|的前缀和presum,当 x被移除时,我们就结算它对 ax的贡献:就一个前缀和的相减,这里需要记录下x何时放入的。

操作结束后,再对还在s里的元素结算下贡献就好了。

神奇的代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<LL> presum;
presum.push_back(0);
set<int> s;
vector<int> la(n, 0);
vector<LL> ans(n, 0);
for (int i = 1; i <= q; ++i) {
int x;
cin >> x;
--x;
if (s.count(x)) {
ans[x] += presum[i - 1] - presum[la[x] - 1];
s.erase(x);
} else {
la[x] = i;
s.insert(x);
}
presum.push_back(presum.back() + s.size());
}
for (auto& i : s) {
ans[i] += presum[q] - presum[la[i] - 1];
}
for (auto& i : ans) {
cout << i << ' ';
}
cout << '\n';
return 0;
}


F - Non-overlapping Squares (abc347 F)

题目大意

给定一个n×n的网格,问三个不重叠的 m×m的网格覆盖,和的最大值。

解题思路

<++>

神奇的代码


G - Grid Coloring 2 (abc347 G)

题目大意

<++>

解题思路

<++>

神奇的代码


posted @   ~Lanly~  阅读(516)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示