【bzoj3944】Sum

  • 本文为博主原创,未经许可不得转载

两个板子合在一起就行了

时限非常的卡

放板子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
#define FILE "read"
#define MAXM 100100
#define MAXN 4000100
using namespace std;
typedef long long ll;
int N,cnt,check[MAXN],prime[MAXN],vis_phi[MAXM],vis_mu[MAXM];
ll n,mu[MAXN],phi[MAXN],value_phi[MAXM],value_mu[MAXM];
inline ll read(){
ll x=0,f=1; char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
void pre(){
N=4000000; mu[1]=1; phi[1]=1;
for(int i=2;i<=N;++i){
if(!check[i]) prime[++cnt]=i,phi[i]=i-1,mu[i]=-1;
for(int j=1;j<=cnt&&i*prime[j]<=N;++j){
check[prime[j]*i]=1;
if(i%prime[j]==0) {phi[prime[j]*i]=phi[i]*prime[j]; mu[prime[j]*i]=0; break;}
phi[prime[j]*i]=phi[i]*phi[prime[j]]; mu[prime[j]*i]=-mu[i];
}
}
for(int i=1;i<=N;++i) phi[i]+=phi[i-1];
for(int i=1;i<=N;++i) mu[i]+=mu[i-1];
}
ll solve1(ll x){
ll pos=n/x;
if(x<=N) return phi[x];
if(vis_phi[pos]) return value_phi[pos];
value_phi[pos]=(ll)x*(x+1)/2; vis_phi[pos]=1;
for(ll i=2,last=0;i<=x;i=last+1){
last=x/(x/i);
value_phi[pos]-=solve1(x/i)*(last-i+1);
}
return value_phi[pos];
}
ll solve2(ll x){
ll pos=n/x;
if(x<=N) return mu[x];
if(vis_mu[pos]) return value_mu[pos];
value_mu[pos]=1; vis_mu[pos]=1;
for(ll i=2,last=0;i<=x;i=last+1){
last=x/(x/i);
value_mu[pos]-=solve2(x/i)*(last-i+1);
}
return value_mu[pos];
}
void solve(){
n=read();
memset(vis_mu,0,sizeof(vis_mu));
memset(vis_phi,0,sizeof(vis_phi));
printf("%lld %lld\n",solve1(n),solve2(n));
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
pre(); for(int T=read();T;--T) solve();
//printf("%d\n",clock());
return 0;
}
文章目录
,