浅谈OI中的向量/数学题

向量,数学界的一颗东方明珠,今天刚好有复习到了,故总结一番

一些概念

维度

一组相互独立的属性的数量

  • 任意属性的取值与其他属性无关。

  • 人类生活的空间有三个维度。

维度与图形的载体

  • 点:最简单的图形,可以看作零维。
  • 线:一维载体。
  • 面:二维载体。

坐标系

直角坐标系

常用

将所有维度的值均为0的点称为原点,用O表示。

  • 将过原点的水平直线称为x轴,左小右大;
    将过原点的垂直直线称为y轴,下小上大。
  • 点的坐标(x, y)就是原点在每个方向上的偏移量。
  • 每个维度的表示是等价的,置换维度不影响形状。

极坐标系

用旋转角与距离表示维度

  • 所有维度的值均为0的点被称为极点,用O表示。
  • 将极点向右发出的射线称为极轴,作为旋转角的起点。
  • 点的坐标(r, θ)由极径r与极角θ表示。
  • 极角是极轴逆时针旋转到穿过点的角度。
  • 极径是点到原点的距离。

向量

在数学中,向量,指具有大小(magnitude)和方向的量。它可以形象化地表示为带箭头的线段。箭头所指:代表向量的方向;线段长度:代表向量的大小。

向量的记法

可以用A点到B点的有向线段$\overrightarrow{AB} $表示向量,长度就是向量的大小。

特殊向量

  • 长度为0的向量被称为零向量,是唯一方向不确定的向量。
  • 长度为1的向量被称为单位向量,
    与向量a同方向的单位向量称为a的单位向量,记作a0。
  • 反向量:大小相同且方向相反的向量
    令\(\overrightarrow{AB} = (x, y)\),则反向量\(\overrightarrow{CD} = (-x, -y)\)。
  • 向量与其反向量相加得到零向量

向量性质

  • 向量没有位置属性
  • 有向线段被平移后表示的是同条向量。
  • 可以将有向线段的起点平移到原点,
    此时用终点就可以表示向量。
  • 此时终点的坐标由有向线段两个点的坐标差组成。

向量运算

向量加法

  • 对每个维度分别相加。
    令\(\overrightarrow{AB} = (x_1, y_1),\overrightarrow{CD} = (x_2, y_2)\),
    则\(\overrightarrow{AB} + \overrightarrow{CD} = (x_1 + x_2, y_1 + y_2)\)。

  • 相当于将两个向量首尾相接。

  • 向量加法满足交换律,即\(\overrightarrow{AB} + \overrightarrow{CD} = \overrightarrow{CD} + \overrightarrow{AB}\)。

向量减法

  • 对每个维度分别相减。
    令\(\overrightarrow{AB} = (x_1, y_1),\overrightarrow{CD} = (x_2, y_2)\),
    则\(\overrightarrow{AB} - \overrightarrow{CD} = (x_1 - x_2, y_1 - y_2)\)。

  • 相当于被减向量加上减向量的反向量。
    向量加减法的规则被称为平行四边形法则

向量点乘

点乘符号为 $\cdot $, \(\overrightarrow{𝑂𝑃} \cdot \overrightarrow{𝑂𝑄} = |\overrightarrow{𝑂𝑃} | | \overrightarrow{𝑂𝑄}  | cos 𝜃\),其中𝜃为\(\overrightarrow{𝑂𝑃}\)和\(\overrightarrow{𝑂𝑄}\)的夹角。

即\(\overrightarrow{𝑂𝑄}\)到\(\overrightarrow{𝑂𝑃}\)的投影长度与\(\overrightarrow{𝑂𝑃}\)的长度相乘。

点乘的结果被称为点积,又被称为内积,是标量。
令\(\overrightarrow{𝑂𝑃}  = (x1, y1), \overrightarrow{𝑂𝑄} = (x2, y2),\)则\(\overrightarrow{𝑂𝑃} • \overrightarrow{𝑂𝑄} = x_1 \times x_2 + y_1 \times y_2\)。

点乘可以判断向量的前后关系

  • 根据参照向量\(\overrightarrow{𝑂𝑃}\)建立直角坐标系,O为原点,\(\overrightarrow{𝑂𝑃}\)方向为x正轴。

  • 当Q在一、四象限或x正轴上时,有\(\overrightarrow{𝑂𝑃} • \overrightarrow{𝑂𝑄} > 0\)。

  • 当Q在二、三象限或x负轴上时,有\(\overrightarrow{𝑂𝑃} • \overrightarrow{𝑂𝑄} < 0\)。

  • 当Q在y轴上时,有\(\overrightarrow{𝑂𝑃} • \overrightarrow{𝑂𝑄} = 0\)。

