Visible Lattice Points

Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

Description

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)出发的直线可看到多少个点(只能看到第一个,后面的视为挡住了看不见)。

解题思路:求gcd(x,y,z)=1的点有多少个,F(n) 表示满足条件的 gcd(x,y,z)==n的 (x,y,z) 对数;G(n) 表示满足 n | gcd(x,y,z) 的(x,y,z)对数,即 gcd(x,y,z)%n==0 的(x,y,z) 对数;

由定义:G(n)=sigma(F(d)),F(n)=sigma(U(d/n)*G(d))

这题就是求F(1)。G(d)=(n/d)*(n/d)(n/d)。

当3个坐标为0时有0个点;

2坐标为0的时候可见点在三条坐标轴上一共3个;

1坐标为0的时候3*ans(ans=sigma(u(d)*(n/i)*(n/i)));

坐标都不为0的时候ans=ans=sigma(u(d)*(n/i)*(n/i)*(n/i))

提示:提交代码时不能用__int64,只能用long long 

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; typedef __int64 LL;
const int maxn=;
int prime[maxn],mu[maxn],num;
bool flag[maxn]; void init()
{
int i,j;num=;mu[]=;
memset(flag,true,sizeof(flag));
for(i=;i<maxn;i++)
{
if(flag[i])
{
prime[num++]=i;mu[i]=-;
}
for(j=;j<num&&prime[j]*i<maxn;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==)
{
mu[i*prime[j]]=;break;
}
else mu[i*prime[j]]=-mu[i];
}
}
} int main()
{
init();
int t,i,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
LL ans=;
for(i=;i<=n;i++)
ans+=(LL)mu[i]*(n/i)*(n/i)*(n/i+);
printf("%I64d\n",ans);
}
return ;
}

spoj 7001 Visible Lattice Points莫比乌斯反演的更多相关文章

  1. SPOJ 7001 Visible Lattice Points (莫比乌斯反演)

    题意:求一个正方体里面,有多少个顶点可以在(0,0,0)位置直接看到,而不被其它点阻挡.也就是说有多少个(x,y,z)组合,满足gcd(x,y,z)==1或有一个0,另外的两个未知数gcd为1 定义f ...

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

  3. Spoj 7001 Visible Lattice Points 莫比乌斯,分块

    题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193   Visible Lattice Points Time L ...

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

  5. SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演

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

  6. spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演

    SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...

  7. SPOJ 7001. Visible Lattice Points (莫比乌斯反演)

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

  8. spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数

    /** 题目:Visible Lattice Points 链接:https://vjudge.net/contest/178455#problem/A 题意:一个n*n*n大小的三维空间.一侧为(0 ...

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

    题目链接 /* http://www.spoj.com/problems/VLATTICE/ 题意:求一个n*n*n的晶体,有多少点可以在(0,0,0)处可以直接看到. 同BZOJ.2301 题目即要 ...

随机推荐

  1. 二叉树、二叉搜索树、平衡二叉树、B树、B+树的精确定义和区别探究

    概述 关于树的概念很多,B树,B+树,红黑树等等. 但是你去翻翻百度百科,或者用百度或者谷歌搜索一下中文的树结构的介绍,全都是狗屁.没有哪个中文网站是真正精确解释树的定义的,尤其是百度百科. 下面我要 ...

  2. DateTime与long互转

    DateTime转long: public static long GetDateLong(object time) { DateTime epoc = TimeZone.CurrentTimeZon ...

  3. java利用SuffixFileFilter统计目录下特定后缀名文件的数目

    /** * 文件处理类 * @author zhangcd * @date 2017年1月3日 */ public class FileUtil { /** * 得到所有后缀的数目 * * @para ...

  4. Python Web 架构

    1. Django(全能型)2. Tornado3. BottlePython+Bottle+Sina SAE快速构建网站http://www.cnblogs.com/Xjng/p/3511983.h ...

  5. How to Install PhantomJS on Ubuntu 16.04

    Introduction PhantomJS is a scripted, headless browser that can be used for automating web page inte ...

  6. Linux菜鸟起飞之路【八】文本编辑器

    在Linux中,文本编辑器有两个,VI和VIM.这两个编辑器用法差不多,但vim是vi的升级版,所以功能更强大一些. vim编辑器一共有三种模式,命令行模式.编辑模式和扩展模式. 进入vim界面,首先 ...

  7. python3.7 内置函数整理

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 内置函数整理 #abs(x) #返回数字的绝对值. 参数可以是整 ...

  8. Python虚拟机函数机制之无参调用(一)

    PyFunctionObject对象 在Python中,任何一个东西都是对象,函数也不例外.函数这种抽象机制,是通过一个Python对象——PyFunctionObject来实现的 typedef s ...

  9. 浅谈CSS中的百分比

    结论: 标准流中的元素,看其属性有没有继承性.对于width和margin-left,它是可以继承的,它会参照父元素或者祖先元素(其实是包含块):对于height,它没有继承性,父元素或者祖先元素会自 ...

  10. 常见的Linux目录及其含义

    /                                   系统根目录,通常不会在这里存放文件 . /bin                              二进制目录,存放许多 ...