隐藏页面特效

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

 

 

Description(题意)

N*N*N网格. 一个角落在 (0,0,0),对顶角落是 (N,N,N). 问从(0,0,0)看有多少个格点是可见的?点 X从点Y可见,当且仅当,线段XY上没有其他的点。

Input:

第一行是测试数据个数T。接着有T行每行有一个整数 N.

Output :

输出T行,每行是对应的可见格点的个数。

Sample Input :

3

1

2

5

Sample Output :

7

19

175

 

Constraints :

T <= 50

1 <= N <= 1000000

 

Solution:

#include<cstdio> #include<iostream> #ifdef WIN32 #define LL "%I64d" #else #define LL "%lld" #endif using namespace std; typedef long long ll; const int M=1e6+5; int n,m,T;ll sum[M]; int tot,prime[M/3],mu[M];bool check[M]; void sieve(){ n=1e6;mu[1]=1; for(int i=2;i<=n;i++){ if(!check[i]) prime[++tot]=i,mu[i]=-1; for(int j=1;j<=tot&&i*prime[j]<=n;j++){ check[i*prime[j]]=1; if(!(i%prime[j])){mu[i*prime[j]]=0;break;} else mu[i*prime[j]]=-mu[i]; } } for(int i=1;i<=n;i++) sum[i]=sum[i-1]+mu[i]; } inline ll s2(int x){return 1LL*x*x;} inline ll s3(int x){return 1LL*x*x*x;} inline ll solve(int n){ ll ans=3; for(int i=1,pos;i<=n;i=pos+1){ pos=n/(n/i); ans+=s3(n/i)*(sum[pos]-sum[i-1]); ans+=3*s2(n/i)*(sum[pos]-sum[i-1]); } return ans; } int main(){ sieve(); for(scanf("%d",&T);T--;){ scanf("%d",&n); printf(LL"\n",solve(n)); } return 0; }

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6748677.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(327)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示