LA 4973 Ardenia (3D Geometry + Simulation)
三维几何,题意是要求求出两条空间线段的距离。题目难度在于要求用有理数的形式输出,这就要求写一个有理数类了。
开始的时候写出来的有理数类就各种疯狂乱套,TLE的结果是显然的。后来发现,在计算距离前都是不用用到有理数类的,所以就将开始的部分有理数改成直接用long long。其实好像可以用int来做的,不过我的方法比较残暴,中间运算过程居然爆int了。所以就只好用long long了。
代码如下,附带debug以及各种强的数据:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a;}
template <class T> T sqr(T x) { return x * x;}
typedef long long LL;
struct Rat {
LL a, b;
Rat() {}
Rat(LL x) { a = x, b = ;}
Rat(LL _a, LL _b) {
LL GCD = gcd(_b, _a);
a = _a / GCD, b = _b / GCD;
}
double val() { return (double) a / b;}
bool operator < (Rat x) const { return a * x.b < b * x.a;}
bool operator > (Rat x) const { return a * x.b > b * x.a;}
bool operator == (Rat x) const { return a * x.b == b * x.a;}
bool operator < (LL x) const { return Rat(a, b) < Rat(x);}
bool operator > (LL x) const { return Rat(a, b) > Rat(x);}
bool operator == (LL x) const { return Rat(a, b) == Rat(x);}
Rat operator + (Rat x) {
LL tb = b * x.b, ta = a * x.b + b * x.a;
LL GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
Rat operator - (Rat x) {
LL tb = b * x.b, ta = a * x.b - b * x.a;
LL GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
Rat operator * (Rat x) {
if (a * x.a == ) return Rat(, );
// if (b * x.b == 0) { puts("..."); while (1) ;}
LL tb = b * x.b, ta = a * x.a;
LL GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
Rat operator / (Rat x) {
if (a * x.b == ) return Rat(, );
// if (b * x.a == 0) { puts("!!!"); while (1) ;}
LL GCD, tb = b * x.a, ta = a * x.b;
GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
void fix() {
a = abs(a), b = abs(b);
LL GCD = gcd(b, a);
a /= GCD, b /= GCD;
}
} ; struct Point {
LL x[];
Point operator + (Point a) {
Point ret;
for (int i = ; i < ; i++) ret.x[i] = x[i] + a.x[i];
return ret;
}
Point operator - (Point a) {
Point ret;
for (int i = ; i < ; i++) ret.x[i] = x[i] - a.x[i];
return ret;
}
Point operator * (LL p) {
Point ret;
for (int i = ; i < ; i++) ret.x[i] = x[i] * p;
return ret;
}
bool operator == (Point a) const {
for (int i = ; i < ; i++) if (!(x[i] == a.x[i])) return false;
return true;
}
void print() {
for (int i = ; i < ; i++) cout << x[i] << ' ';
cout << endl;
}
} ;
typedef Point Vec; struct Line {
Point s, t;
Line() {}
Line (Point s, Point t) : s(s), t(t) {}
Vec vec() { return t - s;}
} ;
typedef Line Seg; LL dotDet(Vec a, Vec b) {
LL ret = ;
for (int i = ; i < ; i++) ret = ret + a.x[i] * b.x[i];
return ret;
} Vec crossDet(Vec a, Vec b) {
Vec ret;
for (int i = ; i < ; i++) {
ret.x[i] = a.x[(i + ) % ] * b.x[(i + ) % ] - a.x[(i + ) % ] * b.x[(i + ) % ];
}
return ret;
} inline LL vecLen(Vec x) { return dotDet(x, x);}
inline bool parallel(Line a, Line b) { return vecLen(crossDet(a.vec(), b.vec())) == ;}
inline bool onSeg(Point x, Point a, Point b) { return parallel(Line(a, x), Line(b, x)) && dotDet(a - x, b - x) < ;}
inline bool onSeg(Point x, Seg s) { return onSeg(x, s.s, s.t);} Rat pt2Seg(Point p, Point a, Point b) {
if (a == b) return Rat(vecLen(p - a));
Vec v1 = b - a, v2 = p - a, v3 = p - b;
if (dotDet(v1, v2) < ) return Rat(vecLen(v2));
else if (dotDet(v1, v3) > ) return Rat(vecLen(v3));
else return Rat(vecLen(crossDet(v1, v2)), vecLen(v1));
}
inline Rat pt2Seg(Point p, Seg s) { return pt2Seg(p, s.s, s.t);}
inline Rat pt2Plane(Point p, Point p0, Vec n) { return Rat(sqr(dotDet(p - p0, n)), vecLen(n));}
inline bool segIntersect(Line a, Line b) {
Vec v1 = crossDet(a.s - b.s, a.t - b.s);
Vec v2 = crossDet(a.s - b.t, a.t - b.t);
Vec v3 = crossDet(b.s - a.s, b.t - a.s);
Vec v4 = crossDet(b.s - a.t, b.t - a.t);
// v1.print();
// v2.print();
// cout << dotDet(v1, v2).val() << "= =" << endl;
return dotDet(v1, v2) < && dotDet(v3, v4) < ;
}// cout << "same plane" << endl; pair<Rat, Rat> getIntersect(Line a, Line b) {
Point p = a.s, q = b.s;
Vec v = a.vec(), u = b.vec();
LL uv = dotDet(u, v), vv = dotDet(v, v), uu = dotDet(u, u);
LL pv = dotDet(p, v), qv = dotDet(q, v), pu = dotDet(p, u), qu = dotDet(q, u);
if (uv == ) return make_pair(Rat(qv - pv, vv), Rat(pu - qu, uu));
// if (vv == 0 || uv == 0 || uv / vv - uu / uv == 0) { puts("shit!"); while (1) ;}
Rat y = (Rat(pv - qv, vv) - Rat(pu - qu, uv)) / (Rat(uv, vv) - Rat(uu, uv));
Rat x = (y * uv - pv + qv) / vv;
// cout << x.a << ' ' << x.b << ' ' << y.a << ' ' << y.b << endl;
return make_pair(x, y);
} void work(Point *pt) {
Line a = Line(pt[], pt[]);
Line b = Line(pt[], pt[]);
if (parallel(a, b)) {
if (onSeg(pt[], b) || onSeg(pt[], b)) { puts("0 1"); return ;}
if (onSeg(pt[], a) || onSeg(pt[], a)) { puts("0 1"); return ;}
// cout << "parallel" << endl;
Rat tmp = min(min(pt2Seg(pt[], b), pt2Seg(pt[], b)), min(pt2Seg(pt[], a), pt2Seg(pt[], a)));
tmp.fix();
printf("%lld %lld\n", tmp.a, tmp.b);
return ;
}
Vec nor = crossDet(a.vec(), b.vec());
Rat ans = pt2Plane(pt[], pt[], nor);
// cout << "~~~" << endl;
if (ans == ) {
// cout << "same plane" << endl;
if (segIntersect(a, b)) { puts("0 1"); return ;}
Rat tmp = min(min(pt2Seg(pt[], b), pt2Seg(pt[], b)), min(pt2Seg(pt[], a), pt2Seg(pt[], a)));
tmp.fix();
printf("%lld %lld\n", tmp.a, tmp.b);
return ;
} else {
// cout << "diff plane" << endl;
pair<Rat, Rat> tmp = getIntersect(a, b);
// cout << tmp.first.val() << "= =" << tmp.second.val() << endl;
// cout << (tmp.first > 0) << endl;
if (tmp.first > && tmp.first < && tmp.second > && tmp.second < ) {
// cout << "cross" << endl;
ans.fix();
printf("%lld %lld\n", ans.a, ans.b);
} else {
// cout << "not cross" << endl;
Rat t = min(min(pt2Seg(pt[], b), pt2Seg(pt[], b)), min(pt2Seg(pt[], a), pt2Seg(pt[], a)));
t.fix();
printf("%lld %lld\n", t.a, t.b);
}
}
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
Point pt[];
int T;
cin >> T;
while (T--) {
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
scanf("%lld", &pt[i].x[j]);
}
}
work(pt);
}
return ;
} /*
13 -20 -20 -20 20 20 19
0 0 0 1 1 1 -20 -20 -20 20 19 20
-20 -20 20 20 20 -20 0 0 0 20 20 20
0 0 10 0 20 10 0 0 0 1 1 1
2 3 4 1 2 2 0 0 0 0 0 0
0 1 1 1 2 3 0 0 0 10 10 10
11 12 13 10 11 11 0 0 0 1 1 1
1 1 1 2 2 2 1 0 0 0 1 0
1 1 0 2 2 0 1 0 0 0 1 0
0 0 0 1 1 0 0 0 0 0 0 20
20 0 10 0 20 10 0 0 0 20 20 20
1 1 2 1 1 2 0 0 0 20 20 20
0 20 20 0 20 20 0 0 0 0 0 20
20 20 0 20 20 20
*/
——written by Lyon
LA 4973 Ardenia (3D Geometry + Simulation)的更多相关文章
- uvalive 4973 Ardenia
题意:给出空间两条线段,求距离. 注意输出格式! #include<cstdio> #include<cmath> #include<algorithm> usin ...
- LA 4973异面线段
题目大意:给两条线段求他们间的最小距离的平方(以分数形式输出). 贴个模版吧!太抽象了. #include<cstdio> #include<cmath> #include&l ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- Pushing state-of-the-art in 3D content understanding
Pushing state-of-the-art in 3D content understanding 2019-10-31 06:34:08 This blog is copied from: h ...
- LA 4064 Magnetic Train Tracks
题意:给定平面上$n(3\leq n \leq 1200)$个无三点共线的点,问这些点组成了多少个锐角三角形. 分析:显然任意三点可构成三角形,而锐角三角形不如直角或钝角三角形容易计数,因为后者有且仅 ...
- LA 4998 Simple Encryption
题意:输入正整数$K_1(K_1 \leq 50000)$, 找一个$12$位正整数$K_2$(不能含有前导零)使得${K_1}^{K_2}\equiv K_2(mod10^{12})$. 例如,$K ...
- LA 3704 Cellular Automaton
题意概述: 等价地,本题可以转化为下面的问题: 考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$.求$ ...
- 计算机视觉code与软件
Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...
- [OSG]OSG的相关扩展
参考:osg官网 http://www.osgchina.org/index.php?view=article&id=176 http://trac.openscenegraph.org/pr ...
随机推荐
- tinkcmf视频上传大小限制
/application/Common/Common/function.php 找到upload_max_filesize把后面的数值改成合适的大小(单位是KB)
- curl应用大法
curl -s -S -k --max-redirs 5 -m 60 --connect-timeout 60 -w "\n\n%{time_namelookup}|%{time_conn ...
- webpack学习之—— 依赖图(Dependency Graph) 及 构建目标(Targets)
Dependency Graph 任何时候,一个文件依赖于另一个文件,webpack 就把此视为文件之间有依赖关系.这使得 webpack 可以接收非代码资源(non-code asset)(例如图像 ...
- selenium(1):python3.6.4+selenium3.0+chrome环境配置
本文为配置过程: python 1.python3.6.4下载安装见python安装说明.(本博客) 2.安装python的集成编译器PyCharm. PyCharm 是由 JetBrains 打造 ...
- LintCode 合并二维数组
合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 之前想的是用剑指offer里替换空格那种方法 但是把 ...
- 论C#的多继承
意外中看到这篇博客,很有意思! 本文转自:http://www.cnblogs.com/leotsai/p/csharp-multi-inheritance.html C#多继承的讨论似乎是个古老的问 ...
- D2D画箭头的例子
原文:D2D画箭头的例子 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sunnyloves/article/details/50830102 用处 ...
- Ajax--Ajax基于原生javascript:创建Ajax对象、链接服务器、发送请求、接受响应结果
Ajax概述 异步:指某段程序执行时不会阻塞其它程序执行,其表现形式为程序的执行顺序不依赖程序本身的书写顺序,相反则为同步. 同步请求: 请求是由浏览器发送 页面会刷新 异步请求: 请求是由浏览器的一 ...
- Directx11教程(10) 画一个简易坐标轴
原文:Directx11教程(10) 画一个简易坐标轴 本篇教程中,我们将在三维场景中,画一个简易的坐标轴,分别用红.绿.蓝三种颜色表示x,y,z轴的正向坐标轴. 为此,我们要先建立一个A ...
- 2019-10-26-Inno-Setup-安装包脚本-Run-的-Flags-标记
title author date CreateTime categories Inno Setup 安装包脚本 Run 的 Flags 标记 lindexi 2019-10-26 08:42:39 ...