板子题(卡常)

也可能是用map太慢了

/**************************************************************
Problem: 3944
User: walfy
Language: C++
Result: Accepted
Time:9932 ms
Memory:84304 kb
****************************************************************/ //#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>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#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 pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
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 void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=5000000+10,maxn=400000+10,inf=0x3f3f3f3f; int prime[N],cnt,mu[N];
ll phi[N];
bool mark[N];
map<int,int>muu;
map<int,int>::iterator it1;
map<int,ll>phii;
map<int,ll>::iterator it2;
void init()
{
mu[1]=phi[1]=1;
for(int i=2;i<N;i++)
{
if(!mark[i])prime[++cnt]=i,mu[i]=-1,phi[i]=i-1;
for(int j=1;j<=cnt&&i*prime[j]<N;j++)
{
mark[i*prime[j]]=1;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for(int i=1;i<N;i++)mu[i]+=mu[i-1],phi[i]+=phi[i-1];
}
ll getmu(ll n)
{
if(n<N)return mu[n];
if((it1=muu.find(n))!=muu.end())return it1->se;
ll ans=1;
for(ll i=2,j;i<=n;i=j+1)
{
j=n/(n/i);
ans-=1ll*(j-i+1)*getmu(n/i);
}
return muu[n]=ans;
}
ll getphi(ll n)
{
if(n<N)return phi[n];
if((it2=phii.find(n))!=phii.end())return it2->se;
ll ans=n*(n+1)/2;
for(ll i=2,j;i<=n;i=j+1)
{
j=n/(n/i);
ans-=1ll*(j-i+1)*getphi(n/i);
}
return phii[n]=ans;
}
int main()
{
init();
int T;scanf("%d",&T);
while(T--)
{
ll n;scanf("%lld",&n);
printf("%lld %lld\n",getphi(n),getmu(n));
}
return 0;
}
/********************
10
2147483638
2147483639
2147483640
2147483641
2147483642
2147483643
2147483644
2147483645
2147483646
2147483647
********************/

不用map的版本

/**************************************************************
Problem: 3944
User: walfy
Language: C++
Result: Accepted
Time:7600 ms
Memory:67700 kb
****************************************************************/ //#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>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#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 pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
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 void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=2000000+10,maxn=400000+10,inf=0x3f3f3f3f; int prime[N],cnt,mu[N];
ll phi[N],n,muu[N],phii[N];
bool mark[N],vis[N];
void init()
{
mu[1]=phi[1]=1;
for(int i=2;i<N;i++)
{
if(!mark[i])prime[++cnt]=i,mu[i]=-1,phi[i]=i-1;
for(int j=1;j<=cnt&&i*prime[j]<N;j++)
{
mark[i*prime[j]]=1;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for(int i=1;i<N;i++)mu[i]+=mu[i-1],phi[i]+=phi[i-1];
}
void get(ll x)
{
if(x<N)return ;
if(vis[n/x])return ;
vis[n/x]=1;
phii[n/x]=x*(x+1)/2;
muu[n/x]=1;
for(ll i=2,j;i<=x;i=j+1)
{
j=x/(x/i);
if(x/i<N)phii[n/x]-=1ll*(j-i+1)*phi[x/i],muu[n/x]-=1ll*(j-i+1)*mu[x/i];
else
{
get(x/i);
phii[n/x]-=1ll*(j-i+1)*phii[n/(x/i)];
muu[n/x]-=1ll*(j-i+1)*muu[n/(x/i)];
}
}
}
int main()
{
init();
int T;scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof vis);
scanf("%lld",&n);
if(n<N)printf("%lld %d\n",phi[n],mu[n]);
else
{
memset(vis,0,sizeof vis);get(n);
printf("%lld %lld\n",phii[1],muu[1]);
}
}
return 0;
}
/********************
10
2147483638
2147483639
2147483640
2147483641
2147483642
2147483643
2147483644
2147483645
2147483646
2147483647
********************/
04-30 01:21