比赛链接:Here

AB水题,

C - Sum of gcd of Tuples (Easy)

题意:\(\sum_{a=1}^{K} \sum_{b=1}^{K} \sum_{c=1}^{K} g c d(a, b, c)\)

数据范围:\(1\le K\le 200\)

思路:因为 \(k\) 比较小,我们直接跑暴力即可

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
ll n; cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
ans += __gcd(__gcd(i, j), k);
cout << ans;
}

D - RGB Triplets

题意:给一个长度为N的字符串S,只包含'R','B','G'。求有多少个三元组 \((i,j,k)(1\le i<j<k\le N)\) 满足 \(S_{i} \neq S_{j}, S_{i} \neq S_{k}, S_{j} \neq S_{k}, j-i \neq k-j_{\circ}\)

数据范围:\(1\le N \le 4000\)

题解:先将满足 \(_{i} \neq S_{j}, S_{i} \neq S_{k}, S_{j} \neq S_{k}\) 的算出来,在减去 \(j-i \neq k-j\) 的数目。

总的显然等于 \(num(R)*num(B)*num(G)\) ,然后枚举两个端点,判断第三个端点是不是不同于这个两个端点的颜色。

const int N = 4e3 + 5;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n; string s;
cin >> n >> s;
int a = 0, b = 0, c = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == 'R') a += 1;
if (s[i] == 'G') b += 1;
if (s[i] == 'B') c += 1;
}
ll ans = 1ll * a * b * c;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
if (s[i] == s[j]) continue;
if (2 * j - i < n && s[i] != s[2 * j - i] && s[j] != s[2 * j - i]) ans--;
}
cout << ans;
}

E - Sum of gcd of Tuples (Hard)

题意:\(\sum_{a_{1}=1}^{K} \sum_{a_{2}=1}^{K} \ldots \sum_{a_{N}=1}^{K} g c d\left(a_{1}, a_{2}, \ldots, a_{N}\right)(\bmod 1 \mathrm{e} 9+7)\)

数据范围:\(2 \leq N \leq 10^{5}, 1 \leq K \leq 10^{5}\)

思路一:莫比乌斯反演化简

\[\begin{array}{l}
A n s=\sum_{a_{1}=1}^{K} \sum_{a_{2}=1}^{K} \ldots \sum_{a_{N}=1}^{K} g c d\left(a_{1}, a_{2}, \ldots, a_{N}\right) \\
=\sum_{i=1}^{K} \sum_{a_{1}=1}^{K} \sum_{a_{2}=1}^{K} \ldots \sum_{a_{N}=1}^{K} i\left[\operatorname{gcd}\left(a_{1}, a_{2}, \ldots, a_{N}\right)==i\right] \\
=\sum_{i=1}^{K} \sum_{a_{1}=1}^{\left\lfloor\frac{K}{i}\right\rfloor} \sum_{a_{2}=1}^{\left\lfloor\frac{K}{i}\right\rfloor} \ldots \sum_{a_{N}=1}^{\left\lfloor\frac{K}{i}\right\rfloor} i\left[\operatorname{gcd}\left(a_{1}, a_{2}, \ldots, a_{N}\right)==1\right] \\
=\sum_{i=1}^{K} \sum_{a_{1}=1}^{\left\lfloor\frac{K}{i}\right\rfloor} \sum_{a_{2}=1}^{\left\lfloor\frac{K}{i}\right\rfloor} \ldots \sum_{a_{N}=1}^{\left\lfloor\frac{K}{i}\right\rfloor} i \sum_{d=1}^{\left\lfloor\frac{K}{i}\right\rfloor} \mu(d)\left\lfloor\frac{d}{a_{1}}\right\rfloor\left\lfloor\frac{d}{a_{2}}\right\rfloor \ldots\left\lfloor\frac{d}{a N}\right\rfloor \\
=\sum_{i=1}^{K} i \sum_{d=1}^{\left\lfloor\frac{K}{\imath}\right\rfloor} \mu(d) \sum_{a_{1}=1}^{\left\lfloor\frac{K}{i d}\right\rfloor} \sum_{a 2=1}^{\left\lfloor\frac{K}{i d}\right\rfloor} \ldots \sum_{a N=1}^{\left\lfloor\frac{K}{i d}\right\rfloor} 1 \\
=\sum_{i=1}^{K} i \sum_{d=1}^{\left\lfloor\frac{K}{i}\right\rfloor} \mu(d)\left\lfloor\frac{K}{i d}\right\rfloor^{N} \\
=\sum_{T=1}^{K}\left\lfloor\frac{K}{T}\right\rfloor^{N} \sum_{d \mid T} \mu(d) *\left\lfloor\frac{T}{d}\right\rfloor(T=i d) \\
=\sum_{T=1}^{K}\left\lfloor\frac{K}{T}\right\rfloor^{N} \phi(T)
\end{array}
\]

