Official
A - 花火の近さ / Closeness of Fireworks Editorial
by
A - 花火の近さ / Closeness of Fireworks Editorial
by
kyopro_friends
初心者の方へ
- AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
- また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
この問題は、 \(\sqrt{(X_i-P_i)^2+(Y_i-Q_i)^2}\leq R\) であるかどうかを判定する問題です。
この数式通りに計算すると、浮動小数点数の誤差の影響により誤判定が起こり不正解となります。両辺を \(2\) 乗して \((X_i-P_i)^2+(Y_i-Q_i)^2\leq R^2\) かどうかで判定することにすると、整数の範囲で計算を行うことができ、誤差の影響を受けません。
言語によってはオーバーフローに注意してください。
実装例 (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
long long r;
cin >> n >> r;
int ans = 0;
for(int i=0; i<n; i++){
long long x, y, p, q;
cin >> x >> y >> p >> q;
if((x-p)*(x-p) + (y-q)*(y-q) <= r*r){
ans++;
}
}
cout << ans << endl;
}
実装例 (Python)
N, R = map(int, input().split())
ans = 0
for _ in range(N):
X, Y, P, Q = map(int, input().split())
if (X-P)**2 + (Y-Q)**2 <= R**2:
ans += 1
print(ans)
posted:
last update:
