SPOJ - 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
题意就是给你一个三维的地图,坐标为(0,0,0)∼(n,n,n),判断有多少个坐标与原点之间的连线不经过其他的点。
思路:统计答案的点分为三类
1.坐标轴上的点(1,0,0)(0,1,0)(0,0,1) 三个
2.xoy,xoz,xoy面上的点gcd(i,j)==1; 二维很简单
3.其他点 gcd(i,j,k)==1
代码如下:
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = +;
- int p[maxn],mo[maxn],phi[maxn],cnt=;
- bool vis[maxn];
- void init()
- {
- mo[]=;
- phi[]=;
- for(int i=;i<=maxn-;i++){
- if(!vis[i]){
- mo[i]=-;
- phi[i]=i-;
- p[cnt++]=i;
- }
- for(int j=;j<cnt&&(ll)i*p[j]<=maxn-;j++){
- vis[i*p[j]]=true;
- if(i%p[j]==){
- mo[i*p[j]]=;
- phi[i*p[j]]=phi[i]*p[j];
- break;
- }
- mo[i*p[j]]=-mo[i];
- phi[i*p[j]]=phi[i]*(p[j]-);
- }
- }
- }
- int n;
- int main()
- {
- //freopen("de.txt","r",stdin);
- init();
- int T;
- scanf("%d",&T);
- while (T--){
- scanf("%d",&n);
- ll ans = ;
- for (int i=;i<=n;++i){
- ans+=(ll)mo[i]*(n/i)*(n/i)*(n/i);
- }
- for (int i=;i<=n;++i){
- ans+=(ll)mo[i]*(n/i)*(n/i)*;
- }
- printf("%lld\n",ans+);
- }
- return ;
- }
SPOJ - VLATTICE (莫比乌斯反演)的更多相关文章
- SPOJ PGCD(莫比乌斯反演)
传送门:Primes in GCD Table 题意:给定两个数和,其中,,求为质数的有多少对?其中和的范围是. 分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0). ...
- bzoj 2820 / SPOJ PGCD 莫比乌斯反演
那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j= ...
- 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 VLATTICE - Visible Lattice Points(莫比乌斯反演)
题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(a, b, c) = 1 a,b,c <=N 的对数. 思路:我们令函数g(x)为g ...
- 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 (莫比乌斯反演基础题)
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 数论 莫比乌斯反演
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演
这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...
- SPOJ VLATTICE Visible Lattice Points(莫比乌斯反演)题解
题意: 有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点. 思路: 按题意研究一下就会发现题目所求为. \[(\sum_{i=1}^n\sum_{j=1}^n\su ...
随机推荐
- tarjan-LCA模板
洛谷P3379 #include <cstdio> using namespace std; ; struct etype{ int t,next; }; struct qtype{ in ...
- Python构造器及析构器:__init__与__new__及__del__
__init__与__new__这两个魔法方法组成了Python类对象的构造器,在Python类实例化时,其实最先调用的不是__init__而是__new__.__new__是负责实例化对象的,而__ ...
- Android Studio使用tips
安装位置:C:\Users\xxx\AppData\Local\Android\sdk https://developer.android.com/topic/libraries/support-li ...
- Codeforces 510C (拓扑排序)
原题:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test:2 seconds mem ...
- error C2065: “SHCNE_DELETE”: 未声明的标识符
转自VC错误:http://www.vcerror.com/?p=1383 问题描述: 编译时出现: error C2065: "SHCNE_DELETE": 未声明的标识符 er ...
- spring4.1.8扩展实战之三:广播与监听
提到广播与监听,我们常常会想到RabbitMQ.Kafka等消息中间件,这些常用于分布式系统中多个应用之间,有时候应用自身内部也有广播和监听的需求(例如某个核心数据发生变化后,有些业务模块希望立即被感 ...
- 详解Linux运维工具:运维流程管理、运维发布变更、运维监控告警
概述 应用上线后,运维工作才刚开始,具体工作可能包括:升级版本上线工作.服务监控.应用状态统计.日常服务状态巡检.突发故障处理.服务日常变更调整.集群管理.服务性能评估优化.数据库管理优化.随着应用 ...
- Python笔记(五)_内置函数BIF
查看所有的内置函数:dir(__builtins__) abs() 获取绝对值 max() 返回给定元素中的最大值 min() 返回给定元素中的最小值 sum() 求和 reverse ...
- upc组队赛14 Communication【并查集+floyd /Tarjan】
Communication 题目描述 The Ministry of Communication has an extremely wonderful message system, designed ...
- Linux目录及说明
Linux目录及说明 [常见目录说明] 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目 ...