Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ?

A point X is visible from point Y iff no other lattice point lies on the segment joining X and Y. 
Input : 
The first line contains the number of test cases T. The next T lines contain an interger N 
Output : 
Output T lines, one corresponding to each test case. 
Sample Input : 




 
Sample Output : 

19 
175 
Constraints : 
T <= 50 
1 <= N <= 1000000

题意就是给你一个三维的地图,坐标为(0,0,0)∼(n,n,n),判断有多少个坐标与原点之间的连线不经过其他的点。

思路:统计答案的点分为三类

1.坐标轴上的点(1,0,0)(0,1,0)(0,0,1) 三个

2.xoy,xoz,xoy面上的点gcd(i,j)==1; 二维很简单

3.其他点 gcd(i,j,k)==1

    

代码如下:

 #include <bits/stdc++.h>

 using namespace std;
typedef long long ll;
const int maxn = +;
int p[maxn],mo[maxn],phi[maxn],cnt=;
bool vis[maxn];
void init()
{
mo[]=;
phi[]=;
for(int i=;i<=maxn-;i++){
if(!vis[i]){
mo[i]=-;
phi[i]=i-;
p[cnt++]=i;
}
for(int j=;j<cnt&&(ll)i*p[j]<=maxn-;j++){
vis[i*p[j]]=true;
if(i%p[j]==){
mo[i*p[j]]=;
phi[i*p[j]]=phi[i]*p[j];
break;
}
mo[i*p[j]]=-mo[i];
phi[i*p[j]]=phi[i]*(p[j]-);
}
}
}
int n;
int main()
{
//freopen("de.txt","r",stdin);
init();
int T;
scanf("%d",&T);
while (T--){
scanf("%d",&n);
ll ans = ;
for (int i=;i<=n;++i){
ans+=(ll)mo[i]*(n/i)*(n/i)*(n/i);
}
for (int i=;i<=n;++i){
ans+=(ll)mo[i]*(n/i)*(n/i)*;
}
printf("%lld\n",ans+);
}
return ;
}

SPOJ - VLATTICE (莫比乌斯反演)的更多相关文章

  1. SPOJ PGCD(莫比乌斯反演)

    传送门:Primes in GCD Table 题意:给定两个数和,其中,,求为质数的有多少对?其中和的范围是. 分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0). ...

  2. bzoj 2820 / SPOJ PGCD 莫比乌斯反演

    那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j= ...

  3. SPOJ 7001(莫比乌斯反演)

    传送门:Visible Lattice Points 题意:0<=x,y,z<=n,求有多少对xyz满足gcd(x,y,z)=1. 设f(d) = GCD(a,b,c) = d的种类数 : ...

  4. SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)

    题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(a, b, c) = 1    a,b,c <=N 的对数. 思路:我们令函数g(x)为g ...

  5. SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3

    http://www.spoj.com/problems/VLATTICE/ 明显,当gcd(x,y,z)=k,k!=1时,(x,y,z)被(x/k,y/k,z/k)遮挡,所以这道题要求的是gcd(x ...

  6. SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)

    Visible Lattice Points Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at ...

  7. [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演

    7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...

  8. SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演

    这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...

  9. SPOJ VLATTICE Visible Lattice Points(莫比乌斯反演)题解

    题意: 有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点. 思路: 按题意研究一下就会发现题目所求为. \[(\sum_{i=1}^n\sum_{j=1}^n\su ...

随机推荐

  1. springmvc的请求参数

    @RequestMapping("/testRequestParam") public String testRequestParam(@RequestParam("us ...

  2. 【BZOJ2946&SPOJ1812】公共串(后缀自动机)

    题意:给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 单词的数量<=5,单词的长度至少为1,最大为2000. 思路: #include<bits/stdc++.h> us ...

  3. bugku | 你从哪里来

    题目链接 之前一直以为要用x-forwarded-for ,谁道用的是referer,Orz.在此特地记录,x-forwarded-for 和 referer的区别 X-Forwarded-For(X ...

  4. asp label居中

    https://stackoverflow.com/questions/25245839/align-a-label-to-the-right-using-asp-net Try something ...

  5. Javascript在ajax提交过程中页面显示加载中,请等待效果,并在提交过程中限制确定按钮防止多次提交,提交完成后,解除提交限制

    加载中,请等待div: <div id="load" class="center-in-center" style="display:none; ...

  6. HttpServletRequest 对文件上传的支持

    此前,对于处理上传文件的操作一直是让开发者头疼的问题,因为 Servlet 本身没有对此提供直接的支持,需要使用第三方框架来实现,而且使用起来也不够简单.Servlet 3.0 已经提供了这个功能,而 ...

  7. 对象与json字符串相互转化

    在java编程中,json字符串和对象的相互转化十分常用,下面我们就对象如何转化为json字符串以及json字符串如何转化为对象进行简要介绍,以便在代码中能方便使用. 1.依赖 本次介绍的方法依赖ja ...

  8. python3使除法结果为整数

    学习python3遇到问题: 今天在学习python时,想利用(121/100)得到的结果为整数 1, 121/100 outout:1.21 但是实际结果是浮点数 1.21 原因:python3后, ...

  9. luoguP1313 计算系数 题解(NOIP2011)

    P1313 计算系数 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<cm ...

  10. python opencv 生成验证码

    基本思路是使用opencv来把随机生成的字符,和随机生成的线段,放到一个随机生成的图像中去. 虽然没有加复杂的形态学处理,但是目前看起来效果还不错 尝试生成1000张图片,但是最后只有998张,因为有 ...