spoj 7001
/***
大意:计算gcd(x,y,z) =1 0<= x, y , z <= n 问有多少个这样的对
莫比乌斯反演:(反演: 用结果推原因)
函数m(m)的定义如下:

莫比乌斯反演:
* f(x) = sigma{g(d)}其中x % d == 0,则g(x) = sigma{miu(d) * f(x/d)}
* f(x) = sigma{g(d)}其中d % x == 0,则g(x) = sigma{miu(d/x) * f(d)}
莫比乌斯反演中miu(x) =
* 1 {x中含有偶数个不同的质因子}
* -1 {x中含有奇数个不同的质因子}
* 0 {其他情况}
本题: 设f(x) = 约数为x 的所有数集合 g(x) = 最大公约数gcd 为x的集合
那么g(x) = siga(mu(d)*f(x/d)) x%d==0
或者 g(x) = siga(mu(d/x) *f(d)) d%x==0
在本题中只包含了x,y,z>1 情况 ,,还应加上退化到3个平面上的情况。。
1、 f(x) = n/x*n/x*n/x;----〉 g(x) = mu[i] *(n/x*n/x*n/x)
2、 加上退化到三个平面 ----〉 g(x) = mu[i]*(n/x+1)*(n/x+1)*(n/x+1)
或者分开求:
1、 三个坐标轴。。3
2、 空间中 n/x* n/x*n/x;
3、 三个坐标平面: n/x*n/x +n/x*n/x +n/x*n/x
**/
1、 第一种方法
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = ;
int pri[maxn];
int prime[maxn];
int mu[maxn];
void pri_mu(){
memset(prime,,sizeof(prime));
int cnt =;
mu[] =;
for(int i=;i<maxn;i++){
if(!prime[i]){
mu[i] =-;
pri[cnt++] = i;
}
for(int j=;j<cnt;j++){
if(i*pri[j]>maxn)
break;
prime[i*pri[j]] = ;
if(i%pri[j]==){
mu[i*pri[j]] =;
break;
}else
mu[i*pri[j]] = -mu[i];
}
}
}
int main()
{
pri_mu();
int t;
cin>>t;
long long n;
while(t--){
cin>>n;
long long ans =;
for(int i=;i<=n;i++)
ans += (long long )mu[i]*((n/i+)*(n/i+)*(n/i+)-);
cout<<ans<<endl;
}
return ;
}
----------------------------------------------------------------------------------------------------------
2、 第二种方法
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = ;
int pri[maxn];
int prime[maxn];
int mu[maxn];
void pri_mu(){
memset(prime,,sizeof(prime));
int cnt =;
mu[] =;
for(int i=;i<maxn;i++){
if(!prime[i]){
mu[i] =-;
pri[cnt++] = i;
}
for(int j=;j<cnt;j++){
if(i*pri[j]>maxn)
break;
prime[i*pri[j]] = ;
if(i%pri[j]==){
mu[i*pri[j]] =;
break;
}else
mu[i*pri[j]] = -mu[i];
}
}
}
int main()
{
pri_mu();
int t;
cin>>t;
long long n;
while(t--){
cin>>n;
long long ans =;
for(int i=;i<=n;i++)
ans += (long long )mu[i]*((n/i)*(n/i)*(n/i+));
cout<<ans<<endl;
}
return ;
}
spoj 7001的更多相关文章
- SPOJ 7001 VLATTICE【莫比乌斯反演】
题目链接: http://www.spoj.com/problems/VLATTICE/ 题意: 1≤x,y,z≤n,问有多少对(x,y,z)使得gcd(x,y,z)=1 分析: 欧拉搞不了了,我们用 ...
- spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演
SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...
- SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)
题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(a, b, c) = 1 a,b,c <=N 的对数. 思路:我们令函数g(x)为g ...
- 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 题意:0<=x,y,z<=n,求有多少对xyz满足gcd(x,y,z)=1. 设f(d) = GCD(a,b,c) = d的种类数 : ...
- SPOJ 7001 Visible Lattice Points (莫比乌斯反演)
题意:求一个正方体里面,有多少个顶点可以在(0,0,0)位置直接看到,而不被其它点阻挡.也就是说有多少个(x,y,z)组合,满足gcd(x,y,z)==1或有一个0,另外的两个未知数gcd为1 定义f ...
- spoj 7001 Visible Lattice Points莫比乌斯反演
Visible Lattice Points Time Limit:7000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
随机推荐
- stl 迭代子的失效
迭代子是STL中很重要的特性,但是其很脆弱(我个人认为),因为使用它的条件很苛刻,一不小心就失效了.其在两中情况下可能会失效. 1.当容器有插入操作时 在初始化了迭代子后,如果容器有插入操作时,迭代子 ...
- rsyslog 基本组成
Facility 定义日志消息的来源,以方便对日志进行分类,facility 有以下几种: --kern 内核消息 --user 用户级消息 --mail 邮件系统消息 --daemon 系统服务消息 ...
- mongodb初体验
最近关注大数据,自然会关注到nosql数据库,其中当然是mongodb. nosql数据库大多是k,v数据库,这也不是新鲜的名词了,berkerly DB已经存在很多年了,现在属于oracle. 具体 ...
- JAVA词汇大全
JAVA词汇大全 A B C D E F H I J L M O P R S T U V W A Abstract Window Toolkit(AWT)抽象窗体工具集 一个用本地图形组件实现的 ...
- 套接字socket 的地址族和类型、工作原理、创建过程
注:本分类下文章大多整理自<深入分析linux内核源代码>一书,另有参考其他一些资料如<linux内核完全剖析>.<linux c 编程一站式学习>等,只是为了更好 ...
- c/c++中宏定义##连接符 和#符的使用
C语言中如何使用宏C(和C++)中的宏(Macro)属于编译器预处理的范畴,属于编译期概念(而非运行期概念).下面对常遇到的宏的使用问题做了简单总结.关于#和##在C语言的宏中,#的功能是将其后面的宏 ...
- 介绍 - OC中的代理模式
一,代理设计模式的场合: 当对象A发生了一些行为,想告知对象B (让对象B成为对象A的代理对象) 对象B想监听对象A的一些行为 (让对象B成为对象A的代理对象) 当对象A无法处理某些行为的时候,想让对 ...
- Javaweb统计在线人数的小栗子
最近在学习Javaweb相关的内容(不黑不吹之前对web开发零基础),下面通过一个统计在线人数的小栗子讲讲Servlet监听器吧 开发环境 eclipse tomcat 7 先说说这个小栗子的构思: ...
- If We Were a Child Again
Description The Problem The first project for the poor student was to make a calculator that can jus ...
- BZOJ 1485: [HNOI2009]有趣的数列( catalan数 )
打个表找一下规律可以发现...就是卡特兰数...卡特兰数可以用组合数计算.对于这道题,ans(n) = C(n, 2n) / (n+1) , 分解质因数去算就可以了... -------------- ...