题目链接:http://www.spoj.com/problems/VLATTICE/

题意:求gcd(a, b, c) = 1    a,b,c <=N 的对数。

思路:我们令函数g(x)为gcd(a, b, c) = x的对数,那么这题就是要求g(1)。我们令f(x)为x | gcd(a, b, c)的对数,显然f(n) = sigma(n | d, g(d)) 。f(d) = (n/d) * (n/d) * (n/d),那么我们就可以用莫比乌斯反演公式了, g(n) = sigma(n | d, mu(d/n)f(d))   g(1) = mu(d)f(d) = mu(d)*(n/d)*(n/d)*(n/d)。考虑1位为0,2位为0的情况。

莫比乌斯反演的两种形式:

g(n) = sigma(d | n, f(d))   f(n) = sigma(d | n, mu(d) * g(n/d))

g(n) = sigma(n | d, f(d))   f(n) = sigma(n | d, mu(d / n) * g(d))

code:

 #include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = ;
bool check[MAXN];
int primes[MAXN];
int mu[MAXN]; void moblus()
{
memset(check, false, sizeof(check));
mu[] = ;
int cnt = ;
for (int i = ; i < MAXN; ++i) {
if (!check[i]) {
primes[cnt++] = i;
mu[i] = -;
}
for (int j = ; j < cnt; ++j) {
if (i * primes[j] > MAXN) break;
check[i * primes[j]] = true;
if (i % primes[j] == ) {
mu[i * primes[j]] = ;
break;
} else {
mu[i * primes[j]] = -mu[i];
}
}
}
} int main()
{
moblus();
int nCase;
scanf("%d", &nCase);
while (nCase--) {
int n;
scanf("%d", &n);
LL ans = ; // 001 010 100
for (int i = ; i <= n; ++i) {
ans += (LL)mu[i] * (n / i) * (n / i) * (n / i + );
}
printf("%lld\n", ans);
}
return ;
}

 

SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演

    这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...

  4. spoj 7001 Visible Lattice Points莫比乌斯反演

    Visible Lattice Points Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

  5. spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数

    /** 题目:Visible Lattice Points 链接:https://vjudge.net/contest/178455#problem/A 题意:一个n*n*n大小的三维空间.一侧为(0 ...

  6. SPOJ 7001 Visible Lattice Points (莫比乌斯反演)

    题意:求一个正方体里面,有多少个顶点可以在(0,0,0)位置直接看到,而不被其它点阻挡.也就是说有多少个(x,y,z)组合,满足gcd(x,y,z)==1或有一个0,另外的两个未知数gcd为1 定义f ...

  7. SPOJ.Visible Lattice Points(莫比乌斯反演)

    题目链接 /* http://www.spoj.com/problems/VLATTICE/ 题意:求一个n*n*n的晶体,有多少点可以在(0,0,0)处可以直接看到. 同BZOJ.2301 题目即要 ...

  8. SPOJ1007 VLATTICE - Visible Lattice Points

    VLATTICE - Visible Lattice Points no tags  Consider a N*N*N lattice. One corner is at (0,0,0) and th ...

  9. [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演

    7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...

随机推荐

  1. spark install

    https://chongyaorobin.wordpress.com/2015/07/01/step-by-step-of-installing-apache-spark-on-apache-had ...

  2. 灵活运用Zend框架

    $aAwardMem = $this->dao_raward->getAwardAndMem($where,'award_level asc',false,false,false,'awa ...

  3. 剑指offer 27二叉搜索树与双向链表

    class Solution { public: void ConvertNode(TreeNode* pRootOfTree,TreeNode** pre) { if(pRootOfTree) { ...

  4. 学习ExtjsFor.NET(第二个案例-Array的Every方法)

    Ext.Array.every(Array array,Function fn,Object scope)是一个遍历的方法. array是数组,fn是方法,scope是作用域.every返回true和 ...

  5. sass安装步骤

    sass 基于Ruby,首先需要安装Ruby.当然也有node-sass,那是另外一种使用方式了.如果能FQ的,就不用看了,主要写给翻不了墙的人用. 1.安装Ruby,ruby下载地址: http:/ ...

  6. select2简单例子

    1.html中静态值 html <%--multiple 为多选--%> <select multiple id="e1"> <option>& ...

  7. pushViewController自定义动画

    实现的主要代码如下: CATransition *transition = [CATransition animation]; transition.duration = 1.0f; transiti ...

  8. Filemanager 的使用

    filemanager的使用包括: 1.创建文件夹 2.删除文件夹 3.写入文件 4.复制文件 5.移动文件 6.删除文件​ 一.创建文件夹​ 首先宏的定义一个字符串作为地址的​来获取当前的docum ...

  9. PHP 单列模式实例讲解以及参考网址

    1,http://blog.csdn.net/jungsagacity/article/details/7618587 2,http://www.cnblogs.com/lh460795/archiv ...

  10. JAVA并发,经典死锁案例-哲学家就餐

    转自:http://blog.csdn.net/tayanxunhua/article/details/38691005 死锁经典案例:哲学家就餐. 这个案例会导致死锁. 通过修改<Java编程 ...