spoj7001. Visible Lattice Poin
来源:互联网 发布:手机大型网络枪战游戏 编辑:程序博客网 时间:2024/06/01 09:28
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
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
这题就是求gcd(a,b,c) = 1 a,b,c <=N 的对数。
用莫比乌斯反演可以求解。
设g(n)为gcd(x,y,z)=n的个数,f(n)为n | g(i*n)的个数,那么有f(n)=sigma(n|d,g(d)),那么g(n)=sigma(n|d, mu(d/n)*f(d)),我们要求g(1),则g(1)=sigma(n|d, mu(d)*f(d)),
因为f(d)=(n/d)*(n/d)*(n/d),所以g(1)=sigma( mu(d)*(n/d)*(n/d)*(n/d) ).
#include<iostream>#include<cstring>#include<cstdio>#include<ostream>#include<istream>#include<algorithm>#include<queue>#include<string>#include<cmath>#include<set>#include<map>#include<stack>#include<vector>#define fi first#define se second#define ll long long#define pii pair<int,int>#define inf (1<<30)#define eps 1e-8#define pb push_backusing namespace std;const int maxn=1000010;int n;int mu[maxn],prime[maxn],tot;bool check[maxn];void getmu(){ int N=1000000; memset(check,0,sizeof(check)); mu[1]=1,tot=0; for(int i=2;i<=N;i++){ if(!check[i]) prime[tot++]=i,mu[i]=-1; for(int j=0;j<tot;j++){ if(i*prime[j]>N) break; check[i*prime[j]]=true; if(i%prime[j]==0){ mu[i*prime[j]]=0; break; } else mu[i*prime[j]]=-mu[i]; } }}int main(){ getmu(); int t; scanf("%d",&t); while(t--) { scanf("%d",&n); ll ans=3; for(int i=1;i<=n;i++) { ans+=(ll)mu[i]*(n/i)*(n/i)*(n/i)+(ll)mu[i]*(n/i)*(n/i)*3; } printf("%lld\n",ans); } return 0;}
0 0
- spoj7001. Visible Lattice Poin
- 【SPOJ7001】Visible Lattice Points-莫比乌斯反演+分块
- SPOJ7001 Visible Lattice Points 莫比乌斯反演+分块+前缀和
- Visible Lattice Points(spoj7001+初探莫比乌斯)gcd(a,b,c)=1 经典
- pku3090 Visible Lattice Points
- poj3090-Visible Lattice Points
- poj3090-Visible Lattice Points
- Visible Lattice Points
- P8 Visible Lattice Points
- poj3090 Visible Lattice Points
- [POJ3090]Visible Lattice Points
- POJ3090 Visible Lattice Points
- Visible Lattice Points
- Visible Lattice Points POJ
- Visible Lattice Points POJ
- pku 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- ZOJ2777 POJ2090 Visible Lattice Points
- Linux虚拟机设置网络、实现主机名ssh访问
- LeetCode 26: Remove Duplicates from Sorted Array
- android---(文件管理)
- 关于数组面试小题
- Apprenticeship Pattern
- spoj7001. Visible Lattice Poin
- 【推荐】SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门
- 前置加加后置加加,易出错题目
- Banner图片都是全屏宽度在不同分辨率下都是显示图片的中间部分
- IOS开发系列--Objective-C之协议、代码块、分类
- Graph DataBase介绍
- 64位Linux编译cximage手记
- 排序算法
- SimHash