向量叉乘

点乘符号为 $\times $, \(\overrightarrow{𝑂𝑃} \cdot \overrightarrow{𝑂𝑄} = |\overrightarrow{𝑂𝑃} | | \overrightarrow{𝑂𝑄}  | sin 𝜃\),其中𝜃为\(\overrightarrow{𝑂𝑃}\)和\(\overrightarrow{𝑂𝑄}\)的夹角。

即\(\overrightarrow{𝑂𝑄}\)到\(\overrightarrow{𝑂𝑃}\)的距离与\(\overrightarrow{𝑂𝑃}\)的长度相乘。

叉乘的结果被称为叉积,又被称为外积,是向量。

令\(\overrightarrow{𝑂𝑃} = (x1, y1), \overrightarrow{𝑂𝑄} = (x2, y2),\)则\(|\overrightarrow{𝑂𝑃} \times \overrightarrow{𝑂𝑄}| = x1 \times y2 - y1 \times x2\)。

叉乘不满足交换律,交换后结果方向相反。

\(\overrightarrow{𝑂𝑃} \times \overrightarrow{𝑂𝑄} = -(\overrightarrow{𝑂𝑄} \times \overrightarrow{𝑂𝑃}) = \overrightarrow{𝑂𝑄} \times -\overrightarrow{𝑂𝑃}\)

交换位置或者取反某个向量都会导致叉乘结果反向。

叉乘可以判断向量的左右关系

  • 根据参照向量\(\overrightarrow{𝑂𝑃}\)建立直角坐标系,O为原点,\(\overrightarrow{𝑂𝑃}\)方向为x正轴。

  • 当Q在一、二象限或y正轴上时,有\(\overrightarrow{𝑂𝑃} \times \overrightarrow{𝑂𝑄} > 0\)。

  • 当Q在三、四象限或y负轴上时,有\(\overrightarrow{𝑂𝑃} \times \overrightarrow{𝑂𝑄} < 0\)。

  • 当Q在x轴上时,有\(\overrightarrow{𝑂𝑃} \times \overrightarrow{𝑂𝑄} = 0\)。

叉积的几何意义

  • 叉积的长度等于两条向量构成的平行四边形的面积。
  • 等于两条向量构成的三角形的面积的两倍。

向量点乘与叉乘的特点

  • 既可以根据几何意义进行运算,也可以直接对向量进行运算。
  • 如果可以用整数坐标表示向量,则运算结果也一定是整数。
  • 对于根据参照向量建立的直角坐标系,结合点乘与叉乘,
    可以判断出任意向量在该坐标系中的方位(象限、轴、原点)。

例题

独眼兔

对于任意一段路径,如果右边还有剩下的点,
由于不能右拐,所以只能左拐绕圈出去。

起点在所有点的左下方,不可能通过左拐绕过,
所以走的每一段路径都要保证剩下点全在左边。

从起点开始,每次走向最靠右的点就能满足要求。

/*

writer:  Luna_Frome_now_pointarry
Problem: 不知名屑题
Knowledge: 垃圾算法
anse: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:

*/
#include<bits/stdc++.h>
#define ll long long
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi firnow_point
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 1005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
using point = struct ___{int x, y;};
point a[N];
int vis[N], n;
int get(point x, point y){
	return x.x * y.y - y.x * x.y;
}
point get_dis(int x, int y){
	return {a[x].x - a[y].x, a[x].y - a[y].y};
}
void work(){
    //please write your code here
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i].x >> a[i].y;
	}
	int now_point, minn = INF;
	for(int i = 1; i <= n; i++){
		if(a[i].y < minn){
			minn = a[i].y;
			now_point = i;
		}
	}
	cout << n << " " << now_point << " ";
	vis[now_point] = 1;
	for(int ans = 1; ans < n; ans++){
		int now = -1;
		for(int i = 1; i <= n; i++){
			if(vis[i]) continue;
			if(now == -1) now = i;
			point dis_i = get_dis(i, now_point);
			point dis_now = get_dis(now, now_point);
			int c = get(dis_i, dis_now);
			if(c > 0){
				now = i;
			}
			if(c == 0 && abs(dis_i.x) <= abs(dis_now.x) && abs(dis_i.y) <= abs(dis_now.y)){
				now = i;
			}
		}
		vis[now] = 1;
		cout << now << " ";
		now_point = now;
	}
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t = 1; while(t--){work();}
	return 0;
}

