# BZOJ_P2671 Calc(数论+莫比乌斯反演)

BZOJ传送门

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 192 Solved: 100
[Submit][Status][Discuss]
Description

1.1<=a< b<=N
2.a+b整除a*b

Input

Output

Sample Input
15

Sample Output
4

HINT

Test N Test N
1 <=10 11 <=5*10^7
2 <=50 12 <=10^8
3 <=10^3 13 <=2*10^8
4 <=5*10^3 14 <=3*10^8
5 <=2*10^4 15 <=5*10^8
6 <=2*10^5 16 <=10^9
7 <=2*10^6 17 <=10^9
8 <=10^7 18 <=2^31-1
9 <=2*10^7 19 <=2^31-1
10 <=3*10^7 20 <=2^31-1

Source

Sol:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 50005
//#define min(a,b) (a>b?b:a)
typedef long long ll;
ll top, stk[N];ll ans,n;
int mu[N],pr[N],cnt;bool b[N];
void GetMu(){
mu[1]=1;
for(int i=2;i<N;i++){
if(!b[i]) pr[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*pr[j]<N;j++){
b[i*pr[j]]=1;if(!(i%pr[j])) break;
mu[i*pr[j]]=-mu[i];
}
}
}
void Decompose(ll x,ll i=1){
for(top=0;i*i<x;i++) if(!(x%i)) stk[++top]=i,stk[++top]=x/i;
if(i*i==x) stk[++top]=i;sort(stk+1,stk+top+1);
}
int main(){
GetMu();scanf("%lld",&n);
for(ll b=1;b*(b+1)<=n;b++){
Decompose(b);
for(ll a=1,j,w=0;a<b&&b*(a+b)<=n;a=j+1,w=0){
j=min(n/(n/b/(a+b))/b-b,b-1);
for(ll i=1;stk[i]<=j;i++) w+=mu[stk[i]]*(j/stk[i]-(a-1)/stk[i]);
ans+=n/b/(a+b)*w;
}
}
printf("%lld\n",ans);
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客