SPOJ1007 VLATTICE - Visible Lattice Points
VLATTICE - 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
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的更多相关文章
- 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 ...
- [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)
题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(a, b, c) = 1 a,b,c <=N 的对数. 思路:我们令函数g(x)为g ...
- SPOJ—VLATTICE Visible Lattice Points(莫比乌斯反演)
http://www.spoj.com/problems/VLATTICE/en/ 题意: 给一个长度为N的正方形,从(0,0,0)能看到多少个点. 思路:这道题其实和能量采集是差不多的,只不过从二维 ...
- 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 ...
- SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演
这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...
- [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 ...
- SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】
题目链接 一道比较简单的莫比乌斯反演,不过ans会爆long long,我是用结构体来存结果的,结构体中两个LL型变量分别存大于1e17和小于1e17的部分 #include<bits/stdc ...
- SPOJ VLATTICE Visible Lattice Points(莫比乌斯反演)题解
题意: 有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点. 思路: 按题意研究一下就会发现题目所求为. \[(\sum_{i=1}^n\sum_{j=1}^n\su ...
随机推荐
- js设置随机切换背景图片
<script> var imgs =["http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_2.jpg&q ...
- 转 linux 权限
发布系统架构图简化如下: 管理员通过Jenkins调用“发布程序(代号varian,以下简称varian)”,发布程序会进行一系列的初始化操作,完成后生成Docker镜像上传到Docker仓库,容器集 ...
- Effective STL读书笔记
Effective STL 读书笔记 本篇文字用于总结在阅读<Effective STL>时的笔记心得,只记录书上描写的,但自己尚未熟练掌握的知识点,不记录通用.常识类的知识点. STL按 ...
- Vue slot简单理解
情形一: 子组件定义了具名的slot,父组件使用具名的slot,slot显示顺序为子组件定义slot的顺序 子组件: Vue.component('child',{ template:`<div ...
- JWT全面解读、使用步骤
JWT全面解读JWT全面解读前言JWT基本使用在pom.xml引入java-jwt示例如下概念介绍JWT消息构成头部playload标准中注册的声明 (建议但不强制使用)自定义数据签名signatur ...
- 转载SQL容易产生的错误问题
概述 因为每天需要审核程序员发布的SQL语句,所以收集了一些程序员的一些常见问题,还有一些平时收集的其它一些问题,这也是很多人容易忽视的问题,在以后收集到的问题会补充在文章末尾,欢迎关注,由于收集的问 ...
- jsp新建项目
1.在原有项目的基础上新建一个文件夹 在文件夹内新建一个jsp文件 取名 JSP容器处理JSP文件需要以下三个阶段:翻译——编译——执行 JSP的页面元素包括 静态内容-HTML静态文本 指令-以“& ...
- yum常用操作
一.yum安装使用: 1.Yum:rpm的前端程序,用来解决软件包相关依赖性,可以在多个库之间定位软件包,up2date的替代工具 2.yum repository:yum repo,存储了众多rpm ...
- MAC下Android的Eclipse开发环境的搭建 转自MacroCheng
原文地址: http://www.cnblogs.com/macro-cheng/archive/2011/09/30/android-001.html 一.Eclipse的下载 到网站:http: ...
- PostgreSQL恢复误删数据
在Oracle中:删除表或者误删表记录:有个闪回特性,不需要停机操作,可以完美找回记录.当然也有一些其他的恢复工具:例如odu工具,gdul工具.都可以找回数据.而PostgreSQL目前没有闪回特性 ...