P2398 GCD SUM solution to a problem

Face questions

Quite interesting.

Set f [i] represents gcd (i, j) = the number i, g [i] represents a k | number gcd (i, j) of;

g[i]=(n/i)*(n/i);

g[i]=f[i]+f[2i]+f[3i]+...;

Therefore, f [i] = g [i] -f [2i] -f [3i] -f [4i] -......

#include <bits/stdc++.h>
#define int long long
using namespace std;
int f[100010];
signed main()
{
    int n;
    cin>>n;
    long long ans=0;
    for(int i=n;i>=1;i--){
        f[i]=(n/i)*(n/i);
        for(int j=2;j*i<=n;j++){
            f[i]-=f[j*i];
        }
        ans+=f[i]*i;
    }
    cout<<ans;
}

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11404790.html