题目链接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 题解的更多相关文章

  1. AtCoder ExaWizards 2019 简要题解

    AtCoder ExaWizards 2019 简要题解 Tags:题解 link:https://atcoder.jp/contests/exawizards2019 很水的一场ARC啊,随随便便就 ...

  2. AtCoder Beginner Contest 154 题解

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

  3. AtCoder Beginner Contest 153 题解

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

  4. AtCoder Beginner Contest 177 题解

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

  5. AtCoder Beginner Contest 184 题解

    AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...

  6. AtCoder Beginner Contest 173 题解

    AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...

  7. AtCoder Beginner Contest 172 题解

    AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...

  8. AtCoder Beginner Contest 169 题解

    AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...

  9. AtCoder Beginner Contest 148 题解

    目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...

随机推荐

  1. 二分-C - Pie

    C - Pie My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a n ...

  2. Python爬虫连载4-Error模块、Useragent详解

    一.error 1.URLError产生的原因:(1)没有网络:(2)服务器连接失败:(3)不知道指定服务器:(4)是OSError的子类 from urllib import request,err ...

  3. SpringBoot整合WEB开发--(六)CROS支持

    简介: CROS(Cross-Origin Resource Sharing)是由W3C制定的一种跨域资源共享技术标准,其目的为了解决前端的跨域请求,在JavaEE开发中,最常见的前端跨域请求解决方案 ...

  4. 与大神聊天1h

    与大神聊天1h 啊,与大神聊天1h真的是干货满满 解bug问题 之所以老出bug是因为我老是调用别人的包啊,在调参数的时候,并不知道内部机制 其实就自己写一个函数,然后能把功能实现就好了. 问题是,出 ...

  5. Java Set集合的详解

    一,Set Set:注重独一无二的性质,该体系集合可以知道某物是否已近存在于集合中,不会存储重复的元素 用于存储无序(存入和取出的顺序不一定相同)元素,值不能重复. 对象的相等性 引用到堆上同一个对象 ...

  6. 控制台输出Scanner和BufferedReader区别

    Scanner取得输入的依据是空格符,包括空格键,Tab键和Enter键.当按下这其中的任一键 时,Scanner就会返回下一个输入. 当你输入的内容中间包括空格时,显然,使用Scanner就不能完整 ...

  7. 素问 - 使用 PE、PB 做估值

    摘自<小韭的学习圈> Q 哪些行业用PE看合适,哪些用PB看合适啊?其中的大致逻辑是什么? A PE = 股价 / 每股收益 使用PE的逻辑是,我们认为一个股票有价值,是因为公司未来能赚钱 ...

  8. 【HTML】三种方法使HTML单页面输入密码才能访问

    方法一 <script type="text/javascript"> function password() { var testV = 1; var pass1 = ...

  9. PP: Time series clustering via community detection in Networks

    Improvement can be done in fulture:1. the algorithm of constructing network from distance matrix. 2. ...

  10. FPM 0.08不能运行破解办法……

    FPM_0.080.rar 破解办法:用UltraEdit打开FPM.exe,查找"33 C0 E9 F2"(注意中间有空格)将33改为8B,F2改为00