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. 【SpringBoot】 理解Spirng中的IOC原理

    前言 前文已经介绍了Spring Bean的生命周期,在这个周期内有一个重要的概念就是: IOC容器 大家也知道IOC是Sping 的重要核心之一,那么如何理解它呢,它又是产生什么作用呢?本文就IOC ...

  2. ASP.NET开发知识总结

    1.统一异常处理 某商城采用的异常处理方式,是全局统一捕捉,统一处理 思路: 一.定义异常过滤器    实现 MyExceptionFilter : FilterAttribute,IExceptio ...

  3. Modify PDF operators.

    1 Depart Process: 2 1. Grep xref and trailer binary position in file. 3 2. Dump xref table and trail ...

  4. shell zip和unzip压缩和解压,压缩效率

    1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录zip -r mydata.zip ./*txt #压缩当 ...

  5. git笔记十:本地仓库同步到gitlab

    本地仓库同步到gitlab 帮助文档 git remote --help 操作场景: 本地创建git仓库(含有readme.md文件), commit了三次 gitlab网站创建了一个项目 添加了re ...

  6. 正则findall的使用

    import re title = 'hello, 你好,world' print(title) title = u'hello, 你好,world' print(title) #汉字匹配 +的意思是 ...

  7. APP测试功能点

    1.使用APP时手机耗电情况 2.APP占用手机内存 3.APP在不同网络下的使用情况(WiFi/4G/3G/2G) 4.APP安装包大小 5.APP流量消耗 6.APP支持系统版本(android, ...

  8. IE兼容模式下样式分离错乱,求CSS高手

    IE正常模式下访问正常 兼容模式右边图片切换区域样式错乱,求CSS高手! 详细参考网址:www.javams.com

  9. 插件化框架解读之android系统服务实现原理(五)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 一.系统服务提供方式 1.我们平时最常见的系统服务使用方式 Wi ...

  10. [已解决]报错: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Users/mac/Ana

    报错代码: pip3 install gerapy 报错内容: Could not install packages due to an EnvironmentError: [Errno 13] Pe ...