Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b85ac912",
"metadata": {},
"source": [
"# 問題分析結果\n",
"\n",
"## 1. 多角的問題分析\n",
"\n",
"### 競技プログラミング視点\n",
"- **制約**: `1 ≤ l, b ≤ 1000` → 小規模入力、数学的解法が最適\n",
"- **核心**: 最大の正方形サイズ = `gcd(l, b)`、個数 = 面積 / 正方形面積\n",
"\n",
"### 業務開発視点\n",
"- **型安全性**: 入力は整数、GCD計算で標準ライブラリ活用\n",
"- **エッジケース**: `l = b` の場合(正方形そのもの)も正しく処理\n",
"\n",
"### Python特有考慮\n",
"- `math.gcd()` は C実装で高速(O(log min(l,b)))\n",
"- 整数除算 `//` で型安全性確保\n",
"\n",
"---\n",
"\n",
"## 2. アルゴリズム比較表\n",
"\n",
"| アプローチ | 時間計算量 | 空間計算量 | Python実装コスト | 可読性 | 標準ライブラリ活用 | 備考 |\n",
"|---------|---------|---------|------------|-------|--------------|-----|\n",
"| GCD利用 | O(log n) | O(1) | 低 | ★★★ | math.gcd | **最適解** |\n",
"| 全探索 | O(min(l,b)) | O(1) | 中 | ★★☆ | なし | 非効率 |\n",
"\n",
"---\n",
"\n",
"## 3. 採用アルゴリズムと根拠\n",
"\n",
"### 数学的洞察\n",
"1. 正方形の一辺は `l` と `b` の両方で割り切れる必要がある\n",
"2. 最大サイズ = 最大公約数 `g = gcd(l, b)`\n",
"3. 個数 = `(l / g) × (b / g) = (l × b) / g²`\n",
"\n",
"### 検証\n",
"- 例1: `l=2, b=2` → `g=2` → `(2×2)/(2×2) = 1` ✓\n",
"- 例2: `l=6, b=9` → `g=3` → `(6×9)/(3×3) = 6` ✓\n",
"\n",
"---\n",
"\n",
"## 4. HackerRankでの回答フォーマット\n",
"\n",
"```python\n",
"#!/bin/python3\n",
"\n",
"import math\n",
"import os\n",
"import random\n",
"import re\n",
"import sys\n",
"\n",
"#\n",
"# Complete the 'restaurant' function below.\n",
"#\n",
"# The function is expected to return an INTEGER.\n",
"# The function accepts following parameters:\n",
"# 1. INTEGER l\n",
"# 2. INTEGER b\n",
"#\n",
"\n",
"def restaurant(l: int, b: int) -> int:\n",
" \"\"\"\n",
" パンを最大サイズの正方形に切り分けた際の個数を計算\n",
" \n",
" アルゴリズム:\n",
" - 正方形の最大サイズ = gcd(l, b)\n",
" - 個数 = (l * b) / gcd(l, b)^2\n",
" \n",
" Time Complexity: O(log(min(l, b)))\n",
" Space Complexity: O(1)\n",
" \n",
" Args:\n",
" l: パンの長さ\n",
" b: パンの幅\n",
" \n",
" Returns:\n",
" 最大サイズの正方形の個数\n",
" \"\"\"\n",
" g = math.gcd(l, b)\n",
" return (l * b) // (g * g)\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" fptr = open(os.environ['OUTPUT_PATH'], 'w')\n",
"\n",
" t = int(input().strip())\n",
"\n",
" for t_itr in range(t):\n",
" first_multiple_input = input().rstrip().split()\n",
"\n",
" l = int(first_multiple_input[0])\n",
"\n",
" b = int(first_multiple_input[1])\n",
"\n",
" result = restaurant(l, b)\n",
"\n",
" fptr.write(str(result) + '\\n')\n",
"\n",
" fptr.close()\n",
"```\n",
"\n",
"---\n",
"\n",
"## 5. Python最適化ポイント\n",
"\n",
"✅ **`math.gcd()` 活用**: C実装の高速GCD \n",
"✅ **整数除算 `//`**: 型安全性保証 \n",
"✅ **シンプルな実装**: 可読性とパフォーマンス両立 \n",
"✅ **型ヒント**: pylanceエラー回避 \n",
"\n",
"### 計算量保証\n",
"- **時間**: O(log(min(l, b))) per query → 全体 O(t × log(max constraint))\n",
"- **空間**: O(1)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading