AtCoder题解 —— AtCoder Beginner Contest 188 —— B - Orthogonality —— 向量内积

题目相关

题目链接

AtCoder Beginner Contest 188 B 题,https://atcoder.jp/contests/abc188/tasks/abc188_b

Problem Statement

Given are two N N N-dimensional vectors A = ( A 1 , A 2 , A 3 , … , A N ) A=(A_1,A_2,A_3,…,A_N) A=(A1,A2,A3,,AN) and B = ( B 1 , B 2 , B 3 , … , B N ) B=(B_1,B_2,B_3,…,B_N) B=(B1,B2,B3,,BN).
Determine whether the inner product of A A A and B B B is 0.
In other words, determine whether A 1 B 1 + A 2 B 2 + A 3 B 3 + ⋯ + A N B N = 0 A_1B_1+A_2B_2+A_3B_3+⋯+A_NB_N=0 A1B1+A2B2+A3B3++ANBN=0.

Input

Input is given from Standard Input in the following format:

N
A1 A2 ... AN
B1 B2 ... BN

Output

If the inner product of A A A and B B B is 0, print Yes; otherwise, print No.

Sample 1

Sample Input 1

2
-3 6
4 2

Sample Output 1

Yes

Explaination

The inner product of A A A and B B B is ( − 3 ) × 4 + 6 × 2 = 0 (−3)×4+6×2=0 (3)×4+6×2=0.

Sample 2

Sample Input 2

2
4 5
-1 -3

Sample Output 2

No

Explaination

The inner product of A A A and B B B is 4 × ( − 1 ) + 5 × ( − 3 ) = − 19 4×(−1)+5×(−3)=−19 4×(1)+5×(3)=19.

Sample 3

Sample Input 3

3
1 3 5
3 -6 3

Sample Output 3

Yes

Explaination

The inner product of A A A and B B B is 1 × 3 + 3 × ( − 6 ) + 5 × 3 = 0 1×3+3×(−6)+5×3=0 1×3+3×(6)+5×3=0.

Constraints

  • 1 ≤ N ≤ 100000 1≤N≤100000 1N100000
  • − 100 ≤ A i ≤ 100 −100≤A_i≤100 100Ai100
  • − 100 ≤ B i ≤ 100 −100≤B_i≤100 100Bi100
  • All values in input are integers.

题解报告

题目翻译

给两个 N N N-维向量 A = ( A 1 , A 2 , A 3 , … , A N ) A=(A_1,A_2,A_3,…,A_N) A=(A1,A2,A3,,AN) B = ( B 1 , B 2 , B 3 , … , B N ) B=(B_1,B_2,B_3,…,B_N) B=(B1,B2,B3,,BN),判断向量 A A A B B B 是否正交。也就是判断 A ⋅ B ≡ 0 A \cdot B \equiv 0 AB0

题目分析

一个简单的数学题,从知识点的角度来说,属于线性代数部分。给定了向量 A A A B B B,判断是否正交,我们只需要就 A A A B B B 的内积即可。
根据数学定义可知, A A A B B B 的内积为
A 1 B 1 + A 2 B 2 + A 3 B 3 + ⋯ + A N B N A_1B_1+A_2B_2+A_3B_3+⋯+A_NB_N A1B1+A2B2+A3B3++ANBN
因此本题没有任何技巧性,就是计算上面的这个累加和即可。
根据本题提供 N N N 大小为 1e5,计算累加和的时间复杂度为 O ( N ) O(N) O(N),可以满足 1 1 1 秒的需求。

数据范围分析

N N N 大小为 1e5,因此用 int 可以表示。下面我们估算一下内积的最大值和最小值。
A n s m a x = 100 ∗ 100 ∗ 1 0 5 = 1 0 9 A n s m i n = − 100 ∗ 100 ∗ 1 0 5 = − 1 0 9 Ans_{max}=100*100*10^5=10^9\\Ans_{min}=-100*100*10^5=-10^9 Ansmax=100100105=109Ansmin=100100105=109
因此,用 int 是可以满足的。

AC 代码

//https://atcoder.jp/contests/abc188/tasks/abc188_a
//A - Three-Point Shot
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
#define __LOCAL

const int MAXN =1e5+4;
int a[MAXN];
int b[MAXN];

int main() {
    
    
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    int n;
    cin>>n;
    for (int i=1; i<=n; i++) {
    
    
        cin>>a[i];
    }
    for (int i=1; i<=n; i++) {
    
    
        cin>>b[i];
    }

    int ans=0;
    for (int i=1; i<=n; i++) {
    
    
        ans += a[i]*b[i];
    }
    if (0==ans) {
    
    
        cout<<"Yes\n";
    } else {
    
    
        cout<<"No\n";
    }

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

在这里插入图片描述

扫描二维码关注公众号,回复: 12359428 查看本文章

时间复杂度

O(N)。

空间复杂度

O(N)。

猜你喜欢

转载自blog.csdn.net/justidle/article/details/112531017
今日推荐