Visible Lattice Points

题目链接(点击)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9031   Accepted: 5490

Description

A lattice point (xy) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (xy) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (xy) with 0 ≤ xy ≤ 5 with lines from the origin to the visible points.

Write a program which, given a value for the size, N, computes the number of visible points (xy) with 0 ≤ xy ≤ N.

Input

The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.

Output

For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.

Sample Input

4
2
4
5
231

Sample Output

1 2 5
2 4 13
3 5 21
4 231 32549

思路:

问题:从原点出发的射线从x轴开始逆时针旋转,如果射线穿过某点则这个点 则该点可以被看到 求可以看到的点的个数总和

知道这个题是有规律(找到斜率相同且最先出现的点)直接看 看了好久也没找到,最后自己索性把所有要被与原点相连接的点打印出来 就可以看出来了

输出如图:

三个值分别表示:

x   y   k

规律:如果x是奇数(例如x=7) 需要满足gcd(x,y)==1的点 若是偶数同理

即:x与y互质 (佩服同学能直接看出来互质……)

下面是找规律的代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAX=1e6;
struct node{
double count2;
LL x;
LL y;
}num[MAX+5];
struct node1{
LL x1;
LL y1;
double num3;
}edge[MAX+5];
bool cmp(node a,node b)
{
if(a.count2==b.count2){
return a.x<b.x;
}
else{
return a.count2<b.count2;
}
}
bool cmp1(node1 a,node1 b)
{
if(a.x1!=b.x1){
return a.x1<b.x1;
}
return a.y1<b.y1;
}
int main()
{
LL count,T,n;
scanf("%lld",&T);
for(LL k=1;k<=T;k++){
scanf("%lld",&n);
count=0;
for(LL i=2;i<=n;i++){
for(LL j=1;j<i;j++){
num[count].count2=(j*1.0)/(i*1.0);
num[count].x=i;
num[count].y=j;
count++;
}
}
sort(num,num+count,cmp);
LL count1=0,count3=0;
for(LL i=0;i<count;i++){
if(num[i].count2!=num[i-1].count2){
edge[count3].x1=num[i].x;
edge[count3].y1=num[i].y;
edge[count3++].num3=num[i].count2;
// printf("*%lld %lld %.2lf\n",num[i].x,num[i].y,num[i].count2);
count1++;
}
}
sort(edge,edge+count3,cmp1);
for(int i=0;i<count3;i++){
printf("*%lld %lld %.2lf\n",edge[i].x1,edge[i].y1,edge[i].num3);
}
if(n==1){
printf("%lld 1 3\n",k);
}
else{
LL sum=3;
sum+=count1*2;
printf("%lld %lld %lld\n",k,n,sum);
}
}
return 0;
}

AC代码:

(找规律接近100行 但AC却只是40行左右)

#include<stdio.h>
typedef long long LL;
const int MAX=1e5;
int gcd(int a,int b)
{
if(b==0){
return a;
}
return gcd(b,a%b);
}
int main()
{
LL num[MAX+5]={0},T;
num[1]=1;
for(int i=2;i<=1000;i++){
LL count=0;
if(i%2==0){
for(int j=1;j<i;j+=2){
if(gcd(i,j)==1){
count++;
}
}
}
else{
for(int j=1;j<i;j++){
if(gcd(i,j)==1){
count++;
}
}
}
num[i]=num[i-1]+count;
}
scanf("%lld",&T);
for(LL k=1;k<=T;k++){
LL n,sum=0;
scanf("%lld",&n);
sum+=(num[n]*2+1);
printf("%lld %lld %lld\n",k,n,sum);
}
return 0;
}

Visible Lattice Points(规律题)【数学规律】的更多相关文章

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

  2. poj 3060 Visible Lattice Points

    http://poj.org/problem?id=3090 Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  3. SPOJ 7001. Visible Lattice Points (莫比乌斯反演)

    7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...

  4. spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演

    SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...

  5. Visible Lattice Points (莫比乌斯反演)

    Visible Lattice Points 题意 : 从(0,0,0)出发在(N,N,N)范围内有多少条不从重合的直线:我们只要求gcd(x,y,z) = 1; 的点有多少个就可以了: 比如 : 点 ...

  6. spoj 7001 Visible Lattice Points莫比乌斯反演

    Visible Lattice Points Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

  7. 数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

    Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: ...

  8. Spoj 7001 Visible Lattice Points 莫比乌斯,分块

    题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193   Visible Lattice Points Time L ...

  9. P8 Visible Lattice Points

    P8 Visible Lattice Points Time Limit:1000ms,     Memory Limit:65536KB Description A lattice point (x ...

随机推荐

  1. [翻译]用于.NET Core的Windows窗体设计器发布

    本文由微信公众号<开发者精选资讯>翻译首发,转载请注明来源 今天我们很高兴地宣布,.NET Core项目的Windows窗体设计器现在可以在 Visual Studio 2019 16.6 ...

  2. Hyperledger Fabric——balance transfer(六)查询

    balance transfer 提供了很多查询接口,包括链码查询,根据区块号查询区块数据,根据交易ID查询交易信息,查询链上的区块数,查询已安装或已实例化的链码,查询通道. 源码解析 1.调用链码查 ...

  3. 矩阵重叠面积计算 线段树hdu1542

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. docker常用命令,及进入Tomcat的WebApps发布目录(就是进入docker容器后台目录)

    docker常用命令,及进入Tomcat的WebApps发布目录(就是进入docker容器后台目录)   一.常用命令 1.显示所有的容器,包括未运行的 docker ps -a   2.启动容器.注 ...

  5. 重学 Java 设计模式:实战建造者模式

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 乱码七糟 [luàn qī bā zāo],我时常怀疑这个成语 ...

  6. 要小心 JavaScript 的事件代理

    我们知道,如果给 form 里面的 button 元素绑定事件,需要考虑它是否会触发 form 的 submit 行为.除此之外,其它场合给 button 元素绑定事件,你几乎不用担心这个事件会有什么 ...

  7. Docker 入门:Dockerfile

    主要内容: 什么是 Dockerfile 查看 DockerHub 中镜像的 Dockerfile Dockerfile 编写 Dockerfile 常用命令 什么是 Dockerfile 使用 Do ...

  8. idea 开发 webpack项目时,只要已加入SVN 版本控制 一直 updating 问题解决

    场景描述,这是一个困扰我很久的一个问题,一直百度,都解决不了,今天自己通过设置终于解决了,慢慢的都是辛酸泪,赶快写个笔记记录一下. 对于idea 开发 vue-cli+webpack 项目,idea  ...

  9. 小谢第6问:js中,filter函数是怎么使用的

    数组的常用方法filter,今天在做数组筛选的时候用到需要将有重复的数组去除,因此用到这个函数,主要用到-- 选择需要的属性,最终留下想要的数组,如果刚开始的话可以看下下面代码 let nums = ...

  10. WebAPI之FormData

    MDNformdata参考--https://developer.mozilla.org/zh-CN/docs/Web/API/FormData MDNformdata参考--https://deve ...