SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)
题目链接: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(莫比乌斯反演)的更多相关文章
- 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 莫比乌斯反演
这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...
- spoj 7001 Visible Lattice Points莫比乌斯反演
Visible Lattice Points Time Limit:7000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数
/** 题目:Visible Lattice Points 链接:https://vjudge.net/contest/178455#problem/A 题意:一个n*n*n大小的三维空间.一侧为(0 ...
- SPOJ 7001 Visible Lattice Points (莫比乌斯反演)
题意:求一个正方体里面,有多少个顶点可以在(0,0,0)位置直接看到,而不被其它点阻挡.也就是说有多少个(x,y,z)组合,满足gcd(x,y,z)==1或有一个0,另外的两个未知数gcd为1 定义f ...
- SPOJ.Visible Lattice Points(莫比乌斯反演)
题目链接 /* http://www.spoj.com/problems/VLATTICE/ 题意:求一个n*n*n的晶体,有多少点可以在(0,0,0)处可以直接看到. 同BZOJ.2301 题目即要 ...
- 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 ...
- [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
随机推荐
- COCOS2d-x简易安装步骤
准备工作:1. 下载 cocos2d-x 下载地址:http://cdn.cocos2d-x.org/cocos2d-x-2.2.zip2. 下载 python 2.7.3 下载地址:h ...
- poj2608---几个字母映射到同一个数字
#include <stdio.h> #include <stdlib.h> #include<string.h> ]={,,,,,,,,,,,,,,,,,,,,, ...
- poj2538---字符匹配(映射)
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { int i,j; ], ...
- current imporant Posts
CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete) http://www.centos.org/docs/5/htm ...
- 程序猿的量化交易之路(20)--Cointrader之Assert实体(8)
转载需说明出处:http://blog.csdn.net/minimicall, http://cloudtrade.top 不论什么可交易的都能够称之为Assert,资产.其类代码例如以下: pac ...
- C++ Primer 读书笔记 第1章
1.1 编写简单的C++程序 每个C++程序都必须包含一个main函数,因为main函数是系统执行入口,且main函数是唯一被系统显示调用的函数. 定义函数必须指定4个元素:返回类型.函数名.形参表. ...
- 自己定义flash的宽和高
前段时间做个项目,是个网页的聊天界面,聊天的内容使用flash制作,我需要将flash的swf插件放到页面上,然后获取聊天内容, 1.将文件在页面上显现出来: 如图,正中间使用后台制作出来的swf文件 ...
- matlab GUI之常用对话框(四)-- 输入对话框 inputdlg、目录对话框 uigetdir、列表对话框 listdlg
常用对话框(四) 1.输入对话框 inputdlg answer = inputdlg(prompt) answer = inputdlg(prompt,dlg_title) answer = in ...
- linq中的cast<T>()及OfType<T>()
DataTable dt=...........//获取从数据库中取出的数据(假设只有一条记录) //Cast<T>()用来将非泛型的序列转换为泛型的序列 DataRow row=dt.R ...
- C++学习之使用new的注意事项
C++学习之使用new的注意事项 在构造函数中使用new来初始化对象的指针成员成员时必须特别小心,具体的说,应该如下这样做: 一.如果在构造函数中使用new来初始化指针成员,则应该在析构函 ...