Skip to content
Open
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
58 changes: 58 additions & 0 deletions md/0034.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
ในโจทย์ข้อนี้ เราสนใจเฉพาะ $a, b, c, d$ ที่เป็นจำนวนเต็มและ $1 \le A \le 100$ ดังนั้นเรารู้ว่า $1 \le a, c \le 100$ และเรารู้ว่า $-100 \le C \le 100$ ดังนั้น $-100 \le b, d \le 100$

เพราะฉะนั้นเราสามารถไล่ค่า $a, b, c, d$ ทุกค่าที่เป็นไปได้แล้วเช็คว่าค่าเหล่านั้นสอดคล้องกับสมการหรือไม่ นั่นคือ
```cpp
for (int a = 1; a <= 100; a++) {
for (int b = -100; b <= 100; b++) {
for (int c = 1; c <= 100; c++) {
for (int d = -100; d <= 100; d++) {
if (a*c == A && a*d+b*c == B && b*d == C) {
cout << a << ' ' << b << ' ' << c << ' ' << d;
return 0;
}
}
}
}
}
```

แต่จะใช้เวลาทำงานค่อนข้างนานเนื่องจากมีหลายกรณีให้คำนวน เราจึงใช้ความรู้คณิตศาสตร์มาช่วยได้ เพราะเมื่อได้ค่า $A, B, C$ มาแล้ว สังเกตว่าถ้าเรารู้ค่า $a$ เราจะหาค่า $c$ ได้จากสมการ $A = ac$ และหากเรารู้ค่า $b$ เราจะสามารถหาค่า $d$ ได้จากสมการ $C = bd$

เราจึงทำการไล่ค่า $a$ ทุกค่าที่เป็นไปได้ นั่นคือตั้งแต่ $1 \le a \le 100$ และคำนวนค่า $c$ จากสมการ $c = A/a$ หาก $c$ ไม่เป็นจำนวนเต็ม เราจะข้ามค่า $a$ นั้นไปได้เลย
1. กรณีแรกถ้า $b = 0$ เราจะได้ว่า $C = bd = 0$ และ $B = ad + bc = ad$ ดังนั้นหาก $C = 0$ และ $d = B/a$ เป็นจำนวนเต็ม เราจะได้คำตอบคือ $a, 0, A/a, B/a$ ซึ่งเป็นจำนวนเต็มทั้งหมด
2. ถ้า $b \ne 0$ เราจะได้ว่า $d = C/b$ และถ้า $d$ เป็นจำนวนเต็ม เราจะต้องตรวจสอบต่อว่า $B = ad + bc$ หรือไม่ ถ้าใช่ เราจะได้คำตอบคือ $a, b, A/a, C/b$

```cpp
#include<bits/stdc++.h>
using namespace std;

int main () {
int A, B, C;
cin >> A >> B >> C;

for (int a = 1; a <= 100; a++) {
int c = A / a;
if (a * c != A) continue;
for (int b = -100; b <= 100; b++) {
if (b == 0) {
if (C != 0) continue;
int d = B / a;
if (a * d == B) {
cout << a << ' ' << b << ' ' << c << ' ' << d;
return 0;
}
}
int d = C / b;
if (b * d != C) continue;
if (a * d + b * c == B) {
cout << a << ' ' << b << ' ' << c << ' ' << d;
return 0;
}
}
}

cout << "No Solution";
return 0;
}

```