将直线转化为ax + by = c的形式,然后扩展欧几里得求在[x1, x2]之间的解

对直线与坐标轴平行的特判

调试了好长时间,注意:

1 正负数转化为整型的处理

2 注意判断有无解

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 1008, INF = 0x3F3F3F3F;
const double eps = 1e-6, meps = 1e-7;
LL Ext_gcd(LL a,LL b,LL &x,LL &y){//扩展欧几里得
if(b==0) { x=1, y=0; return a; }
LL ret= Ext_gcd(b,a%b,y,x);
y-= a/b*x;
return ret;
} LL gcd(LL a, LL b){
while(b){
LL t = a % b;
a = b;
b = t;
}
return a;
} LL cal(LL x1, LL x2, LL x, LL mod){
if(x1 > x2){
return 0;
}
if(x >= x1 && x <= x2){
return (x - x1) / mod + 1 + (x2 - x) / mod;
}
if(x < x1){
return (x2 - x) / mod - (x1 - 1 - x) / mod;
}
return (x - x1) / mod - (x - x2 - 1) / mod; }
LL toLL(double x){
if(x > 0){
return x + eps;
}
if(x < 0){
return x - eps;
}
return 0;
}
int main(){
double t1, t2, t3, t4;
int t;
scanf("%d", &t);
while(t--){
scanf("%lf %lf %lf %lf", &t1, &t2, &t3, &t4);
if(abs(t1 - t3) < eps){
if(abs(t1 - (LL)t1) < eps){
double t22 = min(t2, t4);
double t44 = max(t2, t4);
printf("%lld\n", cal((LL)ceil(t22), (LL)floor(t44), (LL)ceil(t22), 1));
}else{
printf("0\n");
}
continue;
} if(abs(t2 - t4) < eps){
if(abs(t2 - (LL)t2) < eps){
double t11 = min(t1, t3);
double t33 = max(t1, t3);
printf("%lld\n", cal((LL)ceil(t11), (LL)floor(t33), (LL)ceil(t11), 1));
}else{
printf("0\n");
}
continue;
} LL x1 = toLL(t1 * 10), y1 = toLL(t2 * 10), x2 = toLL(t3 * 10), y2 = toLL(t4 * 10); //cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<"\n"; if(x1 > x2){
swap(x1, x2);
swap(y1, y2);
}
LL a = (y2 - y1) * 10, b = (x1 - x2) * 10;
LL c = x1 * y2 - x2 * y1;
LL gc = gcd(gcd(a, b), c);
a /= gc;
b /= gc;
c /= gc; //cout<<a<<" "<<b<<" "<<c<<" ***\n";
if(c % gcd(a, b)){
printf("0\n");
continue;
} //cout<<a * t1 + b * t2 - c<<" aa\n";
//cout<<a * t3 + b * t4 - c<<" aa\n";
LL x, y;
Ext_gcd(a, b, x, y);
//cout<<x<<" "<<y<<" xy\n";
x = c / gcd(a, b) * x;
//cout<<x<<" spe\n";
if(t1 > t3){
swap(t1, t3);
}
printf("%lld\n",cal((LL)ceil(t1), (LL)floor(t3), x, abs(b / gcd(a, b))));
//cout<<tp<<" cal\n";
} return 0;
}

  

UVA 11768 Lattice Point or Not(扩展欧几里德)的更多相关文章

  1. UVA - 11768 Lattice Point or Not (扩展欧几里得)

    求一条线段上有多少个整点. 是道扩欧基础题,列出两点式方程,然后分四种情况讨论即可.但细节处理较多很容易写挫(某zzWA了十几发才过掉的). 由于数据精度较小,浮点数比较没有用eps,直接==比较了. ...

  2. UVA 11768 - Lattice Point or Not(数论)

    UVA 11768 - Lattice Point or Not option=com_onlinejudge&Itemid=8&page=show_problem&categ ...

  3. UVA 11768 - Lattice Point or Not

    首先本题需要用到扩展欧几里得算法…… 关于exgcd算法的一点简略证明: 那么,对于函数exgcd(a,b)=(d,x,y),其中d满足d=gcd(a,b); (x,y)满足ax+by=d; 则exg ...

  4. UVa 11768 格点判定(扩展欧几里得求线段整点)

    https://vjudge.net/problem/UVA-11768 题意: 给定两个点A(x1,y1)和B(x2,y2),均为0.1的整数倍.统计选段AB穿过多少个整点. 思路: 做了这道题之后 ...

  5. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  6. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  7. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

  8. 51nod 1352 扩展欧几里德

    给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...

  9. CF 7C. Line(扩展欧几里德)

    题目链接 AC了.经典问题,a*x+b*y+c = 0整数点,有些忘记了扩展欧几里德,复习一下. #include <cstdio> #include <iostream> # ...

随机推荐

  1. JQuery------分页插件下载地址

    转载GitHub: https://github.com/pgkk/kkpager

  2. photoshop常见抠图方法

    1.多边形套索:这种工具是用于抠图的边界比较平直,但颜色比较复杂类的图像,它也是最笨最无奈的方法,只能是利用鼠标一点一点去点击抠选.2.磁性套索工具:分为三种:套索,多边形,磁性.这类工具一般只用于边 ...

  3. js设置鼠标悬停改变背景色

    看了网上那么多的js鼠标悬停时事件,大多数的,说了那么多话,也没解决什么问题,现在直接贴上代码,以供参考 html: <div id="sign">this is te ...

  4. easyui如何动态改变列的编辑属性

    动态改变列的编辑属性 var tt=$('#dg').datagrid('getColumnOption', 'yearContent'); //通过列名获得此列 tt.editor={type:'t ...

  5. mybatis问题is not known to the MapperRegistry

    is not known to the MapperRegistry mapper.xml里面对应的dao应该是interface而非class

  6. Ubuntu安装SSH服务器故障分析及解决办法(错误1:E:软件包 openssh-server 还没有可供安装的候选者,错误2:E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系)

    •    微博: 小样儿老师2015 Windows下做Linux开发需要SSH强大功能的支持.安装SSH的过程会出现了很多问题,看完这篇文章可以让你少走些弯路,PS:折腾一下午的成果. Ubuntu ...

  7. Git 如何只更新项目中某个目录里的文件

    Git由于在远端和本地都有一个代码库, 这样更新单个文件比SVN要麻烦一点.   1. 如果想拿远端git服务器上的最新版本(或某个特定版本)覆盖本地的修改,可以使用git pull命令,   但这会 ...

  8. ajax跨域解决方案2

    配置文件添加: <system.webServer>       <httpProtocol>        <customHeaders>          &l ...

  9. CI加载model的问题

    1.需求 CI在linux上无法加载model 2.原因 因为linux区分大小写,且model文件名首字符要大写. As said in the comments : Your model's fi ...

  10. tail命令详解

    搜索 纠正错误  添加实例 tail 在屏幕上显示指定文件的末尾若干行 补充说明 tail命令 用于输入文件中的尾部内容.tail命令默认在屏幕上显示指定文件的末尾10行.如果给定的文件不止一个,则在 ...