SPOJ Problem Set (classical)

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

题意:GCD(a,b,c)=1,   0<=a,b,c<=N ;

莫比乌斯反演,十分的巧妙。

GCD(a,b)的题十分经典。这题扩展到GCD(a,b,c)加了一维,但是思想却是相同的。

设f(d) = GCD(a,b,c) = d的种类数 ;

F(n) 为GCD(a,b,c) = d 的倍数的种类数, n%a == 0 n%b==0 n%c==0。

即 :F(d) = (N/d)*(N/d)*(N/d);

则f(d) = sigma( mu[n/d]*F(n), d|n )

由于d = 1 所以f(1) = sigma( mu[n]*F(n) ) = sigma( mu[n]*(N/n)*(N/n)*(N/n) );

由于0能够取到,所以对于a,b,c 要讨论一个为0 ,两个为0的情况 (3种).

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; typedef long long LL;
const int maxn = +;
bool s[maxn];
int prime[maxn],len = ;
int mu[maxn];
void init()
{
memset(s,true,sizeof(s));
mu[] = ;
for(int i=;i<maxn;i++)
{
if(s[i] == true)
{
prime[++len] = i;
mu[i] = -;
}
for(int j=;j<=len && (long long)prime[j]*i<maxn;j++)
{
s[i*prime[j]] = false;
if(i%prime[j]!=)
mu[i*prime[j]] = -mu[i];
else
{
mu[i*prime[j]] = ;
break;
}
}
}
} int main()
{
int n,T;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
LL sum = ;
for(int i=;i<=n;i++)
sum = sum + (long long)mu[i]*(n/i)*(n/i)*;
for(int i=;i<=n;i++)
sum = sum + (long long)mu[i]*(n/i)*(n/i)*(n/i);
printf("%lld\n",sum);
}
return ;
}

最新文章

  1. 从头开始构建LINUX[内核更新和资料]
  2. 数据源增量处理(Delta Proess)三大属性:Recod Mode、Delta Type、Serialization
  3. [Docker]Docker快速上手学习笔记
  4. QT添加二次确认功能,QMessageBox的使用
  5. Spring 文件上传功能
  6. hdu4939 Stupid Tower Defense (DP)
  7. Linux面试题汇总答案
  8. 搭建CAS单点登录服务器
  9. Iframe跨域Session丢失的问题
  10. Json.Net学习(1) 实现简单的序列化和反序列化
  11. Opencv 的数据结构
  12. linux上大量tcp端口处于TIME_WAIT的问题
  13. C# - 自定义 DataSet 的使用
  14. ImageMagick 拼图及切图方法
  15. 设计社区Dribbble VS. Bēhance,你选谁?
  16. [USACO4.2]草地排水Drainage Ditches
  17. CSS3学习系列之背景相关样式(二)
  18. 20175202 《Java程序设计》第六周学习总结
  19. 浏览器与Tomcat交互
  20. ArrayMap代码分析

热门文章

  1. .NET: 防止多个应用程序同时开
  2. SpringMvc的数据绑定流程
  3. linux第8天 connect强化
  4. struts拦截器
  5. JSP-06-使用JDBC操作数据库
  6. [记录]firefox繁体转换成简体的油猴脚本
  7. 薛非《品悟C-抛弃C程序设计中的谬误与恶习》读后感part1【转】
  8. echo date
  9. 指令重排序及Happens-before法则随笔
  10. win10启动文件夹: