bzoj 2671: Calc 数学

题意

给出N,统计满足下面条件的数对(a,b)的个数:
1.1<=a< b<=N
2.a+b整除a*b
n<=2^31-1

分析

%各种大爷们

代码

#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;

const int N=50005;

int top,prime[N],tot;
LL sta[N],mu[N],n,m;
bool not_prime[N];

void get_prime(int n)
{
    mu[1]=1;
    for (int i=2;i<=n;i++)
    {
        if (!not_prime[i]) prime[++tot]=i,mu[i]=-1;
        for (int j=1;j<=tot&&i*prime[j]<=n;j++)
        {
            not_prime[i*prime[j]]=1;
            if (i%prime[j]==0) break;
            mu[i*prime[j]]=-mu[i];
        }
    }
}

void divi(LL n)
{
    top=0;
    LL i;
    for (i=1;i*iif (n%i==0) sta[++top]=i,sta[++top]=n/i;
    if (i*i==n) sta[++top]=i;
    sort(sta+1,sta+top+1);
}

LL solve()
{
    LL ans=0;
    for (LL b=2;b*(b+1)<=n;b++)
    {
        divi(b);
        for (LL a=1,last;a1)
        {
            last=min(n/(n/b/(a+b))/b-b,b-1);LL cnt=0;
            for (int i=1;sta[i]<=last;i++) cnt+=mu[sta[i]]*(last/sta[i]-(a-1)/sta[i]);
            ans+=n/b/(a+b)*cnt;
        }
    }
    return ans;
}

int main()
{
    scanf("%lld",&n);m=sqrt(n);
    get_prime(m+1);
    printf("%lld",solve());
    return 0;
}

你可能感兴趣的:(数学)