Visible Lattice Points SPOJ - VLATTICE

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Visible Lattice Points SPOJ - VLATTICE相关的知识,希望对你有一定的参考价值。

Visible Lattice Points SPOJ - VLATTICE

题意:

有一个n∗n∗n的三维直角坐标空间,问从(0,0,0)看能看到几个点。

题解:

本题是二维的一个升级版,升级成三维
用莫比乌斯反演来做

代码:

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#else
    startTime = clock ();
    freopen("data.in", "r", stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#else
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn=1000020;
int mu[maxn];
int prime[maxn];
int vis[maxn];
int cnt=0;
void get_mu(int N){
	mu[1]=1;
	
	for(int i=2;i<=N;i++){
		if(!vis[i])
		{
			prime[++cnt]=i;
			mu[i]=-1;
		}
		for(int j=1;j<=cnt&&i*prime[j]<=N;j++){
			vis[i*prime[j]]=1;
			if(i%prime[j]==0)break;
			mu[i*prime[j]]=-mu[i];
		}
	}
} 
int main()
{
    //rd_test();
	get_mu(1000000);
	int t;
	read(t);
	while(t--){
		ll n;
		cin>>n;
		ll sum=3ll;
		for(int i=1;i<=n;i++){
			sum+=mu[i]*(1ll*n/i)*(n/i)*(n/i+3ll);
		}
		cout<<sum<<endl;
	}
    //Time_test();
}




以上是关于Visible Lattice Points SPOJ - VLATTICE的主要内容,如果未能解决你的问题,请参考以下文章

POJ3090 Visible Lattice Points

SPOJ1007 VLATTICE - Visible Lattice Points

poj3090 Visible Lattice Points [欧拉函数]

POJ3090Visible Lattice Points

POJ 3090 Visible Lattice Points

poj3090 Visible Lattice Points