AtCoder AGC038 C-LCMs 题解
题目链接:https://agc038.contest.atcoder.jp/tasks/agc038_c?lang=en
题意:给定一个数组,求这个数组中所有数对的LCM之和。
分析:网上看到了很多反演的解法,但是本题也可以通过埃氏筛在\(O(nlnlnn)\)的复杂度下解决。大致做法就是根据\(a_i \leq 1000000\),我们得到\(gcd(a_i, a_j) \leq 1000000\),于是可以通过枚举gcd来解决本题。实现参考代码,埃氏筛的思路就是求出gcd的值在\([1,
1000000]\)范围内的所有\(pair<a_i, a_j>\)的乘积之和,并去重,最后每项再乘逆元即可。
AC代码:
#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define SIZE 1000100
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define int long long
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
const int mod = 998244353;
int n, inv[SIZE] = { 0,1 }, cnt[SIZE], tp, maxx = 0, ans[SIZE], res = 0;
signed main() {
io(); cin >> n;
rep(i, 1, n) {
cin >> tp;
cnt[tp]++;
maxx = max(maxx, tp);
}
rep(i, 2, maxx) inv[i] = (mod - mod / i) * inv[mod % i] % mod;
for (int i = maxx; i; --i) { //求出gcd为i的所有pair的乘积之和
int s1 = 0, s2 = 0;
for (int j = i; j <= maxx; j += i) {
s1 = (s1 + cnt[j] * j % mod) % mod;
s2 = (s2 + cnt[j] * j % mod * j % mod) % mod;
}
ans[i] = (s1 * s1 % mod - s2 + mod) % mod;
// s1 - s2 的作用是使得gcd为j的pair对数为 cnt * (cnt - 1)
for (int j = i + i; j <= maxx; j += i) {
ans[i] = (ans[i] - ans[j] + mod) % mod;
//去重,删除gcd为ik (k > 1)的pair
}
}
rep(i, 1, maxx) { //计算 ans[i] / i % mod
res = (res + ans[i] * inv[i] % mod) % mod;
}
cout << res * inv[2] % mod; //枚举了两遍gcd,res / 2
}
AtCoder AGC038 C-LCMs 题解的更多相关文章
- AtCoder ExaWizards 2019 简要题解
AtCoder ExaWizards 2019 简要题解 Tags:题解 link:https://atcoder.jp/contests/exawizards2019 很水的一场ARC啊,随随便便就 ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 153 题解
目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
- AtCoder Beginner Contest 184 题解
AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- AtCoder Beginner Contest 172 题解
AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...
- AtCoder Beginner Contest 169 题解
AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...
- AtCoder Beginner Contest 148 题解
目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...
随机推荐
- 使用Vue创建一个新项目
1.环境 保证已经安装好了node\npm\vue等工具,将路径设置为想要建立新项目的文件夹路径 2.关于npm与cnpm npm包管理器,是集成在node中的,node环境安装完成,npm包管理器也 ...
- CodeForces 1141A
https://vjudge.net/problem/CodeForces-1141A #include <bits/stdc++.h> using namespace std; int ...
- VS 2017 mscorlib.dll 加载元数据时发生严重错误,需要终止调试
VS 2017 mscorlib.dll 加载元数据时发生严重错误,需要终止调试 C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0. ...
- MySQL数据库重点监控指标
MySQL数据库重点监控指标 QPS queries per seconds 每秒中查询数量 show global status like 'Question%'; Queries/seconds ...
- linux命令 mv
后缀--backup=<备份模式>:若需覆盖文件,则覆盖前先行备份: -b:当文件存在时,覆盖前,为其创建一个备份: -f:若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目 ...
- (转)HDFS简介
转自:http://os.51cto.com/art/201212/369564.html
- python面试的100题(9)
17.python如何实现单例模式?请写出两种实现方式? 第一种方法:使用装饰器 def singleton(cls): instances = {} def wrapper(*args, **kwa ...
- asp.net使用wsdl文件调用接口,以及调用SSL接口报错“根据验证过程 远程证书无效”的处理
1.调用wsdl接口,首先需要将wsdl文件转换为cs文件: 进入VS 开发人员命令提示行,输入如下命令: c:/Program Files/Microsoft Visual Studio 8/VC& ...
- FLAG-回归C++,JAVA什么的等学校教吧
以后刷OJ还是写C++,昂啊! 除非我觉得JAVA更好用
- AcWing 897. 最长公共子序列
#include <iostream> #include <algorithm> using namespace std; ; int n, m; char a[N], b[N ...