51Nod 1575 Gcd and Lcm

Topic Portal
analysis:
If the direct guess this is a multiplicative function, then it should simply some of the (relative) of 2333
but we have to rigorous (
vigorously push formula:

\(~~~~\sum_{i=1}^{n}\sum_{j=1}^{i}\sum_{k=1}^{i}lcm(gcd(i,j),gcd(i,k))\)

OI skilled players should soon be able to launch this step (disorder), if this step can not be launched soon, it is recommended to do a little simple question consolidate what basis (= w =):

\(~~~~\sum_{i=1}^{n}\sum_{d_1|i}\sum_{d_2|i}lcm(d_1,d_2)\varphi(\frac{i}{d_1})\varphi(\frac{i}{d_2})\)

So that it is equal to \ (\ sum_ {i = 1
} ^ {n} f (i) \) and then back is extremely derived immortal
but this only YY buckle and lose disease brain is not, at this transfer big brother skywalkert the derivation of
Here Insert Picture Description
Here Insert Picture Description
OrzOrz
last one was a rolled-up stuff is a multiplicative function, so the \ (f (i) \) is a multiplicative function
OrzOrz
then:

\(f(i)=\prod f(p_i^{a_i})\)

I began to try to push back the original formula \ (f (the p-^ k) \) :

\(~~~~f(p^k)=\sum_{i=1}^{p^k}\sum_{j=1}^{p^k}lcm(gcd(p^k,i),gcd(p^k,j))\)
\(=\sum_{i=0}^{k}\sum_{j=0}^{k}p^{max(i,j)}\varphi(p^{k-i})\varphi(p^{k-j})\)
\(=\sum_{i=0}^{k}p^i\varphi(p^{k-i})(2\sum_{j=0}^{i-1}\varphi(p^{k-j})+\varphi(p^{k-i}))\)

Here's a small trick you can think about it
and then we can directly calculate the \ (\ varphi (the p-^ k) \) , with a simplification into the
process cumbersome, do not write here 2333, is used in geometric series summation the simple knowledge
and finally we launch the equation:

\(~~~~f(p^k)=(2k+1)(p^{2k}-p^{2k-1})+p^{k-1}\)
\(~~~~f(p)=3p^2-3p+1\)

We combine the following requirements min_25 screen:
1, \ (f (the p-) \) is a polynomial function
2, \ (f (the p-^ k) \) can be quickly calculated, where you can use fast power
then the next is happy min_25 screen the

\(\sum_{i=1}^{n}[i\in Prime]f(i)=3G_2(n,|P|)-3G_1(n,|P|)+G_0(n,|P|)\)

I do not know talking about sm can look at this
and then directly to the screen like
unsigned int this thing is disgusting, I do a lot of the details for a long time
some little trick directly looking at the code it

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iostream>
#include<map>
#include<string>

#define maxn 1000005
#define INF 0x3f3f3f3f
#define inv3 2863311531u

using namespace std;

inline unsigned int getint()
{
    unsigned int num=0,flag=1;char c;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
    while(c>='0'&&c<='9')num=num*10+c-48,c=getchar();
    return num*flag;
}

unsigned int N,S;
unsigned int pri[maxn],np[maxn],cnt,sumpri[maxn],sumpw[maxn];
unsigned int G0[maxn],G1[maxn],G2[maxn];
unsigned int num[maxn],tot;
unsigned int pos1[maxn],pos2[maxn];

inline void init()
{
    for(unsigned int i=2;i<maxn;i++)
    {
        if(!np[i])pri[++cnt]=i,sumpri[cnt]=sumpri[cnt-1]+i,sumpw[cnt]=sumpw[cnt-1]+i*i;
        for(unsigned int j=1;j<=cnt&&1ll*i*pri[j]<maxn;j++)
        {
            np[i*pri[j]]=1;
            if(i%pri[j]==0)break;
        }
    }
}

inline unsigned int ksm(unsigned int num,unsigned int k)
{
    unsigned int ret=1;
    for(;k;k>>=1,num=num*num)if(k&1)ret=ret*num;
    return ret;
}

inline unsigned int getf(unsigned int p,unsigned int k)
{return (2*k+1)*(ksm(p,2*k)-ksm(p,2*k-1))+ksm(p,k-1);}

inline unsigned int getsum(unsigned int n,unsigned int m)
{
    if(n<2||pri[m]>n)return 0;
    unsigned int k=(n<=S)?pos1[n]:pos2[N/n],ret=(3*G2[k]-3*G1[k]+G0[k])-(3*sumpw[m-1]-3*sumpri[m-1]+m-1);
    for(unsigned int i=m;i<=cnt&&1ll*pri[i]*pri[i]<=n;i++)
    {
        unsigned int tmp=pri[i];
        for(unsigned int j=1;1ll*tmp*pri[i]<=n;j++,tmp*=pri[i])
            ret+=(getsum(n/tmp,i+1)*getf(pri[i],j))+getf(pri[i],j+1);
    }
    return ret;
}

int main()
{
    init();
    int T=getint();
    while(T--)
    {
        tot=0;
        memset(pos1,0,sizeof pos1),memset(pos2,0,sizeof pos2);
        memset(G0,0,sizeof G0);memset(G1,0,sizeof G1);memset(G2,0,sizeof G2);
        memset(num,0,sizeof num);
        N=getint();
        S=(unsigned int)(sqrt(N));
        for(unsigned int i=1,j;i<=N;i=j+1)
        {
            j=N/(N/i);num[++tot]=N/i;
            G0[tot]=num[tot]-1;
            G1[tot]=(unsigned int)(1ull*num[tot]*(num[tot]+1)/2);G1[tot]--;
            G2[tot]=(unsigned int)(1ull*num[tot]*(num[tot]+1)*(2*num[tot]+1)/2*inv3);G2[tot]--;
            if(num[tot]<=S)pos1[num[tot]]=tot;
            else pos2[j]=tot;
        }
        for(unsigned int j=1;j<=cnt;j++)
            for(unsigned int i=1;i<=tot&&1ll*pri[j]*pri[j]<=num[i];i++)
            {
                unsigned int k=(num[i]/pri[j]<=S)?pos1[num[i]/pri[j]]:pos2[N/(num[i]/pri[j])];
                G0[i]-=G0[k]-(j-1);
                G1[i]-=pri[j]*(G1[k]-sumpri[j-1]);
                G2[i]-=pri[j]*pri[j]*(G2[k]-sumpw[j-1]);
            }
        printf("%u\n",getsum(N,1)+1);
    }
}

Guess you like

Origin www.cnblogs.com/Darknesses/p/12195827.html