SPOJ - 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 :
3
1
2
5
Sample Output :
7
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 (莫比乌斯反演)的更多相关文章
- SPOJ PGCD(莫比乌斯反演)
传送门:Primes in GCD Table 题意:给定两个数和,其中,,求为质数的有多少对?其中和的范围是. 分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0). ...
- bzoj 2820 / SPOJ PGCD 莫比乌斯反演
那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j= ...
- SPOJ 7001(莫比乌斯反演)
传送门:Visible Lattice Points 题意:0<=x,y,z<=n,求有多少对xyz满足gcd(x,y,z)=1. 设f(d) = GCD(a,b,c) = d的种类数 : ...
- 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 莫比乌斯反演 难度: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 (莫比乌斯反演基础题)
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 VLATTICE Visible Lattice Points 莫比乌斯反演
这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...
- SPOJ VLATTICE Visible Lattice Points(莫比乌斯反演)题解
题意: 有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点. 思路: 按题意研究一下就会发现题目所求为. \[(\sum_{i=1}^n\sum_{j=1}^n\su ...
随机推荐
- Python3解leetcode Maximum SubarrayHouse Robber
问题描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- shell脚本学习 (8) fmt 格式化段落
1 获取系统中的字典文件 -n隐藏查找过程 -e 匹配多次,只打印带p的行(不能写成-e -n) ,100p /usr/share/dict/words 会显示1-100行的字母 2 fmt 按默认 ...
- PHP curl_multi_close函数
curl_multi_close — 关闭一组cURL句柄 说明 void curl_multi_close ( resource $mh ) 关闭一组cURL句柄. 参数 mh 由 curl_mul ...
- 安装并配置前端自动化工具-gulp
由于现在前端自动化已经很有必要了,所以我今天死皮烂脸的找了2位前端大咖帮助我安装和配置gulp,讲真,这一步步弄下来直到安装配置成功,到现在还是迷迷糊糊,不过我还是把这些步骤给记录下来,以防下次不记得 ...
- vue数据渲染、条件判断及列表循环
1.数据渲染 {{msg}} <template> <div id="app"> {{msg}} </div> </template&g ...
- Php单元测试 phpunit & codecept
phpunit: Windows版本 整体上说,在 Windows 下安装 PHAR 和手工在 Windows 下安装 Composer 是一样的过程: 下载链接:http://pan.baidu.c ...
- 【读书笔记】:MIT线性代数(1):Linear Combinations
1. Linear Combination Two linear operations of vectors: Linear combination: 2.Geometric Explaination ...
- java环境搭建与安装开发工具全教程
当前端的后台搭档是做java后台时,这时就需要自己搭建一个java开发环境,和安装eclipse了. 那么,一般这些开发环境在一个开发团队中是统一的.正规完善的公司还会有自己软件库和安装配置文档.这时 ...
- SSL连接出现的问题
客户端向服务器发送数据时,份两种情况,SSL单向验证和SSL双向验证 1.SSL单向验证时 代码如下: import java.io.IOException; import java.util.Has ...
- spring-data-neo4j了解
本项目demo地址[请阅读readme文件]: https://gitee.com/LiuDaiHua/project-neo4j 最近项目上要搭建一个关系图谱的东西,领导给了neo4j和d3两个概念 ...