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