将直线转化为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. UP board 漫谈(1)——从Atom到UP Board

    title: UP board 漫谈(1)--从Atom到UP Board date: 2016-12-26 12:33:03 tags: UP board categories: 开发板 perma ...

  2. JVM专题目录

    1.Java虚拟机及运行时数据区 2.JVM内存垃圾回收方法 3.JVM内存分配策略 4.HotSpot JVM常用参数设置

  3. js计时器,点击开始计时,再点击停止

    点击倒计时开始,点击停止,再次点击又开始,再点停止... <i id=</i>秒 <em onclick="timeOpen();">开始</e ...

  4. 关于在Xcode控制台打印的注意点

    注意!!在控制台中打印语句的返回值,这句代码也算是被执行过了一次 比如在下列代码的if语句执行之前,现在控制台打印 [_dataBaseexecuteUpdate:createSql] 的布尔值 if ...

  5. 为什么现在更多需要用的是 GPU 而不是 CPU,比如挖矿甚至破解密码?

    作者:Cascade链接:https://www.zhihu.com/question/21231074/answer/20701124来源:知乎著作权归作者所有,转载请联系作者获得授权. 想要理解G ...

  6. 第一次做socket的一些心得

    理论什么的bilibala的就是自己百度吧 推荐一篇不错的关于socket的文章 http://www.cnblogs.com/sunway/archive/2010/01/29/1659074.ht ...

  7. Sublime Text 3 Plugin Better!

    Package Control Cmake ConvertUTF Markdown preview MarkdownEditing Marking Changed Rows

  8. 关于JQ toggle 的注意事项

    1.9.1以后的版本,好像不支持 jq 的 toggle function的用法啦.

  9. shell脚本重新挂载出问题的卷

    #!/bin/bash#卷的全路径示例#pathexample =mount -t cetusfs 127.0.0.1:/cinderv/var/lib/cinder/volumes/0f6a20f4 ...

  10. 关于Access restriction: The type 'Application' is not API (restriction on required library)

    原文链接:http://rxxluowei.iteye.com/blog/671893 今天写第一次写JavaFX的入门程序就GG 遇到了导入API的问题,无奈疯狂地通过网络找解决方案.. 我的问题是 ...