結果

問題 No.3008 ワンオペ警備員
ユーザー qwewe
提出日時 2025-05-14 13:14:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 5,654 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 52,096 KB
最終ジャッジ日時 2025-05-14 13:15:53
合計ジャッジ時間 3,325 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 6
other WA * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
import sys

# Explanation (説明):
# Problem: Find the minimum number of strokes to draw the net pattern found on a specific mayonnaise package, given an N x M block grid.
# Constraints:
# 1. The pattern is composed of N vertical x M horizontal blocks.
# 2. Strokes must be straight line segments that do not bend at intersections.
# 3. Find the minimum number of strokes required.

# 問題:某マヨネーズの袋に書かれている網目柄を、N x M ブロックのグリッドで描くときの最低筆数を求める。
# 条件:
# 1. 網目柄は N 行 M 列のブロックで構成される。
# 2. 描画は、交点で曲がらない直線的な線分のみを使用する(= 1 ストロークは 1 本の直線)。
# 3. 最低何回の筆運び(ストローク)で描けるかを求める。

# Analysis based on examples (例題からの分析):
# Example 1: N=2, M=2 -> Output: 2. Note that gcd(2, 2) = 2.
# Example 2: N=2, M=3 -> Output: 1. Note that gcd(2, 3) = 1.
# Example 3: N=4, M=4 -> Output: 4. Note that gcd(4, 4) = 4.
# These examples strongly suggest that the answer is the greatest common divisor (GCD) of N and M.

# 例題 1: N=2, M=2 -> 出力: 2。 gcd(2, 2) = 2。
# 例題 2: N=2, M=3 -> 出力: 1。 gcd(2, 3) = 1。
# 例題 3: N=4, M=4 -> 出力: 4。 gcd(4, 4) = 4。
# これらの例から、答えは N と M の最大公約数 (GCD) であると強く推測される。

# Justification for gcd(N, M) (gcd(N, M) が答えになる理由):
# The pattern is typically a cross-hatch, formed by two families of parallel diagonal lines.
# The condition "do not bend at intersections" means each stroke corresponds to a single, long, straight diagonal line segment traversing the grid.
# When considering the structure of these diagonal lines on the N x M grid, especially concerning its finite size and periodicity (like on a torus), the pattern decomposes into several independent connected components.
# It's a known result from grid theory and related problems (like billiard paths) that the number of such independent components formed by diagonal lines on an N x M grid is equal to gcd(N, M).
# Each independent component requires at least one stroke to draw. Since strokes must be straight lines, the minimum number of strokes needed to cover all components is equal to the number of components.
# Therefore, the minimum number of strokes required is gcd(N, M).

# この網目柄は、通常、2 方向の平行な斜線群からなるクロスハッチパターンである。
# 「交点で曲がらない」という条件は、各描画ストロークがグリッドを横断する一本の長い直線に対応することを意味する。
# N x M グリッド上のこれらの斜線の構造を考えると、特にグリッドの有限性と周期性(トーラス上で考える場合など)のために、
# パターン全体はいくつかの独立した連結成分に分解される。
# N x M グリッド上の斜線によって形成される独立した連結成分の数は、N と M の最大公約数 gcd(N, M) に等しいことが、
# 格子理論や関連する問題(ビリヤードの経路問題など)から知られている。
# 各独立成分を描くためには、少なくとも 1 回のストロークが必要となる。
# ストロークは直線でなければならないため、すべての成分を描くために必要な最小ストローク数は、
# 成分の数、すなわち gcd(N, M) となる。

def solve():
    """
    Reads N and M from standard input, calculates gcd(N, M), and prints the result.
    標準入力から N と M を読み込み、gcd(N, M) を計算して出力する。
    """
    try:
        # Read N and M from a single line of standard input, split by space, convert to integers
        # 標準入力から一行読み込み、スペースで分割して整数に変換
        n, m = map(int, sys.stdin.readline().split())

        # Input constraints validation (optional, assuming valid input based on problem statement)
        # 入力値の制約チェック (1 <= N, M <= 10^6)。問題文に基づき、有効な入力が与えられると仮定。
        if not (1 <= n <= 1000000 and 1 <= m <= 1000000):
             # Handle invalid input if necessary, though typically not needed in contests
             # 必要であればエラー処理(競技プログラミングでは通常不要)
             pass # Or raise an error

        # Calculate the greatest common divisor (GCD) using the math.gcd function
        # math.gcd 関数を使って最大公約数を計算
        result = math.gcd(n, m)

        # Print the result. The print() function automatically adds a newline.
        # 結果を出力。print関数は自動的に改行を追加する。
        print(result)

    except ValueError:
        # Handle potential error if input cannot be converted to integers
        # 整数変換エラーの処理 (通常、競技プログラミングでは入力形式が保証されることが多い)
        pass # Assume valid input format
    except Exception as e:
        # Handle any other unexpected errors during execution
        # その他の予期せぬエラー処理
        # print(f"An error occurred: {e}", file=sys.stderr) # Debugging purpose
        pass # Assume smooth execution for valid inputs

# This block ensures that solve() is called only when the script is executed directly
# スクリプトが直接実行された場合に solve() 関数を呼び出すための標準的な Python の書き方
if __name__ == '__main__':
    solve()
0