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 (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解决
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1000005;
int mob[MAX], p[MAX], sum[MAX];
bool noprime[MAX]; int Min(int a, int b, int c)
{
return min(a, min(b, c));
} void Mobius()
{
int pnum = 0;
mob[1] = 1;
sum[1] = 1;
for(int i = 2; i < MAX; i++)
{
if(!noprime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
noprime[i * p[j]] = true;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
sum[i] = sum[i - 1] + mob[i];
}
} ll cal(int l, int r)
{
if(l > r)
swap(l, r);
ll ans = 0;
for(int i = 1, last = 0; i <= l; i = last + 1)
{
last = min(l / (l / i), r / (r / i));
ans += (ll) (l / i) * (r / i) * (sum[last] - sum[i - 1]);
}
return ans;
} ll cal(int l, int m, int r)
{
if(l > r)
swap(l, r);
if(l > m)
swap(l, m);
ll ans = 0;
for(int i = 1, last = 0; i <= l; i = last + 1)
{
last = Min(l / (l / i), m / (m / i), r / (r / i));
ans += (ll) (l / i) * (m / i) * (r / i) * (sum[last] - sum[i - 1]);
}
return ans;
} int main()
{
Mobius();
int T;
scanf("%d", &T);
while(T --)
{
int n;
scanf("%d", &n);
ll ans = 3;
ans += (ll) cal(n, n, n);
ans += (ll) cal(n ,n) * 3;
printf("%lld\n", ans);
}
}
SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)的更多相关文章
- 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的三元组数量 ...
- spoj 7001 Visible Lattice Points莫比乌斯反演
Visible Lattice Points Time Limit:7000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- SPOJ 7001 Visible Lattice Points (莫比乌斯反演)
题意:求一个正方体里面,有多少个顶点可以在(0,0,0)位置直接看到,而不被其它点阻挡.也就是说有多少个(x,y,z)组合,满足gcd(x,y,z)==1或有一个0,另外的两个未知数gcd为1 定义f ...
- [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 Visible Lattice Points 莫比乌斯,分块
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193 Visible Lattice Points Time L ...
- spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数
/** 题目:Visible Lattice Points 链接:https://vjudge.net/contest/178455#problem/A 题意:一个n*n*n大小的三维空间.一侧为(0 ...
- SPOJ VLATTICE Visible Lattice Points(莫比乌斯反演)题解
题意: 有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点. 思路: 按题意研究一下就会发现题目所求为. \[(\sum_{i=1}^n\sum_{j=1}^n\su ...
- SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】
题目链接 一道比较简单的莫比乌斯反演,不过ans会爆long long,我是用结构体来存结果的,结构体中两个LL型变量分别存大于1e17和小于1e17的部分 #include<bits/stdc ...
随机推荐
- 【Oracle】使用dblink+minus方式迁移数据
一.需求说明 1.数据库a104的表syssde.a4_syssde_department中有1000条数据: 2.数据库a230的表syssde.a4_syssde_department中有800条 ...
- 面试准备——springboot相关
https://www.jianshu.com/p/63ad69c480fe https://blog.csdn.net/u013605060/article/details/80255192 htt ...
- E. A Magic Lamp
E. A Magic Lamp Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO ...
- [uiautomator篇][python] wifi接口学习网址
https://wifi.readthedocs.io/en/latest/wifi_command.html#usage
- 【JavaScript 2—基础知识点】:数据类型
导读:我发现不管是哪一门语言,都会先介绍其发展,语法规则,数据类型,流程控制等.那么,这次,就介绍一下JavaScript中的数据类型,有些看着眼熟,有些不熟.熟的也不是之前认识的,不熟的,也不见得就 ...
- BZOJ 4824 [Cqoi2017]老C的键盘 ——树形DP
每一个限制条件相当于一条有向边, 忽略边的方向,就成了一道裸的树形DP题 同BZOJ3167 唯一的区别就是这个$O(n^3)$能过 #include <map> #include < ...
- Django REST
一.什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度 ...
- 通过Idea进行Kubernetes YAML开发
即将推出的IntelliJ IDEA 2018.1 Ultimate Edition通过全新的Kubernetes插件为Kubernetes引入了初步支持.新插件支持从v1.5到最近发布的v1.9 的 ...
- Android数据存储之SQLite数据库
Android数据存储 之SQLite数据库简介 SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎. ...
- spring-boot-nginx代理-docker-compose部署
在本地测试,使用docker部署不用在意环境 java测试项目: web框架:spring boot 框架 项目管理:maven 数据库:redis + postgres + mongo 部署相关:n ...