【杜教筛】【模板】P4213 【模板】杜教筛(Sum)

 


 

裸模板题

但在洛谷提交可能会超时,在洛谷上提交了一晚上,找了一晚上哪里出问题了,直到最后把一份参考的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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值