SPOJ 7001. Visible Lattice Points (莫比乌斯反演)
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 a,b,c <=N 的对数。
用莫比乌斯反演可以求解。
设g(n)为gcd(x,y,z)=n的个数,f(n)为n | g(i*n)的个数,那么有f(n)=sigma(n|d,g(d)),那么g(n)=sigma(n|d, mu(d/n)*f(d)),我们要求g(1),则g(1)=sigma(n|d, mu(d)*f(d)),
因为f(d)=(n/d)*(n/d)*(n/d),所以g(1)=sigma( mu(d)*(n/d)*(n/d)*(n/d) ).
/* ***********************************************
Author :kuangbin
Created Time :2013/8/21 18:28:50
File Name :F:\2013ACM练习\专题学习\数学\莫比乌斯反演\SPOJ7001.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
bool check[MAXN+];
int prime[MAXN+];
int mu[MAXN+];
void Moblus()
{
memset(check,false,sizeof(check));
mu[] = ;
int tot = ;
for(int i = ; i <= MAXN; i++)
{
if( !check[i] )
{
prime[tot++] = i;
mu[i] = -;
}
for(int j = ; j < tot; j++)
{
if(i * prime[j] > MAXN) break;
check[i * prime[j]] = true;
if( i % prime[j] == )
{
mu[i * prime[j]] = ;
break;
}
else
{
mu[i * prime[j]] = -mu[i];
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n;
Moblus();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
long long ans = ;
for(int i = ;i <= n;i++)
ans += (long long)mu[i]*(n/i)*(n/i)*((n/i)+);
printf("%lld\n",ans);
}
return ;
}
SPOJ 7001. Visible Lattice Points (莫比乌斯反演)的更多相关文章
- 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 (莫比乌斯反演基础题)
Visible Lattice Points Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at ...
- Spoj 7001 Visible Lattice Points 莫比乌斯,分块
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193 Visible Lattice Points Time L ...
- 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 莫比乌斯反演
这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...
- spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演
SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...
- spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数
/** 题目:Visible Lattice Points 链接:https://vjudge.net/contest/178455#problem/A 题意:一个n*n*n大小的三维空间.一侧为(0 ...
- SPOJ.Visible Lattice Points(莫比乌斯反演)
题目链接 /* http://www.spoj.com/problems/VLATTICE/ 题意:求一个n*n*n的晶体,有多少点可以在(0,0,0)处可以直接看到. 同BZOJ.2301 题目即要 ...
随机推荐
- 让R与Python共舞
转载:http://ices01.sinaapp.com/?p=129 R(又称R语言)是一款开源的跨平台的数值统计和数值图形化展现 工具.通俗点说,R是用来做统计和画图的.R拥有自己的脚本 ...
- CGI、FastCGI和php-fpm的概念和区别
CGI是HTTP Server和一个独立的进程之间的协议,把HTTP Request的Header设置成进程的环境变量,HTTP Request的正文设置成进程的标准输入,而进程的标准输出就是HTTP ...
- HDU 2544 最短路(floyd+bellman-ford+spfa+dijkstra队列优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找点1到点n的最短路(无向图) 练一下最短路... dijkstra+队列优化: #i ...
- iOS开发:用DES对字符串加解密
参考http://www.cnblogs.com/janken/archive/2012/04/05/2432930.html,做了个小修改,实现PHP,JAVA,Objective-c加解密结果相同 ...
- 在go中连接mysql
5.访问数据库 5.1 database/sql接口 5.2 使用MySQL数据库 5.3 使用SQLite数据库 5.4 使用PostgreSQL数据库 5.5 使用Beego orm库进行ORM开 ...
- JavaScript 七种数据类型
在 JavaScript 规范中,共定义了七种数据类型,分为 “基本类型” 和 “引用类型” 两大类,如下所示: 基本类型:String.Number.Boolean.Symbol.Undefined ...
- git客户端基本操作
首先下载git 一路next安装好了之后,打开任意盘符,右键打开git bash here 首先:初始首次的用户名和邮箱,之后就不用了. git config --global user.name & ...
- lr_get_attrib_string的使用
loadrunner controller 传递参数的一个方法: lr_get_attrib_string lang = lr_get_attrib_string("lang&quo ...
- CentOS 7 kibana安装配置
Kibana 是为Elasticsearch设计的开源分析和可视化平台,你可以使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互.你可以很容易实现高级的数据分析 ...
- OOD沉思录 --- 类和对象的关系 --- 使用关系原则
4.1 尽量减少类的协作的数量,即减少使用者和被使用者的数量. 协作意味着一定程度的耦合,但是完全没有协作的类也是没有意义的,最多只能作为一个库使用. 通过抽象,依赖接口,可以最大程度减少依赖的实现类 ...