CF1436F 题解

· · 题解

不难发现 \{B\} 其实是无用的,题目本质上就是求所有 \gcd_{x\in A}x=1 的 (|A|-1)(\sum_{x\in A}x)^2。

平凡地容斥一下(钦定 d|\gcd_{x\in A}x)。要求的是 \sum_{A\in B}(|A|-1)(\sum_{x\in A}x)^2,其中 B 为所有 d|a_i 组成的集合。

推个式子。

\sum_{A\in B}((|A|-1)(\sum_{x\in A}x^2)+(|A|-1)(\sum_{(x,y)\in A}2xy)) =\sum_{x\in B}x^2\sum_{x\in A\subset B}(|A|-1)+\sum_{(x,y)\in B}2xy\sum_{(x,y)\in A\subset B}(|A|-1) =2^{|B|-2}(|B|-1)\sum_{x\in B}x^2+2^{|B|-3}|B|\sum_{(x,y)\in B}2xy =2^{|B|-2}(|B|-1)\sum_{x\in B}x^2+2^{|B|-3}|B|((\sum_{x\in B}x)^2-\sum_{x\in B}x^2)

不难发现我们只需要对于每个 d 计算出 \sum_{d|i}freq_i\times a_i^k,其中 k=0,1,2 即可求出上式。最后容斥一下即可。

这边的容斥我写的是 n\ln n 的调和级数暴力,实现精细可以做到 n\log\log n,反正能过就行。

#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int mod=998244353;
//Combinatorics
int qp(int a,int b){
    if(b<0){
        return qp(qp(a,mod-2),-b);
    }
    int ans=1;
    while(b){
        if(b&1) (ans*=a)%=mod;
        (a*=a)%=mod;
        b>>=1;
    }
    return ans;
}
int fac[3000005],inv[3000005];
void init(){
    fac[0]=1; for(int i=1;i<=3000000;i++) fac[i]=fac[i-1]*i%mod;
    inv[3000000]=qp(fac[3000000],mod-2); for(int i=2999999;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
int C(int i,int j){
    if(i<0||j<0||i<j) return 0;
    return fac[i]*inv[j]%mod*inv[i-j]%mod;
}
//Fast input/output
int read(){
    char c; int ans=0; int z=1;
    while(!isdigit(c=getchar()))z-=2*(c=='-');
    do{ans=(ans*10+c-'0');}while(isdigit(c=getchar()));
    return ans*z;
}
void print(int x){
    if(x<0) putchar('-');
    if(llabs(x)>=10) print(llabs(x)/10);
    putchar((llabs(x)%10)+'0');
}
void endl(){
    putchar(10);
}
int pw0[100005],pw1[100005],pw2[100005],ans[100005];
signed main(){
    init();
    int m; cin>>m;
    for(int i=1;i<=m;i++){
        int a,fr; cin>>a>>fr;
        pw0[a]=fr;
        pw1[a]=a*fr%mod;
        pw2[a]=a*a%mod*fr%mod;
    }
    for(int i=1;i<=100000;i++) for(int j=i*2;j<=100000;j+=i) pw0[i]+=pw0[j],(pw1[i]+=pw1[j])%=mod,(pw2[i]+=pw2[j])%=mod;
    for(int i=1;i<=100000;i++) ans[i]=((pw0[i]-1)%mod*qp(2,pw0[i]-2)%mod*pw2[i]%mod+pw0[i]%mod*qp(2,pw0[i]-3)%mod*(pw1[i]*pw1[i]%mod+mod-pw2[i])%mod)%mod;
    for(int i=100000;i>=1;i--) for(int j=i*2;j<=100000;j+=i) (ans[i]+=mod-ans[j])%=mod;
    cout<<ans[1];
    return 0;
}