7001. Visible Lattice Points

Problem code: VLATTICE

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

题目大意

给定n*n*n的立方体,每一个整数点除(0。0。0)之外都有一盏灯(抽象理解),问能看到多少盏灯(被盖住的灯不算)

解题思路

莫比乌斯反演/容斥原理的典型应用

用容斥原理来解释就是三个点都能被k整除的个数乘上莫比乌斯系数,求和就可以

而三个点都能被k整除的个数就是floor(n/i)^3

注意到最大数据量为1000000 直接线性处理的办法可能TLE

而(n/i)在后面i>(n/2)的部分结果都为1 能够省去一次次计算,直接按mu的前缀和来处理

则我们就统计同样(n/i)的值是否出现两次。假设出现两次那么我们就開始依照前缀和的方法来处理

不优化 6200ms

优化后 490ms

code

优化前

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <ctime>
  5. #include <cctype>
  6. #include <cmath>
  7. #include <string>
  8. #include <cstring>
  9. #include <stack>
  10. #include <queue>
  11. #include <list>
  12. #include <vector>
  13. #include <map>
  14. #include <set>
  15.  
  16. #define sqr(x) ((x)*(x))
  17. #define LL long long
  18. #define INF 0x3f3f3f3f
  19. #define PI acos(-1.0)
  20. #define eps 1e-10
  21. #define mod 100000007ll
  22. using namespace std;
  23. LL n;
  24. LL com[1000005],pri[1000005],phi[1000005],pn,sum[1000005],mu[1000005];
  25. LL a[1000005];
  26. int main()
  27. {
  28. memset(com ,0 ,sizeof com);
  29. mu[1]=1;
  30. for (int i=2;i<=1000000ll;i++)
  31. {
  32. if (com[i]==0)
  33. {
  34. phi[i]=i-1;
  35. pri[++pn]=i;
  36. mu[i]=-1;
  37. // printf("%d\n", pri[pn]);
  38. // system("pause");
  39. }
  40. for (int j=1;j<=pn&&pri[j]*i<=1000000ll;j++)
  41. {
  42. if (i%pri[j])
  43. {
  44. phi[i*pri[j]]=phi[i]*(pri[j]-1);
  45. com[i*pri[j]]=1;
  46. mu[i*pri[j]]=-mu[i];
  47. }
  48. else
  49. {
  50. phi[i*pri[j]]=phi[i]*(pri[j]);
  51. com[i*pri[j]]=1;
  52. mu[i*pri[j]]==0;
  53. break;
  54. }
  55. }
  56. }
  57. sum[0]=0;
  58. for (int i=1;i<=1000000ll;i++)
  59. sum[i]=sum[i-1]+phi[i];
  60. int T;
  61. scanf("%d",&T);
  62. while (T--)
  63. {
  64. // n=1000000;
  65. LL ans=0;
  66. scanf("%lld",&n);
  67. for (int i=n;i;i--)
  68. {
  69. a[i]=(n/i)*(n/i)*(n/i);
  70. ans+=a[i]*mu[i];
  71. }
  72. printf("%lld\n",ans+(sum[n]*2+1)*3+3);
  73. }
  74.  
  75. return 0;
  76. }

