裸模板题
但在洛谷提交可能会超时,在洛谷上提交了一晚上,找了一晚上哪里出问题了,直到最后把一份参考的AC代码再交一遍又T了之后,蓝瘦香菇。。!
大佬博客,讲的超详细
https://www.cnblogs.com/peng-ym/p/9446555.html
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<string.h>
#include<vector>
#if(__cplusplus == 201103L)
#include <unordered_map>
#include <unordered_set>
#else
#include <tr1/unordered_map>
#include <tr1/unordered_set>
namespace std
{
using std::tr1::unordered_map;
using std::tr1::unordered_set;
}
#endif
#include<tr1/unordered_map>
#include<set>
#include<stack>
#define debug cout<<"*"<<endl;
#define input(x) scanf("%d",&x)
#define output(x) printf("%d\n",x);
#define llinput(x) scanf("%lld",&x)
#define lloutput(x) printf("%lld\n",x);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef pair<char,char> pcc;
const int mod=1e9+7;
const int INF=1e9+7;
const int maxn=6000010;
const double PI=acos(-1);
const int N=3007;
unordered_map<int,int>mp_mu;
unordered_map<ll,ll> mp_phi;
bool vis[maxn];
int prime[maxn];
int cot;
int mu[maxn];
int phi[maxn];
int sum_mu[maxn];
ll sum_phi[maxn];
void pre(int N)
{
phi[1]=mu[1]=1;
for(int i=2;i<=N;i++)
{
if(!vis[i])
{
prime[++cot]=i;
mu[i]=-1;phi[i]=i-1;
}
for(int j=1;j<=cot&&prime[j]*i<=N;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else mu[i*prime[j]]=-mu[i],phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for(int i=1;i<=maxn;i++)sum_mu[i]=sum_mu[i-1]+mu[i],sum_phi[i]=sum_phi[i-1]+phi[i];
}
int djs_mu(int n)
{
if(n<=6000000)return sum_mu[n];
if(mp_mu[n])return mp_mu[n];
int ans=1;
for(int l=2,r;l>=0&&l<=n;l=r+1)
{
r=n/(n/l);
ans-=(r-l+1)*djs_mu(n/l);
}
return mp_mu[n]=ans;
}
ll djs_phi(ll n)
{
if(n<=6000000)return sum_phi[n];
if(mp_phi[n])return mp_phi[n];
ll ans=n*(n+1)/2;
for(ll l=2,r;l<=n;l=r+1)
{
r=n/(n/l);
ans-=(r-l+1)*djs_phi(n/l);
}
return mp_phi[n]=ans;
}
int main()
{
pre(6000000);
int t;
int n;
input(t);
while(t--)
{
input(n);
printf("%lld %d\n",djs_phi(n),djs_mu(n));
}
return 0;
}