独眼兔-时空复杂度

时间复杂度为\(\mathcal{O(n^2)}\)

空间复杂度为\(\mathcal{O(n)}\)

“向量与数学” 相关的OI题目


1. POJ 1696 Space Ant(极角排序 + 叉积)

题意:一只蚂蚁只能向左转,不能穿过以前走过的路径。给定 (n) 个点,求经过所有点的顺序。

思路:
非常经典的极角排序模板题。由于蚂蚁只能向左转,意味着每次选择的下一个点必须保证剩余所有点都在当前方向向量的左侧(包括共线)。
我们可以每次在当前点,遍历所有未访问的点,利用叉积判断哪个点是最靠右的(即所有其他点都在它的左边)。叉积 (a \times b > 0) 表示 (b) 在 (a) 的逆时针方向。

/*

writer:  Luna_Frome_Starry
Problem: Space Ant
Knowledge: 极角排序 + 叉积
Time: O(n^2)

*/
/*
Note:
利用叉积找最靠右的点,保证剩余点全在左侧。
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 60;
const int INF = 1e9;

struct Point{
    int x, y, id;
} p[N];
bool vis[N];
int n;

int cross(Point a, Point b, Point c){
    // 向量 ab 和 ac 的叉积
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

void work(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> p[i].id >> p[i].x >> p[i].y;
        vis[i] = false;
    }
    
    // 找起点:y坐标最小的点
    int st = 1;
    for(int i = 2; i <= n; i++){
        if(p[i].y < p[st].y || (p[i].y == p[st].y && p[i].x < p[st].x)){
            st = i;
        }
    }
    
    vector<int> ans;
    ans.push_back(st);
    vis[st] = true;
    
    int now = st;
    for(int step = 1; step < n; step++){
        int nxt = -1;
        for(int i = 1; i <= n; i++){
            if(vis[i]) continue;
            if(nxt == -1) nxt = i;
            else{
                int val = cross(p[now], p[nxt], p[i]);
                // 如果 i 在 nxt 的右侧(或共线且更近),则更新 nxt
                if(val < 0 || (val == 0 && (abs(p[i].x - p[now].x) + abs(p[i].y - p[now].y) < abs(p[nxt].x - p[now].x) + abs(p[nxt].y - p[now].y)))){
                    nxt = i;
                }
            }
        }
        ans.push_back(nxt);
        vis[nxt] = true;
        now = nxt;
    }
    
    cout << ans.size();
    for(int i = 0; i < (int)ans.size(); i++){
        cout << " " << p[ans[i]].id;
    }
    cout << "\n";
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        work();
    }
    return 0;
}

2. [OJ#39] 左手右手(凸包 + 旋转卡壳)

题意:有 (n) 个人,每个人左右手上各写着一个整数 ((a_i, b_i))。定义两人 (i, j) 的好感度差异为 (|a_i b_j - a_j b_i|)。求最大好感度差异。

思路:
两人的好感度差异其实就是两个二维向量 ((a_i, b_i)) 和 ((a_j, b_j)) 的叉积的绝对值。我们要最大化这个叉积,等价于找两个向量使得它们围成的平行四边形面积最大。
由凸包性质可知,使叉积最大的向量一定在凸包上。我们先对点集求凸包,然后在凸包上使用旋转卡壳(双指针法),由于凸包的极角单调性,可以 (O(n)) 求出最大叉积。

/*

writer:  Luna_Frome_Starry
Problem: 左手右手
Knowledge: 凸包 + 旋转卡壳
Time: O(n log n)

*/
/*
Note:
求凸包后,旋转卡壳找最大叉积。
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
const int INF = 1e9;

struct Vec{
    int x, y;
    Vec operator - (const Vec &t) const{
        return {x - t.x, y - t.y};
    }
    bool operator < (const Vec &t) const{
        return x != t.x ? x < t.x : y < t.y;
    }
} p[N], hull[N];

int cross(Vec a, Vec b){
    return a.x * b.y - a.y * b.x;
}

int n, cnt;

void work(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> p[i].x >> p[i].y;
    }
    sort(p + 1, p + n + 1);
    
    // 求凸包
    for(int i = 1; i <= n; i++){
        while(cnt > 1 && cross(hull[cnt] - hull[cnt - 1], p[i] - hull[cnt]) >= 0) cnt--;
        hull[++cnt] = p[i];
    }
    int up = cnt;
    for(int i = n - 1; i >= 1; i--){
        while(cnt > up && cross(hull[cnt] - hull[cnt - 1], p[i] - hull[cnt]) >= 0) cnt--;
        hull[++cnt] = p[i];
    }
    cnt--; // 去掉重复的起点
    
    // 旋转卡壳
    int ans = 0;
    int j = 2;
    for(int i = 1; i <= cnt; i++){
        int nxt = i % cnt + 1;
        while(abs(cross(hull[nxt] - hull[i], hull[j] - hull[i])) <= abs(cross(hull[nxt] - hull[i], hull[j % cnt + 1] - hull[i]))){
            j = j % cnt + 1;
        }
        ans = max(ans, abs(cross(hull[nxt] - hull[i], hull[j] - hull[i])));
    }
    
    cout << ans << "\n";
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--){
        work();
    }
    return 0;
}

3. P4557 [JSOI2018] 战争(闵可夫斯基和 + 凸包)

题意:给出两个点集 (A, B),多次询问将 (B) 平移某个向量后,是否与 (A) 有交。

思路:
将 (A, B) 分别求凸包。问题等价于判断平移向量是否在 (A) 与 (-B) 的闵可夫斯基和凸包内。
闵可夫斯基和的凸包有一个优美的性质:其边是由 (A) 和 (-B) 的凸包边进行极角排序后归并得到的。因此我们可以在 (O(n+m)) 时间内构造出闵可夫斯基和,然后用二分法判断点是否在凸包内。

/*

writer:  Luna_Frome_Starry
Problem: 战争
Knowledge: 闵可夫斯基和 + 凸包
Time: O((n+m+q) log n)

*/
/*
Note:
先求 A 和 -B 的凸包,然后归并边求闵可夫斯基和,最后二分判断点是否在内。
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 300005;
const int INF = 1e18;

struct Point{
    int x, y;
    bool operator < (const Point &t) const{
        return x != t.x ? x < t.x : y < t.y;
    }
    Point operator - (const Point &t) const{
        return {x - t.x, y - t.y};
    }
    Point operator + (const Point &t) const{
        return {x + t.x, y + t.y};
    }
} a[N], b[N], c[N], s[N];

int cross(Point u, Point v){
    return u.x * v.y - u.y * v.x;
}

int n, m, q, cnt;

void get_hull(Point *p, int &n){
    sort(p + 1, p + n + 1);
    int k = 0;
    for(int i = 1; i <= n; i++){
        while(k > 1 && cross(s[k] - s[k - 1], p[i] - s[k]) >= 0) k--;
        s[++k] = p[i];
    }
    int t = k;
    for(int i = n - 1; i >= 1; i--){
        while(k > t && cross(s[k] - s[k - 1], p[i] - s[k]) >= 0) k--;
        s[++k] = p[i];
    }
    n = k - 1;
    for(int i = 1; i <= n; i++) p[i] = s[i];
}

bool check(Point x){
    if(cross(x - c[1], c[2] - c[1]) > 0) return false;
    if(cross(x - c[1], c[cnt] - c[1]) < 0) return false;
    int l = 2, r = cnt - 1, pos = -1;
    while(l <= r){
        int mid = (l + r) >> 1;
        if(cross(x - c[1], c[mid] - c[1]) <= 0){
            pos = mid;
            l = mid + 1;
        }else{
            r = mid - 1;
        }
    }
    if(pos == -1) return false;
    return cross(x - c[pos], c[pos % cnt + 1] - c[pos]) >= 0;
}

void work(){
    cin >> n >> m >> q;
    for(int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;
    for(int i = 1; i <= m; i++) cin >> b[i].x >> b[i].y;
    
    get_hull(a, n);
    for(int i = 1; i <= m; i++) b[i].x = -b[i].x, b[i].y = -b[i].y;
    get_hull(b, m);
    
    // 归并求闵可夫斯基和
    cnt = 1;
    c[1] = a[1] + b[1];
    int i = 2, j = 2;
    while(i <= n || j <= m){
        Point va = (i > n) ? (a[1] - a[n]) : (a[i] - a[i - 1]);
        Point vb = (j > m) ? (b[1] - b[m]) : (b[j] - b[j - 1]);
        if(cross(va, vb) >= 0){
            c[cnt + 1] = c[cnt] + va;
            cnt++;
            i++;
        }else{
            c[cnt + 1] = c[cnt] + vb;
            cnt++;
            j++;
        }
    }
    cnt--; // 去掉重复点
    
    while(q--){
        Point x;
        cin >> x.x >> x.y;
        if(check(x)) cout << "1\n";
        else cout << "0\n";
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--){
        work();
    }
    return 0;
}

4. P6160 [Cnoi2020] 向量(内积最值)

题意:给定三个向量 (\vec{a}, \vec{b}, \vec{c}) 的模长 (r_1, r_2, r_3),求 (\sigma = \min(\vec{a} \cdot \vec{b} + \vec{b} \cdot \vec{c} + \vec{c} \cdot \vec{a}))。

思路:
利用内积公式展开:(\sigma = r_1 r_2 \cos\theta_{ab} + r_2 r_3 \cos\theta_{bc} + r_3 r_1 \cos\theta_{ca})。
由于三个向量共面时内积取到最小值,且可以通过调整夹角使其全部为钝角。当夹角相互为 (120^\circ) 时取得最小值 (-\frac{1}{2}(r_1^2 + r_2^2 + r_3^2))。但需要注意三角不等式:如果 (r_1 + r_2 < r_3),则无法构成封闭三角形,最小值为 (r_1 r_2 - r_2 r_3 - r_3 r_1)。

/*

writer:  Luna_Frome_Starry
Problem: 向量
Knowledge: 向量内积 + 数学分类讨论
Time: O(1)

*/
/*
Note:
1. 若能构成三角形,最小值为 -1/2 * (r1^2 + r2^2 + r3^2)
2. 若不能构成三角形,说明有一个向量极长,最小值为 r1*r2 - r2*r3 - r3*r1
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;

void work(){
    int r1, r2, r3;
    cin >> r1 >> r2 >> r3;
    
    if(r1 + r2 <= r3){
        cout << (double)(r1 * r2 - r2 * r3 - r3 * r1) << "\n";
    }else{
        cout << fixed << setprecision(1) << (double)(-1.0 / 2.0 * (r1 * r1 + r2 * r2 + r3 * r3)) << "\n";
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--){
        work();
    }
    return 0;
}

5. P2520 [HAOI2011] 向量(裴蜀定理 + 向量线性组合)

题意:给定向量 ((a,b)) 和 ((x,y)),问能否用 ((a,b), (b,a), (a,-b), (b,-a)) 这些向量,通过整数次线性组合,得到向量 ((x,y))。

思路:
设四种向量的系数分别为 (c_1, c_2, c_3, c_4),将方程按 (x,y) 分量拆开,利用裴蜀定理。实际上通过加减消元,可以得到 (c_1 + c_3) 和 (c_2 + c_4) 必须满足由 (\gcd(a,b)) 整除的条件,同时还要满足整数解的同奇偶性。最后分类讨论四个等式是否有任意一个成立即可。

/*

writer:  Luna_Frome_Starry
Problem: 向量
Knowledge: 裴蜀定理
Time: O(log(min(a,b)))

*/
/*
Note:
利用 gcd 判断整除关系,同时判断奇偶性。
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;

int gcd(int a, int b){
    return b == 0 ? a : gcd(b, a % b);
}

bool check(int x, int y, int d){
    return (x % d == 0) && (y % d == 0);
}

void work(){
    int a, b, x, y;
    cin >> a >> b >> x >> y;
    int d = 2 * gcd(a, b);
    
    if(check(x + a + b, y + a + b, d) || check(x + a, y + b, d) || check(x + b, y + a, d) || check(x, y, d)){
        cout << "Y\n";
    }else{
        cout << "N\n";
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        work();
    }
    return 0;
}

6. P3812 【模板】线性基(线性基)

题意:给定 (n) 个整数,求它们能异或出的最大结果。

思路:
线性基是解决“子集异或和”问题的大杀器。它的本质是对向量空间基底的一种维护。我们将每个数看作二进制向量,通过类似高斯消元的方法插入线性基。插入完成后,从高位到低位贪心地尝试异或,如果异或后结果变大就异或,就能得到最大异或和。

/*

writer:  Luna_Frome_Starry
Problem: 线性基
Knowledge: 线性基
Time: O(n log V)

*/
/*
Note:
构造线性基,然后从高位到低位贪心异或。
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 55;

int p[N];

void insert(int x){
    for(int i = 50; i >= 0; i--){
        if(!(x >> i & 1)) continue;
        if(!p[i]){
            p[i] = x;
            break;
        }
        x ^= p[i];
    }
}

void work(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        int x;
        cin >> x;
        insert(x);
    }
    
    int ans = 0;
    for(int i = 50; i >= 0; i--){
        if((ans ^ p[i]) > ans){
            ans ^= p[i];
        }
    }
    cout << ans << "\n";
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--){
        work();
    }
    return 0;
}
posted @ 2026-10-24 21:40  Luna_From_Starry  阅读(33)  评论(0)    收藏  举报