由于 \(K\) 不大,预处理欧拉函数,直接遍历即可。\(K\) 大的话,整除分块加杜教筛(雾。

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int N = 1e5 + 5, MD = 1e9 + 7;
int pri[N], tot, phi[N];
bool p[N];
void init() {
p[1] = true, phi[1] = 1;
for (int i = 2; i < N; i++) {
if (!p[i]) pri[++tot] = i, phi[i] = i - 1;
for (int j = 1; j <= tot && i * pri[j] < N; j++) {
p[i * pri[j]] = true;
if (i % pri[j] == 0) {
phi[i * pri[j]] = phi[i] * pri[j];
break;
} else phi[i * pri[j]] = phi[i] * (pri[j] - 1);
}
}
}
ll qpow(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1, a = a * a % MD) if (b & 1) ans = ans * a % MD;
return ans;
}
void add(int &x, int y) { x += y; if (x >= MD) x -= MD;}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
init();
int n, k, ans = 0;
cin >> n >> k;
for (int i = 1; i <= k; i++)
add(ans, 1LL * qpow(k / i, n)*phi[i] % MD); cout << ans;
}

思路二:

学习了下官方题解:定义 \(f[i]\) 代表 \(\gcd\) 为 \(i\) 的个数,递推关系式:\(f[i]=\left\lfloor\frac{K}{i}\right\rfloor^{N}-\sum_{j>i, i \mid j} f[j]_{\circ}\)​

双重循环即可,里面那层循环的复杂度总和是个调和级数,\(\log\) 级别的。

const int N = 1e5 + 5, MD = 1e9 + 7;
int f[N];
ll qpow(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1, a = a * a % MD) if (b & 1) ans = ans * a % MD;
return ans;
}
void add(int &x, int y) {
x += y;
if (x >= MD) x -= MD;
if (x < 0) x += MD;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, k;
scanf("%d%d", &n, &k);
cin >> n >> k;
for (int i = k; i >= 1; i--) {
f[i] = qpow(k / i, n);
for (int j = 2 * i; j <= k; j += i)
add(f[i], -f[j]);
}
int ans = 0;
for (int i = 1; i <= k; i++)
add(ans, 1LL * f[i]*i % MD); cout << ans;
}

F - Select Half

题意:给一个长度为 \(N\) 的序列 \(A\),要求选 \(\left[\frac{N}{2}\right\rfloor\) 个数,且下标互不相邻,最大化它们的总和。

数据范围:\(2\le N\le 2\times 10^5\)

题解:对于选的数的下标, \(B_{1}, B_{2} \ldots, B_{\left\lfloor\frac{N}{2}\right\rfloor}\),可以发现 \(\sum_{i=2}^{\left\lfloor\frac{N}{2}\right\rfloor} B_{i}-B_{i-1}-2 \leq 2\)

因此定义 \(f[i][j]\)​ 代表选第 \(1~i\) 里面的数(必选第个 \(i\) 数),前面多空了 \(j\) 的最大值。

\[\left\{\begin{aligned}
f[i][0] &=f[i-2][0]+a[i] \\
f[i][1] &=\max (f[i-2][1], f[i-3][0])+a[i] \\
f[i][2] &=\max (f[i-2][2], f[i-3][1], f[i-4][0])+a[i]
\end{aligned}\right.
\]
const int N = 2e5 + 5;
int a[N];
ll f[N][3];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n; cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; i++)
for (int j = 0; j < 3; j++)
f[i][j] = -1e18; for (int i = 1; i <= 3; i++)
f[i][i - 1] = a[i]; f[3][0] = a[1] + a[3];
for (int i = 4; i <= n; i++) {
f[i][0] = f[i - 2][0] + a[i];
f[i][1] = max(f[i - 2][1], f[i - 3][0]) + a[i];
f[i][2] = max(f[i - 2][2], f[i - 3][1]) + a[i];
if (i > 4) f[i][2] = max(f[i][2], f[i - 4][0] + a[i]);
}
ll ans;
if (n & 1) ans = max(f[n][2], max(f[n - 1][1], f[n - 2][0]));
else
ans = max(f[n][1], f[n - 1][0]);
cout << ans;
}

