SP5971 LCMSUM number theory

Face questions

To this topic we ask:

\[\sum_{i=1}^n lcm(i,n)\]

The beginning of the formula:

\[\sum_{i=1}^{n} \frac{i*n}{gcd(i,n)}\]

\[\sum_{d|n} \sum_{i=1}^{\frac{n}{d}} i*n[gcd(i,\frac{n}{d})=1]\]

\[n*\sum_{d|n}\sum_{i=1}^{d}i[gcd(i,d)=1]\]

Note that the \ (\ sum_ {i = 1 } ^ {d} i [gcd (i, d) = 1] \) is seeking \ ([1, d] \ ) in all \ (D \) coprime and the number can be proved if (d> 1 \) \ , it is equal to \ (\ FRAC * {D \ Phi (D)} {2} \) , demonstrated as follows:

For each \ (I \) , and if it is \ (D \) coprime, the \ (DI \) also \ (D \) coprime, each pair \ (I \) and \ (DI \) and for the \ (D \) , and so the average \ (D \) prime number value \ (\ FRAC} {2} {D \) , a total of \ (\ phi (d) \ ) and a \ (D \) prime number, and so is their \ (\ frac {d * \ phi (d)} {2} \)

With the \ (1 \) prime number and apparently \ (1 \)

So the formula can be turned into

\[n*\big((\sum_{d|n,d>1}\frac{d*\phi(d)}{2})+1\big)\]

\[ \big( \frac{n}{2}*\sum_{d|n,d>1}d*\phi(d)\big)+n\]

Set \ (G (n-) = \ sum_ {D | n-D} * \ Phi (D) \) , into the equation:

\[\frac{n}{2}*(g(n)-1)+n\]

Wherein \ (g (n) \) is a multiplicative function, can be \ (O (n) \) screened out

Always ask is \ (O (1) \) of

The total time complexity is \ (O (n + T) \)

Screening out how line \ (G (n-) \) :

\ (n-\) is a prime number: \ (G (n-) +. 1 = n-* \ Phi (n-). 1 + n-* = (. 1-n-) \)

\(n=p^k\)

\[g(n)=1+\sum_{i=1}^{k}p^i*\phi(p^i)\]

\[=1+\sum_{i=1}^{k}p^i*p^{i-1}*(p-1)\]

\[=1+(p-1)\sum_{i=1}^{k}p^{2i-1}\]

\[=1+(p-1)\frac{p^{2k+1}-p}{p^2-1}\]

\[ =1+\frac{p^{2k+1}-p}{p+1}\]

\ (n-\) minimum quality factor \ (P \) : \ (G (n-) = G (n-'* ^ P K) = G (n-') * G (K ^ P) \)

Then you can screen a linear

Code:

#include<bits/stdc++.h>
using namespace std;
#define N 1000007
#define ll long long
const int lim=1e6;
int pr[N],cnt,pk[N];
bool zhi[N];
ll f[N];
void Init()
{
    int i,j;
    f[1]=1;
    for(i=2;i<=lim;i++)
    {
        if(!zhi[i])
        {
            pr[++cnt]=i,f[i]=1+1ll*i*(i-1);
            pk[i]=i;
        }
        for(j=1;j<=cnt&&i*pr[j]<=lim;j++)
        {
            int p=pr[j],x=i*p;
            zhi[x]=true;
            if(i%p==0)
            {
                pk[x]=pk[i]*p;
                f[x]=f[x/pk[x]]*(1+(1ll*pk[x]*pk[x]*p-p)/(p+1));
                break;
            }
            pk[x]=p;
            f[x]=f[i]*f[p];
        }
    }
    for(i=1;i<=lim;i++)
        f[i]=1ll*(f[i]-1)*i/2+i;
}
int main()
{
    int t,n;
    Init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%lld\n",f[n]);
    }
    return 0;
}

I had approach is to use \ (\ mu \) violent demolition \ ([gcd (i, d) = 1] \) , but to do that complexity is \ (O (n \ log n ) \) , but also Without this ingenious approach, not talked about here.

Guess you like

Origin www.cnblogs.com/lishuyu2003/p/11261022.html