Lcms(莫比乌斯反演)

题目路径:

https://www.luogu.com.cn/problem/AT_agc038_c

思路:

代码:

 #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 = 1e6 + 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];
        }
    }
}
LL quick(LL a, LL b,LL mod)
{
    LL ans = 1;
    while (b)
    {
        if (b & 1) ans = ans * a % mod;
        b = b >> 1;
        a = a * a % mod;
    }
    return ans;
}
int main() {
    cin >> n;
    into();
    int ma = 0;
    LL w = 0;
    for (int k = 1; k <= n; k++)
    {
        int x;
        cin >> x;
        w = (w + x)%mod;
        ma = max(ma, x);
        for (int i = 1; i * i <= x; i++)
        {
            if (x % i == 0)
            {
                F[i] =(F[i]+x)%mod;
                if (i * i != x) F[x / i] =(F[x/i]+ x)%mod;
            }
        }
    }
    for (int d = 1; d <= ma; d++)
    {
        LL cnt = 0;
        for (int x = d; x <= ma; x += d)
            if (mu[x / d]) cnt =(cnt+mu[x/d]*F[x]*F[x])%mod;
        ans = (ans+cnt*quick(d,mod-2,mod))%mod;
    }
    cout << (((ans-w)*quick(2,mod-2,mod))%mod+mod)%mod<< endl;
    return 0;
}

  • 34
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值