AtCoder Beginner Contest 162 C~F的更多相关文章

  1. AtCoder Beginner Contest 238 A - F 题解

    AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...

  2. AtCoder Beginner Contest 162

    比赛链接:https://atcoder.jp/contests/abc162/tasks A - Lucky 7 #include <bits/stdc++.h> using names ...

  3. AtCoder Beginner Contest 131 Task F. Must Be Rectangular

    Score: 600 points Approach 固定横坐标 $x$,考虑横坐标为 $x$ 的竖直线上最多可以有几个点. Observations 若最初两条竖直线 $x_1$.$x_2$ 上都有 ...

  4. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  5. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  6. AtCoder Beginner Contest 154 题解

    人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...

  7. AtCoder Beginner Contest 153 题解

    目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...

  8. AtCoder Beginner Contest 177 题解

    AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...

  9. AtCoder Beginner Contest 161

    比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...

  10. 题解 AtCoder Beginner Contest 168

    小兔的话 欢迎大家在评论区留言哦~ AtCoder Beginner Contest 168 A - ∴ (Therefore) B - ... (Triple Dots) C - : (Colon) ...

随机推荐

  1. BAPI_ACC_DOCUMENT_POST 解决多行一次性供应商凭证导入问题

    POST 函数将一次性供应商 的信息放在抬头入参上,业务需要多个一次性供应商一起做凭证时,就满足不了. 抬头入参会把所有行的一次性给一样的名称. 想起之前做的 IF_EX_ACC_DOCUMENT~C ...

  2. The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission,iphone手机video标签报错

    The request is not allowed by the user agent or the platform in the current context, possibly becaus ...

  3. 浅析MySQL代价模型:告别盲目使用EXPLAIN,提前预知索引优化策略

    背景 在 MySQL 中,当我们为表创建了一个或多个索引后,通常需要在索引定义完成后,根据具体的数据情况执行 EXPLAIN 命令,才能观察到数据库实际使用哪个索引.是否使用索引.这使得我们在添加新索 ...

  4. STL常用函数

    STL简介 \(STL\)是\(Standard\) \(Template\) \(Library\)的简称,中文名称为标准模板库,从根本上讲, 就是各种\(STL\)容器的集合,容器可以理解为能够实 ...

  5. Markdown References

    每次写文章,用到都要查一下的Markdown语法 字体颜色 [Markdown笔记]设置字体颜色 字体颜色 Font colors Decrsiption Demonstration Effect 选 ...

  6. ROW_NUMBER 开窗函数优化方案(Oracle && PostgreSQL 性能比对)

    帮朋友优化一条很简单的窗口函数 ROW_NUMBER() OVER() , Oracle 迁移 PostgreSQL项目. 原始SQL和执行计划 STUDENT_BAK 表我模拟的数据,3千万行数据. ...

  7. Fiddler使用 抓取手机数据包及中文乱码解决方案

    https://blog.csdn.net/zyb2017/article/details/79260086

  8. ASR项目实战-任务队列在文件转写特性中的应用

    转写时长超出60秒的语音文件,业界的竞品通常会使用创建异步转写任务的方式来提供支持. 一个简单.直接的实现方案,即: 网关服务接收到来自客户的转写请求时,将任务信息持久化至任务队列中. 由算法服务的实 ...

  9. linux云服务器病毒处理

    阿里云服务器被挖矿病毒入侵,CPU跑满,需要先停止相关进程.为了根除病毒,还需要 解决系统的后门问题(这部分听从阿里云工程师的建议备份系统盘快照后重置系统,再通过快照恢复数据) 然而重置系统后依然存在 ...

  10. 9 个让你的 Python 代码更快的小技巧

    哈喽大家好,我是咸鱼 我们经常听到 "Python 太慢了","Python 性能不行"这样的观点.但是,只要掌握一些编程技巧,就能大幅提升 Python 的运 ...