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 ...
随机推荐
- shell脚本学习 (9) 提取开头或结尾的几行
1 提取开头的n行 用head awk或者 sed实现 do.txt sed 1q do.txt awk 'FNR <= 1' do.txt do.txt文件 2 显示行尾的几行 用tail - ...
- [POJ3735]Training little cats
题目:Training little cats 链接:http://poj.org/problem?id=3735 分析: 1)将操作用矩阵表示出来,然后快速幂优化. 2)初始矩阵:$ \left[ ...
- [CSP-S模拟测试]:antipalindrome(数学)
题目传送门(内部题58) 输入格式 第一行一个数$T$表示数据组数.接下来每行两个数$n$和$m$. 输出格式 $T$行,每行一个答案,对${10}^9+7$取模. 样例 样例输入: 25 66 5 ...
- [杂题]:B/b(二分答案)
题目传送门(内部题53) 输入格式 第二行$2$个整数表示$n,m$.接下来$m$行每行两个整数,描述一个点对$(x_i,y_i)$. 输出格式 一个整数,表示最短距离. 样例 样例输入: 6 21 ...
- Transform.Find()
代码演示: using System.Collections;using System.Collections.Generic;using UnityEngine; public class Tran ...
- GameObject.Find
代码演示: using System.Collections;using System.Collections.Generic;using UnityEngine; public class Game ...
- 执行hbase zkcli命令报错
执行hbase zkcli后报错信息如下: 15/10/02 15:17:55 INFO zookeeper.ZooKeeper: Client environment:java.library.pa ...
- 【转】 Linux 命令解释(Linux基础二)
前言 对服务器来讲,图形界面会占用更多的系统资源,而且会安装更多的服务.开放更多的端口,这对服务器的稳定性和安全性都有负面影响.其实,服务器是一个连显示器都没有的家伙,要图形界面干十么? 说到这里,有 ...
- Font and PDF
1. 独立存在的Font文件 有三类: Type 1 Font TrueType Font OpenType Font Type 1 是由Adobe开发的,它是基于PostScript的Font,它通 ...
- Git和Github操作
个人笔记和总结.如有错误欢迎指出https://github.com/zhaozehua0312/leran-gitAndgithub 内容已发布github上