JZOJ 3736. 【NOI2014模拟7.11】数学题
2024-09-08 18:17:01
\(\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));
}
}
最新文章
- svn diff 详解
- 小菜学习Winform(七)系统托盘
- 关于HTML页面布局要注意的问题
- Netty学习一:基本知识
- python中迭代器和生成器
- [转]A plain english introduction to cap theorem
- Android 高级编程 RecyclerView 控件的使用
- Cocos2D-x培训课程
- 图像显示 imshow()[OpenCV 笔记5]
- android代码混淆笔记
- java数组排序,并将数组内的数据求和
- 自动化服务部署(二):Linux下安装jenkins
- Mysql 学习笔记02
- socketserver 实现并发
- 局域网 FTP建立,搭建一个简易的局域网服务器
- cf946d 怎样逃最多的课dp
- spark Graph 的PregelAPI 理解和使用
- Redis 缓存设计原则
- R语言学习——欧拉计划(1)Multiples of 3 and 5
- Node.js Cookie管理
热门文章
- Java实现Excel批量导入数据库
- python 之定时任务(schedule)
- 为 ASPNETCORE 7 项目添加 Serilog
- [OpenCV实战]39 在OpenCV中使用ArUco标记的增强现实
- Java内存区域有哪些构成?
- visualstudio2017 community版本,有点失去信心了,同样两行代码,外观看不出任何区别,但是一个报错
- (15)go-micro微服务main.go开发
- Apache IoTDB C# SDK Apache-IoTDB-Client-CSharp
- 记OPNsense防火墙的安装过程 - 安全
- Vue.js 前端项目在常见 Web 服务器上的部署配置