[更新中]Luogu P3911 最小公倍数之和 题解

Link

题意

求$\sum_{i=1}^N\sum_{j=1}^N lcm(A_i,A_j)$。

Code

#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;

#define re register
#define int long long 

struct FastIO{
    static const int S=1048576;
    char buf[S],*L,*R;int stk[20],Top;
    ~FastIO(){clear();}
    inline char nc(){return L==R&&(R=(L=buf)+fread(buf,1,S,stdin),L==R)?EOF:*L++;}
    inline void clear(){fwrite(buf,1,Top,stdout);Top=0;}
    inline void pc(char ch){Top==S&&(clear(),0);buf[Top++]=ch;}
    inline void endl(){pc('\n');}
    FastIO& operator >> (int& ret){
        ret=0;int f=1;char ch=nc();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=nc();}
        while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=nc();}
        ret*=f;return *this;
    }
    FastIO& operator >> (char* s){
        re int Len=0;re char ch=nc();
        while(ch!='\n'){
            *(s+Len)=ch;
            Len++;
            ch=nc();
        }
    }
    FastIO& operator << (int x){
        if(x<0){pc('-');x=-x;}
        do{stk[++stk[0]]=x%10;x/=10;}while(x);
        while(stk[0]) pc('0'+stk[stk[0]--]);
        return *this;
    }
    FastIO& operator << (char ch){pc(ch);return *this;}
    FastIO& operator << (string str){
        re int Len=str.size()-1;for(stk[0]=0;Len>=0;Len--) stk[++stk[0]]=str[Len];
        while(stk[0]) pc(stk[stk[0]--]);
        return *this;
    }
}fin,fout;
#define File freopen("A.in","r",stdin);freopen("A.out","w",stdout); 
//#define mod 998244353
const int N=50010;
int mob[N],vis[N],prime[N],a[N],g[N],ans,tot,n;
void Mobius(int n){
    memset(prime,0,sizeof(prime));
    memset(mob,0,sizeof(mob));
    memset(vis,0,sizeof(vis));
    tot=0;mob[1]=1; 
    for(int i=2;i<=n;i++){
        if(vis[i]==0) prime[tot++]=i,mob[i]=-1;
        for(int j=0;j<tot&&i*prime[j]<=n;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]) mob[i*prime[j]]=-mob[i];
            else{
                mob[i*prime[j]]=0;
                break ;
            }
        }
    }
}
signed main(){
//  File
    fin>>n;
    for(int x,i=1;i<=n;i++) fin>>x,a[x]++;
    for(int x=1;x<=N;x++){
        for(int i=1;i<=N/x;i++) g[x]+=i*a[i*x];
        g[x]*=g[x];
    }
    Mobius(N);
    for(int x=1;x<=N-10;x++){
        for(int i=1;i<=(N-10)/x;i++){
            ans+=i*i*x*g[x*i]*mob[i];
        }
    }
    fout<<ans<<'\n';
//  fclose(stdin);fclose(stdout);
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