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 ;
}
spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演的更多相关文章
- SPOJ 7001. Visible Lattice Points (莫比乌斯反演)
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- Spoj 7001 Visible Lattice Points 莫比乌斯,分块
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193 Visible Lattice Points Time L ...
- spoj 7001 Visible Lattice Points莫比乌斯反演
Visible Lattice Points Time Limit:7000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- SPOJ 7001 Visible Lattice Points (莫比乌斯反演)
题意:求一个正方体里面,有多少个顶点可以在(0,0,0)位置直接看到,而不被其它点阻挡.也就是说有多少个(x,y,z)组合,满足gcd(x,y,z)==1或有一个0,另外的两个未知数gcd为1 定义f ...
- [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 (莫比乌斯反演基础题)
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 莫比乌斯反演 难度: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(莫比乌斯反演)题解
题意: 有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点. 思路: 按题意研究一下就会发现题目所求为. \[(\sum_{i=1}^n\sum_{j=1}^n\su ...
- SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演
这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...
随机推荐
- ZOJ 3645 BiliBili(高斯消元)
Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, and Kuroko ...
- springday04-go1
springmvc02:1.创建项目,导入jar包 2.复制xml文件到src下 3.在web.xml中配置DispatcherServlet(代码一致) <?xml version=" ...
- Android中实现两次点击返回键退出本程序
1,当用户使用我们的app的时候,有时候无意的或者不是有心的按下了我们的返回键,这时候为了更好的用体验,我们需要让用户再一次确定一下,以便判断用户的真实意图 代码如下: //该功能实现退出时提示的功能 ...
- Android测试AsyncTask下载图片
package com.example.myact8_async; import org.apache.http.HttpEntity; import org.apache.http.HttpResp ...
- jquery选项卡切换
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- springmvc下上传文件
使用ajax+表单+jQuery: function sendFile() { var action = "c/goFile.do"; $("#form").a ...
- Android—监听器
网上有很多短信和电话监听的程序,使用Broadcast. 记得一年前自己对照视频和教材是能够实现的,这周打开视频和教材照猫画虎,无论怎么都不会,纠结啊! 问题原因: 3.0之后没有主动开启过的应用无法 ...
- 为VirtualBox里的Linux系统安装增强功能
先说下为什么要安装增强功能, 很重要的原因是分辨率,没有安装增强功能的虚拟机里的系统往往不能全屏的,屏幕显示的内容没有充满整个屏幕. 还有主机和客户机之间共享文件夹也需要客户机的系统安装了增强功能. ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON color_fuses1
zw版[转发·台湾nvp系列Delphi例程]HALCON color_fuses1 procedure TForm1.Button1Click(Sender: TObject);var w, h : ...
- NOIP201205Vigenère密码
NOIP201205Vigenère密码 [问题描述] 16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密 ...