min25筛

时间复杂度\(O(\frac{n^{\frac{3}{4}}}{log(n)})\),空间\(O(\sqrt(n))\)
求\(\phi\)和\(\mu\)的前缀和

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
//#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=mul(ans,a,c);a=mul(a,a,c),b>>=1;}return ans;}

using namespace std;
//using namespace __gnu_pbds;

const ld pi=acos(-1);
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=1000000+10,inf=0x3f3f3f3f;

bool mark[N];
int prime[N],cnt;
ll g[N],h[N],id[2][N],val[N],sum[N],up,n;
ll S(ll x,int y)
{
    if(x<=1||prime[y]>x)return 0;
    int k=(x<=up)?id[0][x]:id[1][n/x];
    ll ret=g[k]-h[k]-sum[y-1]+y-1;
    for(int i=y;i<=cnt&&1ll*prime[i]*prime[i]<=x;i++)
    {
        ll t1=prime[i],t2=1ll*prime[i]*prime[i];
        for(int j=1;t2<=x;t1=t2,t2*=prime[i],j++)
            ret+=S(x/t1,i+1)*(t1/prime[i]*(prime[i]-1))+(t2/prime[i]*(prime[i]-1));
    }
    return ret;
}
ll S1(ll x,int y)
{
    if(x<=1||prime[y]>x)return 0;
    int k=(x<=up)?id[0][x]:id[1][n/x];
    ll ret=-h[k]+y-1;
    for(int i=y;i<=cnt&&1ll*prime[i]*prime[i]<=x;i++)
    {
        ll t1=prime[i],t2=1ll*prime[i]*prime[i];
        ret-=S1(x/t1,i+1);
    }
    return ret;
}
void init()
{
    up=sqrt(n);
    cnt=0;
    memset(mark,0,sizeof mark);
    for(int i=2;i<=up;i++)
    {
        if(!mark[i])prime[++cnt]=i,sum[cnt]=sum[cnt-1]+i;
        for(int j=1;j<=cnt&&i*prime[j]<=up;j++)
        {
            mark[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
    int m=0;
    for(ll i=1,j;i<=n;i=j+1)
    {
        j=n/(n/i);
        val[++m]=n/i;
        g[m]=(n/i)*(n/i+1)/2-1;
        h[m]=n/i-1;
        if(n/i<=up)id[0][n/i]=m;
        else id[1][i]=m;
    }
    for(int j=1;j<=cnt;j++)for(int i=1;i<=m&&1ll*prime[j]*prime[j]<=val[i];i++)
    {
        ll te=val[i]/prime[j];
        int k=(te<=up)?id[0][te]:id[1][n/te];
        g[i]-=1ll*(sum[j]-sum[j-1])*(g[k]-sum[j-1]);
        h[i]-=h[k]-j+1;
    }
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        if(n==0){puts("0 0");continue;}
        init();
        printf("%lld %lld\n",S(n,1)+1,S1(n,1)+1);
    }
    return 0;
}
/********************

********************/
上一篇:Codeforces 1206 D - Shortest Cycle


下一篇:Java Object-Oriented:day11 【 红包案例】