SPOJ VLATTICE Visible Lattice Points(莫比乌斯反演)题解
题意:
有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点。
思路:
按题意研究一下就会发现题目所求为。
\[(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n[gcd(i,j,k)==1])+(\sum_{i=1}^n\sum_{j=1}^n[gcd(i,j)==1])\\+(\sum_{i=1}^n\sum_{k=1}^n[gcd(i,k)==1])+(\sum_{j=1}^n\sum_{k=1}^n[gcd(j,k)==1])
\]
随便求其中一个,由莫比乌斯函数已知\(\sum_{d|n}\mu(d)=[n=1]\),替换可得
\[\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\sum_{d|n}\mu(d)=\sum_{d=1}^n\mu(d)*\lfloor{\frac{n}{d}}\rfloor^3
\]
其他情况同理。
参考:
代码:
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1000000 + 5;
const int INF = 0x3f3f3f3f;
const ull seed = 131;
const ll MOD = 1e9;
using namespace std;
int mu[maxn], vis[maxn];
int prime[maxn], cnt;
void getmu(int n){
memset(vis, 0, sizeof(vis));
memset(mu, 0, sizeof(mu));
cnt = 0;
mu[1] = 1;
for(int i = 2; i <= n; i++) {
if(!vis[i]){
prime[cnt++] = i;
mu[i] = -1;
}
for(int j = 0; j < cnt && prime[j] * i <= n; j++){
vis[prime[j] * i] = 1;
if(i % prime[j] == 0) break;
mu[i * prime[j]] = -mu[i];
}
}
}
ll get(int n){
return 1LL * n * n * n + 3LL * n * n + 3LL * n;
}
int main(){
int n, T;
getmu(1000000);
scanf("%d", &T);
while(T--){
scanf("%d", &n);
ll ans = 0;
for(int i = 1; i <= n; i++){
ans += 1LL * mu[i] * get(n / i);
}
printf("%lld\n", ans);
}
return 0;
}