【莫比乌斯反演】C - LCMs - AGC038
题目链接(https://atcoder.jp/contests/agc038/tasks/agc038_c)
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : points
Problem Statement
We have an integer sequence of length : .Find the following sum ( denotes the least common multiple of and ):
Since the answer may be enormous, compute it modulo .
Constraints
All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the sum modulo .
Sample Input 1
3 2 4 6
Sample Output 1
22
.
Sample Input 2
8 1 2 3 4 6 8 12 12
Sample Output 2
313
Sample Input 3
10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644
Sample Output 3
353891724
题意
给定长度为的序列,求,模
思路
(笔记)
众所周知
设答案为,则可以由
间接知道大小
设
对作倍数和,相当于是一样的所以后面作平方
最终得到求和式
在这里,对于任意的的这样的一个求和式,可以通过枚举因子或者枚举倍数来求得,复杂度
然后就可以一步步求求和式:
对于求和式,第一项是求所有的是的倍数的求和, ,就可以把这里的求和看作,把用一个数组存起来,对这个数组进行倍数和,可以用的复杂度求取,用桶数组优化方便累加,即
然后就可以来求,又是类似于倍数和的一种形式,可以用的复杂度求取,即这样
最后可以用的复杂度求取
总复杂度
(嘤嘤嘤,被笨死惹,不想敲注释,名字和思路里的变量都对应的上吧)
AC代码
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define int long long
#define ull unsigned long long
#define PII pair<int,int>
#define endl '\n'
const int N = 1e6 + 100;
const int mod = 998244353;
const double pi = acos(-1.0);
typedef long long ll;
using namespace std;
int n;
int mx;
int a[N],pos[N];
int h1[N], h2[N];
int miu[N];
int prime[N], num;
bool vis[N];
int quickpow(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
int getinv(int a) {
return quickpow(a, mod - 2);
}
void init() {
mx = 0;
for (int i = 1; i <= n; i++) {
a[i] = 0;
}
return;
}
void solve() {
init();
for (int i = 1; i <= n; i++)
{
cin >> a[i];
pos[a[i]] = (pos[a[i]] + a[i]) % mod;
mx = max(mx, a[i]);
}
miu[1] = 1;
for (int i = 2; i <= 1e6; i++)
{
if (!vis[i]) prime[++num] = i, miu[i] = -1;
for (int j = 1; j <= num && i * prime[j] <= 1e6; j++)
{
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
miu[i * prime[j]] = -miu[i];
}
}
for (int d = 1; d <= 1e6; d++)
{
int d_inv = getinv(d);
for (int dd = d; dd <= 1e6; dd += d)
{
h1[d] = (h1[d] + pos[dd]) % mod;
h2[dd] = (h2[dd] + miu[dd / d] * d_inv % mod + mod) % mod;
}
}
int ans = 0;
for (int d = 1; d <= mx; d++) {
ans = (ans + h1[d] * h1[d] % mod * h2[d] % mod) % mod;
}
ans = ((ans - h1[1]) % mod + mod) % mod;
cout << ans * getinv(2) % mod;
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while (cin >> n) {
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用