VLATTICE - Visible Lattice Points

no tags 

 

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

 

Description(题意)


N*N*N网格.
一个角落在 (0,0,0),对顶角落是
(N,N,N). 问从(0,0,0)看有多少个格点是可见的?点
X从点Y可见,当且仅当,线段XY上没有其他的点。

Input:

第一行是测试数据个数T。接着有T行每行有一个整数
N.

Output :

输出T行,每行是对应的可见格点的个数。

Sample Input :

3

1

2

5

Sample Output :

7

19

175

Constraints :

T <= 50

1 <= N <= 1000000

Solution:

#include<cstdio>
#include<iostream>
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
using namespace std;
typedef long long ll;
const int M=1e6+;
int n,m,T;ll sum[M];
int tot,prime[M/],mu[M];bool check[M];
void sieve(){
n=1e6;mu[]=;
for(int i=;i<=n;i++){
if(!check[i]) prime[++tot]=i,mu[i]=-;
for(int j=;j<=tot&&i*prime[j]<=n;j++){
check[i*prime[j]]=;
if(!(i%prime[j])){mu[i*prime[j]]=;break;}
else mu[i*prime[j]]=-mu[i];
}
}
for(int i=;i<=n;i++) sum[i]=sum[i-]+mu[i];
}
inline ll s2(int x){return 1LL*x*x;}
inline ll s3(int x){return 1LL*x*x*x;}
inline ll solve(int n){
ll ans=;
for(int i=,pos;i<=n;i=pos+){
pos=n/(n/i);
ans+=s3(n/i)*(sum[pos]-sum[i-]);
ans+=*s2(n/i)*(sum[pos]-sum[i-]);
}
return ans;
}
int main(){
sieve();
for(scanf("%d",&T);T--;){
scanf("%d",&n);
printf(LL"\n",solve(n));
}
return ;
}

SPOJ1007 VLATTICE - Visible Lattice Points的更多相关文章

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

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

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

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

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

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

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

  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 莫比乌斯反演

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

  7. [SPOJ7001]VLATTICE - Visible Lattice Points

    题目大意: $q(q\leq50)$组询问,对于给定的$n(n\leq10^7)$,求$\displaystyle\sum_{i=0}^n\sum_{j=0}^n\sum_{k=0}^n[\gcd(i ...

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

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

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

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

随机推荐

  1. NFS介绍 NFS服务端安装配置 NFS配置选项

    NFS 介绍 • NFS是Network File System的缩写 • NFS最早由Sun公司开发,分2,,4三个版本,2和3由Sun起草开发,.0开始Netapp公司参与并主导开发,最新为4.1 ...

  2. VS2010 工程设置

       本篇文章的主要内容转载自 http://blog.csdn.net/waitforfree/article/details/8622059 ,感谢博主的辛苦劳动.此处,对比较重要的部分,进行进一 ...

  3. Oracle 初始化参数 二三事,随记

    (1) alter system set log_archive_dest_n='location=d:\一个存在的目录';  ---- 预期 但是如果“d:\一个存在的目录”不是一个有效的目录,则“ ...

  4. QT 随机数生成

    下面总结了QT中随机生成的方法(仅供学习参考),分为旧方法和新方法,一般来说,旧的方法已经被抛弃,在开发新的应用中推荐使用新方法.  C++ Code  12345678910111213141516 ...

  5. Linux同时安装python2和Python3

    我们以Ubuntu 为例,默认地,Linux安装好后会默认安装python2版本: 安装Python3: For Debian:   [user@host]$ sudo apt-get install ...

  6. CDbConnection failed to open the DB connection: could not find driver错误的处理

    在PHP.INI文件中extension=php_pdo_mysql.dll 去掉注释

  7. python 类属性 、实例属性,可变数据结构作为类属性需要注意的地方

    1.一些经典的python错误不去阅读和不重视,就会把错误的做法带入到实际项目中来,甚至造成难以排查问题. 2.有一个大笨猪,按java写观察者模式,java写得是直接在类名下声明一个实例属性(不加s ...

  8. [原]unity3d ios平台内存优化(一)

    关于内存优化,人云亦云 各有己见.本文将通过设置Strpping Level ,减少内存使用. 先看三幅图: 1.没做任何优化,默认选项 2.设置Stripping level 为 Use micro ...

  9. Linux静态库生成

    Linux上的静态库,其实是目标文件的归档文件. 在Linux上创建静态库的步骤如下: 写源文件,通过 gcc -c xxx.c 生成目标文件. 用 ar 归档目标文件,生成静态库. 配合静态库,写一 ...

  10. [Bayesian] “我是bayesian我怕谁”系列 - Naive Bayes with Prior

    先明确一些潜规则: 机器学习是个collection or set of models,一切实践性强的模型都会被归纳到这个领域,没有严格的定义,’有用‘可能就是唯一的共性. 机器学习大概分为三个领域: ...