7001. Visible Lattice Points

Problem code: 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 : 









 

Sample Output : 



19 

175 

 

Constraints : 

T <= 50 

1 <= N <= 1000000

题目大意

给定n*n*n的立方体,每一个整数点除(0。0。0)之外都有一盏灯(抽象理解),问能看到多少盏灯(被盖住的灯不算)

解题思路

莫比乌斯反演/容斥原理的典型应用

用容斥原理来解释就是三个点都能被k整除的个数乘上莫比乌斯系数,求和就可以

而三个点都能被k整除的个数就是floor(n/i)^3

注意到最大数据量为1000000 直接线性处理的办法可能TLE

而(n/i)在后面i>(n/2)的部分结果都为1 能够省去一次次计算,直接按mu的前缀和来处理

则我们就统计同样(n/i)的值是否出现两次。假设出现两次那么我们就開始依照前缀和的方法来处理

不优化 6200ms

优化后 490ms

code

优化前

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set> #define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-10
#define mod 100000007ll
using namespace std;
LL n;
LL com[1000005],pri[1000005],phi[1000005],pn,sum[1000005],mu[1000005];
LL a[1000005];
int main()
{
memset(com ,0 ,sizeof com);
mu[1]=1;
for (int i=2;i<=1000000ll;i++)
{
if (com[i]==0)
{
phi[i]=i-1;
pri[++pn]=i;
mu[i]=-1;
// printf("%d\n", pri[pn]);
// system("pause");
}
for (int j=1;j<=pn&&pri[j]*i<=1000000ll;j++)
{
if (i%pri[j])
{
phi[i*pri[j]]=phi[i]*(pri[j]-1);
com[i*pri[j]]=1;
mu[i*pri[j]]=-mu[i];
}
else
{
phi[i*pri[j]]=phi[i]*(pri[j]);
com[i*pri[j]]=1;
mu[i*pri[j]]==0;
break;
}
}
}
sum[0]=0;
for (int i=1;i<=1000000ll;i++)
sum[i]=sum[i-1]+phi[i];
int T;
scanf("%d",&T);
while (T--)
{
// n=1000000;
LL ans=0;
scanf("%lld",&n);
for (int i=n;i;i--)
{
a[i]=(n/i)*(n/i)*(n/i);
ans+=a[i]*mu[i];
}
printf("%lld\n",ans+(sum[n]*2+1)*3+3);
} return 0;
}

优化后

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set> #define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-10
using namespace std;
int mu[1000005];
int com[1000005];
int pri[1000005],pn=0;
int phi[1000005];
LL presum[1000005];
int musum[1000005];
int main()
{
memset(com,0,sizeof com);
presum[1]=0;
mu[1]=1;
phi[1]=0;
for (int i=2;i<=1000000;i++)
{
if (com[i]==0)
{
pri[++pn]=i;
mu[i]=-1;
phi[i]=i-1;
}
for (int j=1;j<=pn&&pri[j]*i<=1000000;j++)
{
if (i%pri[j])
{
mu[i*pri[j]]=-mu[i];
com[i*pri[j]]=1;
phi[i*pri[j]]=phi[i]*(pri[j]-1);
}
else
{
phi[i*pri[j]]=phi[i]*(pri[j]);
mu[i*pri[j]]=0;
com[i*pri[j]]=1;
break;
}
}
presum[i]=presum[i-1]+phi[i];
musum[i]=musum[i-1]+mu[i];
}
int T;
scanf("%d",&T);
int a,b,c,d,k;
while (T--)
{
int n;
LL ans=0;
scanf("%d",&n);
int i;
for (i=1;i<=n;i++)
if ((n/i)==(n/(i+1))) break;
else
ans+=(LL)(n/i)*(n/i)*(n/i)*mu[i];
for (int j=(n/i);j;j--)
ans+=(LL)(j)*(j)*(j)*(musum[n/(j)]-musum[n/(j+1)]);
ans+=(LL)presum[n]*6+6;
printf("%lld\n",ans);
}
return 0;
}

[SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演的更多相关文章

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

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

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

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

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

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

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

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

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

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

    Visible Lattice Points 题意 : 从(0,0,0)出发在(N,N,N)范围内有多少条不从重合的直线:我们只要求gcd(x,y,z) = 1; 的点有多少个就可以了: 比如 : 点 ...

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

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

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

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

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

随机推荐

  1. Struts/Hibernate/Spring源码下载

    Struts: https://olex.openlogic.com/packages/struts Hibernate: https://olex.openlogic.com/packages/hi ...

  2. oracle排序union和union all区别

    是这样的,表格中有几个属性,比如age吧是之一,age是字符类型的数字,select之间由union连接,此时是无法对前面的select语句进行order by的,也就是无法排序,无法达成我要的按ag ...

  3. ABP(http://www.aspnetboilerplate.com/)下载初始化

    官网:http://www.aspnetboilerplate.com/ 下载 下载完成后用vs2015打开,是2015,低版本打开可能会出现一些问题 生成项目,Nuget会自动下载需要的类库 ABP ...

  4. Js构造对象-添加方法的三种方式

    Js构造函数添加方法有多种方案,来看一个混合方式构造函数的例子:申明person构造函数,有两个属性,name,qq.在原型上添加方法showname.这是最常用的方法. <script> ...

  5. javascript 数组 常用方法

    前言  学学忘忘  闲来做个笔记 整理下数组常用方法. Array 数组常用方法  创建数组的基本方式有两种    1.第一种是使用Array构造函数,  var arr = new Array(); ...

  6. 使用tomcat搭建Jenkins环境(centos7.3)

    1.从官网下载最新版本的tomcat下载地址:https://tomcat.apache.org/2.Jenkins 官方网站下载最新版本war包Jenkins官网地址:http://jenkins- ...

  7. Python学习笔记之异常处理

    1.概念 Python 使用异常对象来表示异常状态,并在遇到错误时引发异常.异常对象未被捕获时,程序将终止并显示一条错误信息 >>> 1/0 # Traceback (most re ...

  8. Python-Pandas简单操作

    1.直接构建复杂嵌套索引 2. MultiIndex方式构建复杂的索引 多层索引操作 pandas堆叠处理

  9. 1.Eclipse创建普通java工程

    1.创建java工程 2.输入java 工程名 3.编写类

  10. react 父组件 向 子组件 传值

    父组件 import React, { Component } from 'react'; import Test from './component/test'; //声明welcome组件 cla ...