优化后

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <ctime>
  5. #include <cctype>
  6. #include <cmath>
  7. #include <string>
  8. #include <cstring>
  9. #include <stack>
  10. #include <queue>
  11. #include <list>
  12. #include <vector>
  13. #include <map>
  14. #include <set>
  15.  
  16. #define sqr(x) ((x)*(x))
  17. #define LL long long
  18. #define INF 0x3f3f3f3f
  19. #define PI acos(-1.0)
  20. #define eps 1e-10
  21. using namespace std;
  22. int mu[1000005];
  23. int com[1000005];
  24. int pri[1000005],pn=0;
  25. int phi[1000005];
  26. LL presum[1000005];
  27. int musum[1000005];
  28. int main()
  29. {
  30. memset(com,0,sizeof com);
  31. presum[1]=0;
  32. mu[1]=1;
  33. phi[1]=0;
  34. for (int i=2;i<=1000000;i++)
  35. {
  36. if (com[i]==0)
  37. {
  38. pri[++pn]=i;
  39. mu[i]=-1;
  40. phi[i]=i-1;
  41. }
  42. for (int j=1;j<=pn&&pri[j]*i<=1000000;j++)
  43. {
  44. if (i%pri[j])
  45. {
  46. mu[i*pri[j]]=-mu[i];
  47. com[i*pri[j]]=1;
  48. phi[i*pri[j]]=phi[i]*(pri[j]-1);
  49. }
  50. else
  51. {
  52. phi[i*pri[j]]=phi[i]*(pri[j]);
  53. mu[i*pri[j]]=0;
  54. com[i*pri[j]]=1;
  55. break;
  56. }
  57. }
  58. presum[i]=presum[i-1]+phi[i];
  59. musum[i]=musum[i-1]+mu[i];
  60. }
  61. int T;
  62. scanf("%d",&T);
  63. int a,b,c,d,k;
  64. while (T--)
  65. {
  66. int n;
  67. LL ans=0;
  68. scanf("%d",&n);
  69. int i;
  70. for (i=1;i<=n;i++)
  71. if ((n/i)==(n/(i+1))) break;
  72. else
  73. ans+=(LL)(n/i)*(n/i)*(n/i)*mu[i];
  74. for (int j=(n/i);j;j--)
  75. ans+=(LL)(j)*(j)*(j)*(musum[n/(j)]-musum[n/(j+1)]);
  76. ans+=(LL)presum[n]*6+6;
  77. printf("%lld\n",ans);
  78. }
  79. return 0;
  80. }

[SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演的更多相关文章

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

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

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

    http://www.spoj.com/problems/VLATTICE/en/ 题意: 给一个长度为N的正方形,从(0,0,0)能看到多少个点. 思路:这道题其实和能量采集是差不多的,只不过从二维 ...

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

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

  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 (莫比乌斯反演基础题)

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

  6. Visible Lattice Points (莫比乌斯反演)

    Visible Lattice Points 题意 : 从(0,0,0)出发在(N,N,N)范围内有多少条不从重合的直线:我们只要求gcd(x,y,z) = 1; 的点有多少个就可以了: 比如 : 点 ...

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

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

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

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

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

随机推荐

  1. 禁止tomcat扫描jar包的tld文件

    禁止tomcat扫描jar包的tld文件tomcat/conf/logging.properties 取消注释org.apache.jasper.compiler.TldLocationsCache. ...

  2. 【Android】实例 忐忑的精灵

    在Android Studio中创建项目,名称为“Animation And Multimedia”,然后在该项目中创建一个Module,名称为“Frame-By-Frame Animation”.在 ...

  3. poj3009 Curling 2.0 深搜

    PS:以前看到题目这么长就没写下去了.今天做了半天,没做出来.准备看题解,打开了网站都忍住了,最后还是靠自己做出来的.算是一点进步吧. 分析: 题目的意思没明白或者理解有偏差都没办法做题.看样例3和样 ...

  4. objectdatasouce的温故

    在做ecxel的时候,需要前台做一个联动的效果. 记录一下这个数据源的用法,大学时候用的,忘得差不多了 首先就是往页面拖拽一个objectdatasouce的控件 然后配置数据源: 选择业务对象(其实 ...

  5. Embedded之Stack之一

    1 Intro When a program starts executing, a certain contiguous section of memory is set aside for the ...

  6. vue遇到的大坑,h5在ios10版本下不能打开页面

    无论是谁,在做事情的过程中总是会遇到学坑,才能成为最后的大神 这个坑不说了,找了半天.希望能帮助到你们 进入build文件夹: 找到webpack.prod.conf.js文件: 在UglifyPlu ...

  7. 前端面试题总结 -vue

    1.active-class是哪个组件的属性? vue-router模块的router-link组件. 2.嵌套路由怎么定义? 在 VueRouter 的参数中使用 children 配置,这样就可以 ...

  8. Jquery常见操作多选框/复选框/checkbox

    1.判断checkbox是否为选中状态: if($("#searchNews").attr("checked")=="checked") { ...

  9. Python基础(二)数据类型

    (一)数字 Python3中的数字类型分为3种,分别是整型,浮点型以及复数. Python2种的数字类型分为4种,分别是整型,长整型,浮点型以及复数. 其中长整型时Python2为应对位数较大的而设置 ...

  10. Day 20 python基础总复习

    一.计算机基础 1.1 计算机基础之编程 编程语言是人与计算机之间交流的介质 编程就是写一堆文件 编程为了奴隶计算机,解放劳动力 1.2 计算机组成原理 CPU 控制器:控制硬件 运算器:逻辑运算和算 ...