\(\text{Problem}\)

给出向量 \(\boldsymbol a = (x1,y1), \boldsymbol b = (x2,y2)\)

求 \(|\lambda_1\boldsymbol a + \lambda_2\boldsymbol b|\) 的最小值

其中 \(\lambda_1,\lambda_2\) 为整数

\(\text{Solution}\)

二维欧几里得算法的应用

然而并不懂是什么东西,我们只考虑这道题

做法见金斌的集训队论文《欧几里得算法的应用》

Here

注意共线的向量答案为 \(0\) 要特判

\(\text{Code}\)

#include <cstdio>
#include <cmath>
#include <iostream>
#define IN inline
using namespace std; struct Vector{double x, y;}a, b;
IN double operator * (const Vector &a, const Vector &b){return a.x * b.x + a.y * b.y;}
IN Vector operator * (const Vector &a, double k){return Vector{a.x * k, a.y * k};}
IN Vector operator + (const Vector &a, const Vector &b){return Vector{a.x + b.x, a.y + b.y};}
IN Vector operator - (const Vector &a, const Vector &b){return Vector{a.x - b.x, a.y - b.y};}
IN double cos(Vector a, Vector b){return a * b / (sqrt(a * a) * sqrt(b * b));} int main()
{
freopen("math.in", "r", stdin), freopen("math.out", "w", stdout);
while (~scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y))
{
if (a * a > b * b) swap(a, b);
if (a * b < 0) b = b * -1;
double cos_alpha = cos(a, b);
if (cos_alpha >= 1){printf("0\n"); continue;}
while (cos_alpha > 0.5)
{
Vector c = b * cos_alpha;
if (c * c < a * a) b = b - a;
else b = b - a * (int)(sqrt(c * c) / sqrt(a * a));
if (a * a > b * b) swap(a, b);
if (a * b < 0) b = b * -1;
cos_alpha = cos(a, b);
}
printf("%.0lf\n", min(a * a, b * b));
}
}

最新文章

  1. svn diff 详解
  2. 小菜学习Winform(七)系统托盘
  3. 关于HTML页面布局要注意的问题
  4. Netty学习一:基本知识
  5. python中迭代器和生成器
  6. [转]A plain english introduction to cap theorem
  7. Android 高级编程 RecyclerView 控件的使用
  8. Cocos2D-x培训课程
  9. 图像显示 imshow()[OpenCV 笔记5]
  10. android代码混淆笔记
  11. java数组排序,并将数组内的数据求和
  12. 自动化服务部署(二):Linux下安装jenkins
  13. Mysql 学习笔记02
  14. socketserver 实现并发
  15. 局域网 FTP建立,搭建一个简易的局域网服务器
  16. cf946d 怎样逃最多的课dp
  17. spark Graph 的PregelAPI 理解和使用
  18. Redis 缓存设计原则
  19. R语言学习——欧拉计划(1)Multiples of 3 and 5
  20. Node.js Cookie管理

热门文章

  1. Java实现Excel批量导入数据库
  2. python 之定时任务(schedule)
  3. 为 ASPNETCORE 7 项目添加 Serilog
  4. [OpenCV实战]39 在OpenCV中使用ArUco标记的增强现实
  5. Java内存区域有哪些构成?
  6. visualstudio2017 community版本,有点失去信心了,同样两行代码,外观看不出任何区别,但是一个报错
  7. (15)go-micro微服务main.go开发
  8. Apache IoTDB C# SDK Apache-IoTDB-Client-CSharp
  9. 记OPNsense防火墙的安装过程 - 安全
  10. Vue.js 前端项目在常见 Web 服务器上的部署配置