E. Covered Points (线段上的整点数)
题目链接:https://codeforces.com/contest/1036/problem/E
思路:学会了一个在线段上的整数点等于 GCD(x1 - x2, y1 - y2) + 1,然后去重线段相交的重复整点。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-;
const int maxn = 1e3 + ;
int sgn(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
}
struct Point{
double x, y;
Point(){}
Point(double _x, double _y){
x = _x, y = _y;
}
void input(){
scanf("%lf%lf", &x, &y);
}
bool operator == (Point b) const{
return sgn(x - b.x) == && sgn(y - b.y) == ;
}
bool operator < (Point b)const{
return sgn(x - b.x) == ? sgn(y - b.y < ) : x < b.x;
}
Point operator - (const Point &b)const{
return Point(x - b.x, y - b.y);
}
double operator ^(const Point &b){
return x * b.y - y * b.x;
}
double operator *(const Point &b){
return x * b.x + y * b.y;
}
};
struct Line{
Point s, e;
Line(){}
Line(Point _s, Point _e){s = _s, e = _e;}
bool operator == (Line v){
return (s == v.s) && (e == v.e);
}
void input(){
s.input();
e.input();
}
int segcrossing(Line v)
{
int d1 = sgn((e - s)^(v.s - s));
int d2 = sgn((e - s)^(v.e - s));
int d3 = sgn((v.e - v.s)^(s - v.s));
int d4 = sgn((v.e - v.s)^(e - v.s));
if( (d1^d2) == - && (d3^d4) == - )return ;
return (d1 == && sgn((v.s - s)*(v.s - e)) <= ) ||
(d2 == && sgn((v.e - s)*(v.e - e)) <= ) ||
(d3 == && sgn((s - v.s)*(s - v.e))<=) ||
(d4 == && sgn((e - v.s)*(e - v.e))<=);
}
Point crosspoint(Line v){
double a1 = (v.e - v.s)^(s - v.s);
double a2 = (v.e - v.s)^(e - v.s);
return Point((s.x*a2 - e.x*a1)/(a2 - a1),(s.y*a2 - e.y*a1)/(a2 - a1));
}
};
Line l[maxn];
int main()
{
std::ios::sync_with_stdio(false);
int n;
cin >> n;
ll ans = ;
int x1, x2, y1, y2;
for(int i = ;i < n;i++){
cin >> x1 >> y1 >> x2 >> y2;
l[i] = Line(Point((double)x1, (double)y1), Point((double)x2, (double)y2));
ans += __gcd(abs(x1 - x2), abs(y1 - y2)) + ;
}
for(int i = ;i < n;i++)
{
set< pair<int, int> >st;
for(int j = i + ;j < n;j++)
{
if(l[i].segcrossing(l[j])){
Point v = l[i].crosspoint(l[j]);
if((int)v.x == v.x && (int)v.y == v.y)
st.insert({v.x,v.y});
}
}
ans -= st.size();
}
cout << ans << endl;
return ;
}
E. Covered Points (线段上的整点数)的更多相关文章
- Codeforces 1036E Covered Points (线段覆盖的整点数)【计算几何】
<题目链接> <转载于 >>> > 题目大意: 在二维平面上给出n条不共线的线段(线段端点是整数),问这些线段总共覆盖到了多少个整数点. 解题分析: 用GC ...
- EDU 50 E. Covered Points 利用克莱姆法则计算线段交点
E. Covered Points 利用克莱姆法则计算线段交点.n^2枚举,最后把个数开方,从ans中减去. ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算. #include ...
- POJ 1265 /// 皮克定理+多边形边上整点数+多边形面积
题目大意: 默认从零点开始 给定n次x y上的移动距离 组成一个n边形(可能为凹多边形) 输出其 内部整点数 边上整点数 面积 皮克定理 多边形面积s = 其内部整点in + 其边上整点li / 2 ...
- BZOJ 1041: [HAOI2008]圆上的整点
1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3621 Solved: 1605[Submit][Sta ...
- BZOJ 1041: [HAOI2008]圆上的整点【数论,解方程】
1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4210 Solved: 1908[Submit][Sta ...
- [ HAOI 2008 ] 圆上的整点
\(\\\) Description 给出一个整数 \(r\) ,求圆 \(x^2+y^2=r^2\) 上的整点数. \(r\le 2\times 10^9\) \(\\\) Solution 神题. ...
- Covered Points Count CF1000C 思维 前缀和 贪心
Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- C - Covered Points Count CodeForces - 1000C (差分,离散化,统计)
C - Covered Points Count CodeForces - 1000C You are given nn segments on a coordinate line; each end ...
- 「Luogu P2508」[HAOI2008]圆上的整点 解题报告
题面 给定圆的半径,求圆上整点数 这是一道很Nice的数学题!超爱!好吧,由于这道题,我去Study了一下复数(complex number)复杂的数 真棒!!! 有兴趣的戳这里!!!\(\huge ...
随机推荐
- redis 服务器端安装(三)
redis 服务器端安装(三) Redis is an open source, BSD licensed, advanced key-value store. It is often referre ...
- CSS属性去除图片链接时的虚线框
CSS 之outline (轮廓)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用.outline 属性是一个简写属性,用于设置元素周围的轮廓线.注释:轮廓线不会占据空间,也不一定是 ...
- XAMPP【phpmyadmin】外网访问被拒绝解决办法
问题场景: 在阿里云搭建一个apache服务器,正常访问XAMPP目录下的页面. 服务器本地是可以正常访问的 但是远程 就不可以访问了: 出现这样的画面: 解决方法 1.按照提示找到httpd-xam ...
- 北京邀请赛 B. Beautiful Garden
题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,近期水的要死,看了队友的代码做的 #include &l ...
- python基础类型(字典:dict)
字典的介绍: 字典(dict)Python中唯一的一个映射类型.他是以{}括起来的键值对组成,在dict中key是唯一的.在保存的时候,根据key来计算出一个内存地址,然后将key-value保存到这 ...
- 在Linux上搭建私有git仓库
最近在学Linux,顺便将自己的服务器用起来,不然又得废弃一年.这次是跟着网上的教程做一个简单的git私有仓库,复杂完整的git系统还需使用gitlib系统. 首先在linux上安装git yum i ...
- java当中的Timer定时器的4种使用方式
import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask ...
- Pandas之loc\iloc\ix
---------------------------------------------------------------------------------------------------- ...
- Struts2学习笔记 - namespace命名空间
默认的命名空间“ namespace="" ”. 根命名空间 “ namespace="/" ”. <package name="test&qu ...
- 转帖 移动端h5页面不同尺寸屏幕适配兼容方法
1. viewport属性及html页面结构 <meta name="viewport" content="width=device-width,initial ...