Comet OJ - Contest # 8 magical function

 

 

Meaning of the questions:

 

 Ideas:

       First we observe play table obtained, f (x) is a prime factor decomposition after x Πpi ^ (ti / 2) 

    Reanalysis available, Σf (i) = n / (1 * 1) * 1 * 1 only for the number of + n / (2 * 2) * 2 * 2 + number of only ... 

       But the results timeout.

            Performing the analysis is available only for the number x is phi (x).

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=b;i>=a;i--)
using namespace std;
#define ll long long
const int N=3e5+5;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int T;
ll n;
ll f[10000010],ans,sum;
int main()
{
    T=rd();
    while(T--)
    {
        ans=0;sum=0;
        n=rd();
        int p=sqrt(n);
        dep(i,2,p)
        {
            int x=i+i;
            f[i]=n/(1ll*i*i);
            while(x<=p)
            {    
                f[i]-=f[x];
                x+=i;
            }
            ans=ans+1ll*f[i]*i;
            sum+=f[i];
        }
        printf("%lld\n",ans+n-sum); 
    }
}
View Code

 

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=b;i>=a;i--)
using namespace std;
#define ll long long
const int N=1e7+5;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int prime[N+10],phi[N+10],num[N+10],flag[N+10],l=0;
void isprime()
{
    phi[1]=1;
    for(int i=2;i<=N;i++)
    {
        if(!flag[i]) 
        {
            prime[++l]=i;
            phi[i]=i-1; 
        }
        for(int j=1;j<=l;j++)
        {
            if(1ll*prime[j]*i>N) break;
            phi[i*prime[j]]=phi[i]*(i%prime[j]?prime[j]-1:prime[j]);
            flag[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int T;
ll n;
ll f[10000010],ans,sum;
int main()
{
    isprime();
    T=rd();
    while(T--)
    {
        ans=0;sum=0;
        n=rd();
        int p=sqrt(n);
        dep(i,1,p)
        {
            int x=i+i;
            f[i]=n/(1ll*i*i);
            ans=ans+1ll*f[i]*phi[i];
        }
        printf("%lld\n",years); 
    } 
}
View Code

 

Guess you like

Origin www.cnblogs.com/The-Pines-of-Star/p/11334828.html