BZOJ 3944: Sum 杜教筛

来源:互联网 发布:5230软件下载 编辑:程序博客网 时间:2024/04/30 14:13

3944: Sum

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 4289  Solved: 1128
[Submit][Status][Discuss]

Description

Input

一共T+1行
第1行为数据组数T(T<=10)
第2~T+1行每行一个非负整数N,代表一组询问

Output

一共T行,每行两个用空格分隔的数ans1,ans2

Sample Input

6
1
2
8
13
30
2333

Sample Output

1 1
2 0
22 -2
58 -3
278 -3
1655470 2

懒懒懒

注意一个地方:

在根号划分一个数的循环中多写一句if(pos==x)break;

为什么? 可以拍一拍。。。


#include<cmath>#include<ctime>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<bitset>#include<queue>#include<set>#include<map>using namespace std;typedef long long ll;inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void print(ll x){if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}const int N=5001000;int prime[N>>3],mobius[N];ll phi[N];bool book[N];void initial(){register int i,j,cnt=0,now;mobius[1]=1;phi[1]=1;for(i=2;i<N;++i){if(!book[i]){mobius[i]=-1;prime[++cnt]=i;phi[i]=i-1;}for(j=1;j<=cnt&&prime[j]*i<N;++j){now=i*prime[j];book[now]=1;phi[now]=phi[i]*(prime[j]-1);mobius[now]=-mobius[i];if(i%prime[j]==0){mobius[i*prime[j]]=0;phi[now]=phi[i]*prime[j];break;}}}for(i=1;i<N;++i)mobius[i]+=mobius[i-1];for(i=1;i<N;++i)phi[i]+=phi[i-1];}map<int,int>mp_mobius;map<int,ll>mp_phi;int get_mobius(int x){if(x<N)return mobius[x];if(mp_mobius[x])return mp_mobius[x];register int i,pos,res=1;for(i=2;i<=x;i=pos+1){pos=x/(x/i);res-=(pos-i+1)*get_mobius(x/i);if(pos==x)break;}mp_mobius[x]=res;return res;}ll get_phi(int x){if(x<N)return phi[x];if(mp_phi[x])return mp_phi[x];register int i,pos;ll res=(ll)x*((ll)x+1)/2;for(i=2;i<=x;i=pos+1){pos=x/(x/i);res-=1ll*(pos-i+1)*get_phi(x/i);if(pos==x)break;}mp_phi[x]=res;return res;}int main(){initial();register int T=read(),x;while(T--){x=read();print(get_phi(x));putchar(' ');print(get_mobius(x));puts("");}return 0;}/*6128133023331 12 022 -258 -3278 -31655470 2*/

原创粉丝点击