Visible Lattice Points(规律题)【数学规律】
Visible Lattice Points
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9031 | Accepted: 5490 |
Description
A lattice point (x, y) 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 (x, y) 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 (x, y) with 0 ≤ x, y ≤ 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 (x, y) with 0 ≤ x, y ≤ 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(规律题)【数学规律】的更多相关文章
- 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 ...
- poj 3060 Visible Lattice Points
http://poj.org/problem?id=3090 Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Tota ...
- SPOJ 7001. Visible Lattice Points (莫比乌斯反演)
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演
SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...
- Visible Lattice Points (莫比乌斯反演)
Visible Lattice Points 题意 : 从(0,0,0)出发在(N,N,N)范围内有多少条不从重合的直线:我们只要求gcd(x,y,z) = 1; 的点有多少个就可以了: 比如 : 点 ...
- spoj 7001 Visible Lattice Points莫比乌斯反演
Visible Lattice Points Time Limit:7000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- 数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5636 Accepted: ...
- Spoj 7001 Visible Lattice Points 莫比乌斯,分块
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193 Visible Lattice Points Time L ...
- P8 Visible Lattice Points
P8 Visible Lattice Points Time Limit:1000ms, Memory Limit:65536KB Description A lattice point (x ...
随机推荐
- E. Alternating Tree 树点分治|树形DP
题意:给你一颗树,然后这颗树有n*n条路径,a->b和b->a算是一条,然后路径的权值是 vi*(-1)^(i+1) 注意是点有权值. 从上头往下考虑是点分治,从下向上考虑就是树形DP, ...
- 【JVM】堆区域的一个详细了解并附带调优案例
话不多说,直接撸图: 1>Eden中通过可达性分析,存活下来的对象直接通过复制算法移动到From区域中,此时该对象的分代年龄加1: 2>当下一次虚拟机进行[Minor GC]时,会同时对[ ...
- Poj2965 冰箱的开关
#include<iostream> using namespace std; int flag; int step; ][]; ] = { }; ] = { }; void turn(i ...
- vi和软件安装
一 vi编辑器简介 vim 全屏幕纯文本编辑器 二 vim使用 1 vi 模式 vi 文件名 命令模式 输入模式 末行模式 命令---->输入 a:追加 i:插入 o:打开 ...
- Oracle 中同义词使用
一.数据库对象: 模式对象: 数据库对象是逻辑结构的集合,最基本的数据库对象是表; 其他对象包括:create增.drop删.改alter 同义词.序列.视图.索引 1.同义词: ①. 现有对象的一个 ...
- Unity调用Android Studio中的Java方法
1. 新建Unity项目: 2. Android Studio中新建EmptyActivity: 3. 新建安卓项目时记住最小版本号: 4. 将左侧项目文件浏览面板切换到Project项下,在本项根节 ...
- protocbuf的简单理解
之前通信协议替换为protocbuf!新老交替,很多不同看法,也提出来一些负面因数: 1.老的内部通信协议体已经有一段时间了,稳定熟悉! 2.通过通信结构体进行交互,实际上并没有序列化和反序列化的过程 ...
- 个人工具,编辑器visual studio code
个人收集的使用方法:简化版 主要基于基础web前端开发,visual studio code教程——基础使用.扩展插件安装使用 下载地址: https://visualstudio.microsoft ...
- jQuery-语言基础整理
jQuery是js的一个类库,主要封装的是js中DOM操作部分,使用和原生js一样 1.需要先引入页面才可以使用 代码引入:<script src='jquery.js'></scr ...
- Rocket - diplomacy - LazyModuleImpLike
https://mp.weixin.qq.com/s/gDbUto1qd7uWbpnxovr5pg 介绍LazyModuleImpLike类的实现. 1. wrapper LazyMo ...