Visible Lattice Points

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 :

3

1

2

5

 

Sample Output :

7

19

175

 

Constraints :

T <= 50

1 <= N <= 1000000


Added by: Varun Jalan
Date: 2010-07-29
Time limit: 1.368s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages: All except: NODEJS objc PERL 6 VB.net
Resource: own problem used for Indian ICPC training camp

题目链接:http://www.spoj.com/problems/VLATTICE/en/

题目大意:求在(0,0,0)到(n,n,n)这个立方体里从(0,0,0)能看到多少个点

题目分析:(2,2,2)就看不到。由于被(1,1,1)挡住了。做过能量採集的都知道,就是求gcd(a, b, c) = 1的组数。当中1 <= a, b, c <= n,裸的莫比乌斯反演题,注意两点。三个数轴上还有三点(0, 0, 1)。(0 ,1, 0),(1, 0, 0),另外xoy面。yoz面,xoz面。三个面上另一些点,这些都要单独算,然后再加上立方体中不包含轴和面的点,分块求和优化10ms解决

  1. #include <cstdio>
  2. #include <algorithm>
  3. #define ll long long
  4. using namespace std;
  5. int const MAX = 1000005;
  6. int mob[MAX], p[MAX], sum[MAX];
  7. bool noprime[MAX];
  8.  
  9. int Min(int a, int b, int c)
  10. {
  11. return min(a, min(b, c));
  12. }
  13.  
  14. void Mobius()
  15. {
  16. int pnum = 0;
  17. mob[1] = 1;
  18. sum[1] = 1;
  19. for(int i = 2; i < MAX; i++)
  20. {
  21. if(!noprime[i])
  22. {
  23. p[pnum ++] = i;
  24. mob[i] = -1;
  25. }
  26. for(int j = 0; j < pnum && i * p[j] < MAX; j++)
  27. {
  28. noprime[i * p[j]] = true;
  29. if(i % p[j] == 0)
  30. {
  31. mob[i * p[j]] = 0;
  32. break;
  33. }
  34. mob[i * p[j]] = -mob[i];
  35. }
  36. sum[i] = sum[i - 1] + mob[i];
  37. }
  38. }
  39.  
  40. ll cal(int l, int r)
  41. {
  42. if(l > r)
  43. swap(l, r);
  44. ll ans = 0;
  45. for(int i = 1, last = 0; i <= l; i = last + 1)
  46. {
  47. last = min(l / (l / i), r / (r / i));
  48. ans += (ll) (l / i) * (r / i) * (sum[last] - sum[i - 1]);
  49. }
  50. return ans;
  51. }
  52.  
  53. ll cal(int l, int m, int r)
  54. {
  55. if(l > r)
  56. swap(l, r);
  57. if(l > m)
  58. swap(l, m);
  59. ll ans = 0;
  60. for(int i = 1, last = 0; i <= l; i = last + 1)
  61. {
  62. last = Min(l / (l / i), m / (m / i), r / (r / i));
  63. ans += (ll) (l / i) * (m / i) * (r / i) * (sum[last] - sum[i - 1]);
  64. }
  65. return ans;
  66. }
  67.  
  68. int main()
  69. {
  70. Mobius();
  71. int T;
  72. scanf("%d", &T);
  73. while(T --)
  74. {
  75. int n;
  76. scanf("%d", &n);
  77. ll ans = 3;
  78. ans += (ll) cal(n, n, n);
  79. ans += (ll) cal(n ,n) * 3;
  80. printf("%lld\n", ans);
  81. }
  82. }

SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)的更多相关文章

  1. 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 ...

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

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

  3. spoj 7001 Visible Lattice Points莫比乌斯反演

    Visible Lattice Points Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

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

    题意:求一个正方体里面,有多少个顶点可以在(0,0,0)位置直接看到,而不被其它点阻挡.也就是说有多少个(x,y,z)组合,满足gcd(x,y,z)==1或有一个0,另外的两个未知数gcd为1 定义f ...

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

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

  6. Spoj 7001 Visible Lattice Points 莫比乌斯,分块

    题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193   Visible Lattice Points Time L ...

  7. spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数

    /** 题目:Visible Lattice Points 链接:https://vjudge.net/contest/178455#problem/A 题意:一个n*n*n大小的三维空间.一侧为(0 ...

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

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

  9. SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】

    题目链接 一道比较简单的莫比乌斯反演,不过ans会爆long long,我是用结构体来存结果的,结构体中两个LL型变量分别存大于1e17和小于1e17的部分 #include<bits/stdc ...

随机推荐

  1. 详解Java类的生命周期

    引言 最近有位细心的朋友在阅读笔者的文章时,对Java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内Java方面的教材大多只是告 ...

  2. HTML与XML的区别

    什么是HTML HTML的全拼是Hypertext Markup Language, 中文也就是超文本链接标示语言.HTML(HyperTextMark-upLanguage)即超文本标记语言,是WW ...

  3. angularJs 中ui-router 路由向controller传递数据

    页面上 : ui-sref="home.dataAnalysis({role:'thirdpart:tokenverify',menuType:'a'})" 路由设置 .state ...

  4. rocketmq 问题

    1. 收不到消息-consumerOffset.json 信息错位 这种情况一般是,手动删除了store/commitlog目录里的数据等非常规手段造成了consumerOffset.json中记录的 ...

  5. 07-复习数组和简单api

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. RDDs基本操作、RDDs特性、KeyValue对RDDs、RDD依赖

    摘要:RDD是Spark中极为重要的数据抽象,这里总结RDD的概念,基本操作Transformation(转换)与Action,RDDs的特性,KeyValue对RDDs的Transformation ...

  7. 【bzoj4930】棋盘 费用流

    题目描述 给定一个n×n的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置(x,y),(u,v)能互相攻击当前仅 当满足以下两个条件: 1:x=u或y=v 2:对于(x,y)与(u,v)之间 ...

  8. haskell 乱搞(2)之 Y-conbinator [原创]

    Y-conbinator"有没有用"?并没有,在大多数支持函数式编程的语言里,你可以自由的使用递归,而这货只是作为理论基石弥散在函数式编程的血肉之中 这是数学笔记,这是数学笔记,这 ...

  9. 跟着xiaoxin巨巨做cf

    cf 385 C. Bear and Prime Numbers 题目大意:有一个数列{xi},每次给出一个询问[l, r],即问 S(l ,r)是l和r之间的素数,f(p)表示数列{xi}中整除p的 ...

  10. 算法复习——欧拉函数(poj3090)

    题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or e ...