最小公倍数之和(莫比乌斯反演P3911)

发布时间:2024年01月19日

路径:

P3911 最小公倍数之和 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

思路:

代码:

?#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
const int N = 5e4 + 100;
#define LL long long
const LL mod = 998244353;
int ?cn, pre[N], su[N], mu[N], n;
LL F[N];
LL ans;
string s;
void into()
{
?? ?mu[1] = su[1] = su[0] = 1;
?? ?for (int i = 2; i <N; i++)
?? ?{
?? ??? ?if (!su[i]) pre[++cn] = i, mu[i] = -1;
?? ??? ?for (int j = 1; j <= cn && (LL)pre[j] * i <N; j++)
?? ??? ?{
?? ??? ??? ?su[pre[j] * i] = 1;
?? ??? ??? ?if (i % pre[j] == 0) break;
?? ??? ??? ?mu[pre[j] * i] = -mu[i];
?? ??? ?}
?? ?}
}
int main() {
?? ?cin >> n;
?? ?into();
?? ?int ma = 0;
?? ?for (int i = 1; i <= n; i++)
?? ?{
?? ??? ?int x;
?? ??? ?cin >> x;
?? ??? ?ma = max(ma, x);
?? ??? ?for (int i = 1; i * i <= x; i++)
?? ??? ?{
?? ??? ??? ?if (x % i == 0)?
?? ??? ??? ?{
?? ??? ??? ??? ?F[i] += x;
?? ??? ??? ??? ?if (i * i != x) F[x / i] += x;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?for (int d = 1; d <=ma; d++)
?? ?{
?? ??? ?LL cnt = 0;
?? ??? ?for (int x = d; x <= ma; x += d)
?? ??? ??? ?if (mu[x / d]) cnt += mu[x / d] * F[x] * F[x];
?? ??? ?ans += cnt / d;
?? ?}
?? ?cout << ans << endl;
?? ?return 0;
}

文章来源:https://blog.csdn.net/yusen_123/article/details/135687518
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。