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 ...
随机推荐
- JarvisOJ 逆向Writeup
1. 爬楼梯 先运行apk,查看具体的功能 爬一层楼是可以点击的,爬到了,看FLAG是不可以点击的.我们可以大致的了解到到了具体的楼层才可以看到flag,多次打开软件,楼层数目是随机的. 用APKID ...
- play framework 在idea简单运行配置(mac为例)
文章目录 play 最基本的构建 在idea中配置 配置jdk相关 配置play 运行 运行 play 最基本的构建 https://blog.csdn.net/dataiyangu/article/ ...
- Html5 学习笔记 --》布局
不推荐: 浮动布局: footer 设置 clear : both 清理浮动 | header | |边 | | |内 | 内容 ...
- JavaScript 标准参考教程(alpha) 阮一峰
JavaScript 标准参考教程(alpha)http://javascript.ruanyifeng.com/#introduction
- 54.Counting Bits( 计算1的个数)
Level: Medium 题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ ...
- Golang 读书
var a map[string] int 类似Key\Value var( 多个变量,避免多次声明var ) 声明变量 多重赋值 i,j=j,i 匿名变量 _ literal 字面常量 con ...
- 获取年月日格式为yyyy-m-d简单写法
方法:通过日期函数toLocaleString()获取.new Date().toLocaleString( ); //"2019/9/1 上午11:20:23" 获取年月日方法 ...
- ARM-LINUX学习记录
1:调用C语言函数之前会有一段汇编代码在前面执行来完成软硬件方面的初始化.比如:关闭看门狗:初始化时钟:设置堆栈:调用main函数等.在学习51单片机时候这些操作是由开发环境(如KEIL)在编译C代码 ...
- RabbitMQ学习第三记:发布/订阅模式(Publish/Subscribe)
工作队列模式是直接在生产者与消费者里声明好一个队列,这种情况下消息只会对应同类型的消费者. 举个用户注册的列子:用户在注册完后一般都会发送消息通知用户注册成功(失败).如果在一个系统中,用户注册信息有 ...
- 转 HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...