Skip to content

Commit 6545c3e

Browse files
committed
Add Restaurant problem solution and SRI calculation script
1 parent 8a0a85d commit 6545c3e

3 files changed

Lines changed: 608 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b85ac912",
6+
"metadata": {},
7+
"source": [
8+
"# 問題分析結果\n",
9+
"\n",
10+
"## 1. 多角的問題分析\n",
11+
"\n",
12+
"### 競技プログラミング視点\n",
13+
"- **制約**: `1 ≤ l, b ≤ 1000` → 小規模入力、数学的解法が最適\n",
14+
"- **核心**: 最大の正方形サイズ = `gcd(l, b)`、個数 = 面積 / 正方形面積\n",
15+
"\n",
16+
"### 業務開発視点\n",
17+
"- **型安全性**: 入力は整数、GCD計算で標準ライブラリ活用\n",
18+
"- **エッジケース**: `l = b` の場合(正方形そのもの)も正しく処理\n",
19+
"\n",
20+
"### Python特有考慮\n",
21+
"- `math.gcd()` は C実装で高速(O(log min(l,b)))\n",
22+
"- 整数除算 `//` で型安全性確保\n",
23+
"\n",
24+
"---\n",
25+
"\n",
26+
"## 2. アルゴリズム比較表\n",
27+
"\n",
28+
"| アプローチ | 時間計算量 | 空間計算量 | Python実装コスト | 可読性 | 標準ライブラリ活用 | 備考 |\n",
29+
"|---------|---------|---------|------------|-------|--------------|-----|\n",
30+
"| GCD利用 | O(log n) | O(1) | 低 | ★★★ | math.gcd | **最適解** |\n",
31+
"| 全探索 | O(min(l,b)) | O(1) | 中 | ★★☆ | なし | 非効率 |\n",
32+
"\n",
33+
"---\n",
34+
"\n",
35+
"## 3. 採用アルゴリズムと根拠\n",
36+
"\n",
37+
"### 数学的洞察\n",
38+
"1. 正方形の一辺は `l` と `b` の両方で割り切れる必要がある\n",
39+
"2. 最大サイズ = 最大公約数 `g = gcd(l, b)`\n",
40+
"3. 個数 = `(l / g) × (b / g) = (l × b) / g²`\n",
41+
"\n",
42+
"### 検証\n",
43+
"- 例1: `l=2, b=2` → `g=2` → `(2×2)/(2×2) = 1` ✓\n",
44+
"- 例2: `l=6, b=9` → `g=3` → `(6×9)/(3×3) = 6` ✓\n",
45+
"\n",
46+
"---\n",
47+
"\n",
48+
"## 4. HackerRankでの回答フォーマット\n",
49+
"\n",
50+
"```python\n",
51+
"#!/bin/python3\n",
52+
"\n",
53+
"import math\n",
54+
"import os\n",
55+
"import random\n",
56+
"import re\n",
57+
"import sys\n",
58+
"\n",
59+
"#\n",
60+
"# Complete the 'restaurant' function below.\n",
61+
"#\n",
62+
"# The function is expected to return an INTEGER.\n",
63+
"# The function accepts following parameters:\n",
64+
"# 1. INTEGER l\n",
65+
"# 2. INTEGER b\n",
66+
"#\n",
67+
"\n",
68+
"def restaurant(l: int, b: int) -> int:\n",
69+
" \"\"\"\n",
70+
" パンを最大サイズの正方形に切り分けた際の個数を計算\n",
71+
" \n",
72+
" アルゴリズム:\n",
73+
" - 正方形の最大サイズ = gcd(l, b)\n",
74+
" - 個数 = (l * b) / gcd(l, b)^2\n",
75+
" \n",
76+
" Time Complexity: O(log(min(l, b)))\n",
77+
" Space Complexity: O(1)\n",
78+
" \n",
79+
" Args:\n",
80+
" l: パンの長さ\n",
81+
" b: パンの幅\n",
82+
" \n",
83+
" Returns:\n",
84+
" 最大サイズの正方形の個数\n",
85+
" \"\"\"\n",
86+
" g = math.gcd(l, b)\n",
87+
" return (l * b) // (g * g)\n",
88+
"\n",
89+
"\n",
90+
"if __name__ == '__main__':\n",
91+
" fptr = open(os.environ['OUTPUT_PATH'], 'w')\n",
92+
"\n",
93+
" t = int(input().strip())\n",
94+
"\n",
95+
" for t_itr in range(t):\n",
96+
" first_multiple_input = input().rstrip().split()\n",
97+
"\n",
98+
" l = int(first_multiple_input[0])\n",
99+
"\n",
100+
" b = int(first_multiple_input[1])\n",
101+
"\n",
102+
" result = restaurant(l, b)\n",
103+
"\n",
104+
" fptr.write(str(result) + '\\n')\n",
105+
"\n",
106+
" fptr.close()\n",
107+
"```\n",
108+
"\n",
109+
"---\n",
110+
"\n",
111+
"## 5. Python最適化ポイント\n",
112+
"\n",
113+
"✅ **`math.gcd()` 活用**: C実装の高速GCD \n",
114+
"✅ **整数除算 `//`**: 型安全性保証 \n",
115+
"✅ **シンプルな実装**: 可読性とパフォーマンス両立 \n",
116+
"✅ **型ヒント**: pylanceエラー回避 \n",
117+
"\n",
118+
"### 計算量保証\n",
119+
"- **時間**: O(log(min(l, b))) per query → 全体 O(t × log(max constraint))\n",
120+
"- **空間**: O(1)"
121+
]
122+
}
123+
],
124+
"metadata": {
125+
"language_info": {
126+
"name": "python"
127+
}
128+
},
129+
"nbformat": 4,
130+
"nbformat_minor": 5
131+
}

0 commit comments

Comments
 (0)