ACM-ICPC Live Archive

  三维几何,题意是要求求出两条空间线段的距离。题目难度在于要求用有理数的形式输出,这就要求写一个有理数类了。

  开始的时候写出来的有理数类就各种疯狂乱套,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)的更多相关文章

  1. uvalive 4973 Ardenia

    题意:给出空间两条线段,求距离. 注意输出格式! #include<cstdio> #include<cmath> #include<algorithm> usin ...

  2. LA 4973异面线段

    题目大意:给两条线段求他们间的最小距离的平方(以分数形式输出). 贴个模版吧!太抽象了. #include<cstdio> #include<cmath> #include&l ...

  3. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  4. 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 ...

  5. LA 4064 Magnetic Train Tracks

    题意:给定平面上$n(3\leq n \leq 1200)$个无三点共线的点,问这些点组成了多少个锐角三角形. 分析:显然任意三点可构成三角形,而锐角三角形不如直角或钝角三角形容易计数,因为后者有且仅 ...

  6. LA 4998 Simple Encryption

    题意:输入正整数$K_1(K_1 \leq 50000)$, 找一个$12$位正整数$K_2$(不能含有前导零)使得${K_1}^{K_2}\equiv K_2(mod10^{12})$. 例如,$K ...

  7. LA 3704 Cellular Automaton

    题意概述: 等价地,本题可以转化为下面的问题: 考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$.求$ ...

  8. 计算机视觉code与软件

    Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...

  9. [OSG]OSG的相关扩展

    参考:osg官网 http://www.osgchina.org/index.php?view=article&id=176 http://trac.openscenegraph.org/pr ...

随机推荐

  1. WIN7快捷键大全

    1. 轻松访问键盘快捷方式 按住右Sht 8秒钟: 启用和关闭筛选键 按左 Alt+左 Sht+PrtScn(或 PrtScn):启用或关闭高对比度 按左 Alt+左 Sht+Num Lock :启用 ...

  2. Hibernate_条件查询客户列表

    分析:通过名称查询 实现: 1.在list.jsp中修改 2.修改ListCustomerServlet 首先获取cust_name,增加条件:若不为空,则模糊搜索,再调用Service方法,结果放到 ...

  3. 极简bootstrap file 美化样式(无需第三方插件)

    原本的file上传表单非常的丑,但是又不想使用第三方插件,Bootstrap也没有相关的美化,于是用纯CSS完成,美化,JS实现功能,连BootStrap都不需要,十分简单 1.给原版丑表单隐藏了di ...

  4. 阿里云Global Connection亮相MWC 2019,做企业全球化开路先锋

    上周在巴塞罗那举行的MWC 2019世界移动通信大会上,阿里云发布了包含Global Connection解决方案在内的7款重量级产品和解决方案,为全球企业提供了基于阿里云的智能化企业数字转型思路.G ...

  5. oracle -视图 序列 约束

    1.视图 视图是基于一个或者多个表数据库对象,视图允许用户创建一个无数据的”伪表“,视图只是一个获取特定列好行的sql查询组成,通过视图检索数据就像从表中检索数据 一样. 视图可以提供一个附加的安全层 ...

  6. vue使用填坑之:model和v-model的区别

    v-model通常用于input的双向数据绑定 <input v-model="parentMsg">,也可以实现子组件到父组件数据的双向数据绑定:首先说说v-mode ...

  7. WPF学习(8)数据绑定 https://www.cnblogs.com/jellochen/p/3541197.html

    说到数据绑定,其实这并不是一个新的玩意儿.了解asp.net的朋友都知道,在asp.net中已经用到了这个概念,例如Repeater等的数据绑定.那么,在WPF中的数据绑定相比较传统的asp.net中 ...

  8. 【python爬虫】加密代理IP的使用与设置一套session请求头

    1:代理ip请求,存于redis: # 请求ip代理连接,更新redis的代理ip def proxy_redis(): sr = redis.Redis(connection_pool=Pool) ...

  9. bnd.bnd属性文件格式

    1.Header以大写字母开头 Bundle-Name: StoreAdminProductsTool 2.Instruction以-和小写字母开头 -sources: true 3. Macro形式 ...

  10. Unicode, UTF-8, GBK, ASCII的区别

    看完知乎的两篇文章大概就明白了 https://zhuanlan.zhihu.com/p/25435644 https://www.zhihu.com/question/23374078 看完总结一下 ...