๐Ÿ˜‰/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์นด๋ผ์ถ”๋ฐ”์˜ ๋น ๋ฅธ ๊ณฑ์…ˆ(Karatsuba)

ruhz 2020. 7. 28. 17:17

123 × 456 = ?

ํ‰์†Œ์— ์ผ์ƒ์ ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋˜ ๋ฐฉ๋ฒ•์„ ๋ถ„์„์ ์œผ๋กœ ํ’€์–ด์จ๋ณด๋ฉด ์ด๋ ‡๋‹ค.
์šฐ๋ฆฌ๋Š” ํ‰์†Œ ํ•œ ์ž๋ฆฌ์— 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์‹ญ์ง„๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ ์ž๋ฆฌ๋ณ„๋กœ ์‹ญ์˜ ์ž๋ฆฌ ์ˆ˜๋Š” ๋‹ค์Œ ์นธ์œผ๋กœ ๋„˜๊ฒจ์ค€๋‹ค(์˜ˆ๋ฅผ ๋“ค์–ด 10์˜ ์ž๋ฆฌ 27์€ (20 + 7)์ด๋ฏ€๋กœ 100์˜ ์ž๋ฆฌ๋กœ 2๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค).

 

(4 * 10000) + (13 * 1000) + (28 * 100) + (27 * 10) + (18 * 1)
= (
5 * 10000) + (6 * 1000) + (0 * 100) + (8 * 10) + (8 * 1)
= 56088

 

์ตœ์ข…์ ์œผ๋กœ ๊ฐ’์„ ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐํ•ด ๋‚ผ ์ˆ˜์žˆ๋‹ค.
456์˜ ๊ฐฏ์ˆ˜๋งŒํผ, 123์˜ ๊ฐ ์›์†Œ(1, 2, 3)๊ณผ ํ•œ ๋ฒˆ์”ฉ ๊ณฑ์…ˆ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•จ์„ ์•Œ ์ˆ˜์žˆ๋‹ค.
๋”ฐ๋ผ์„œ ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋งŒ์•ฝ (n ์ž๋ฆฟ์ˆ˜) × (n ์ž๋ฆฟ์ˆ˜)์˜ ์ƒํ™ฉ์—์„œ๋Š” n2 ๋ฒˆ์˜ ๊ณฑ์…ˆ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

โ€‹

 

 

์นด๋ผ์ถ”๋ฐ”์˜ ๋น ๋ฅธ ๊ณฑ์…ˆ

์นด๋ผ์ถ”๋ฐ”(Karatsuba)๋Š” ๋ถ„ํ• ์ •๋ณต์„ ์ด์šฉํ•ด, ์ด๋Ÿฌํ•œ ๊ณผ์ •์˜ ํšจ์œจ์„ ํฌ๊ฒŒ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์˜€๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด,1234 × 5678 ์˜ ๊ณ„์‚ฐ์„ ์นด๋ผ์ถ”๋ฐ”์˜ ๋น ๋ฅธ ๊ณฑ์…ˆ์„ ์ด์šฉํ•ด๋ณด์ž.
1234 = 12 × 100 + 34,
5678 = 56 × 100 + 78๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.โ€‹

 

1234 × 5678
= (12 × 100 + 34) × (56 × 100 + 78)
= (12 × 56) × 10000 + (12 × 78 + 34 × 56) × 100 + 34 × 78โ€‹

 

์—ฌ๊ธฐ์„œ 10์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์˜ ๊ณ„์ˆ˜๋ฅผ z0, z1, z2๋กœ ๋†“๋Š”๋‹ค.
z0 = 34 × 78
z1 = 12 × 78 + 34 × 56
z2 = 12 × 56โ€‹

 

z1์€ ๋‹ค์‹œ ์“ฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์€์ง€๋งŒ ํ™•์ธํ•ด๋ณด์ž.
z1 = (34 + 12) × (56 + 78) - z0 - z2

 

์ด์ œ z0, z1, z2๋ฅผ ์ด์šฉํ•˜๋ฉด, ๋‹จ 3๋ฒˆ์˜ ๊ณฑ์…ˆ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค!
๊ฐ ๊ณฑ์…ˆ์— ๋Œ€ํ•˜์—ฌ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ•  ๊ฒƒ์ด๋ฏ€๋กœ, 3log n ( = nlog 3)์˜ ํšจ์œจ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค!
log 3 = 0.4 ์ •๋„์ด๋ฏ€๋กœ ํ›จ์”ฌ ์ข‹์€ ํšจ์œจ์„ ๋ณด์ธ๋‹ค. ๋‹ค๋งŒ ์ฝ”๋“œ์˜ ๊ธธ์ด ์ƒ ํฌ๊ธฐ๊ฐ€ ํฐ ๊ณฑ์…ˆ์—์„œ๋งŒ ๋” ๋น ๋ฅธ ์‹คํ–‰์„ ๋ณด์ธ๋‹ค. โ€‹์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;

int karatsuba(int num1, int num2) {
    // ํ•œ ์ž๋ฆฟ์ˆ˜๊ฐ€ ๋˜๋ฉด ๊ทธ๋ƒฅ ์—ฐ์‚ฐ
	if (num1 < 10 || num2 < 10)
		return num1 * num2;
	
    // ์ˆซ์ž๋ฅผ ๋ถ„ํ• ํ•  m์„ ๋‘˜ ์ค‘ ๋” ์ž‘์€ ์ž๋ฆฟ์ˆ˜์˜ ์ ˆ๋ฐ˜์œผ๋กœ ์„ค์ •
	int m;
	m = min((int)(log10(num1) + 1), (int)(log10(num2) + 1));
	m /= 2;

    // num์„ high(์•ž๋ถ€๋ถ„), low(๋’ท๋ถ€๋ถ„)์œผ๋กœ ๋ถ„ํ• 
	int high1, low1;
	high1 = num1 / pow(10, m);
	low1 = num1 % (int)pow(10, m);	
	int high2, low2;
	high2 = num2 / pow(10, m);
	low2 = num2 % (int)pow(10, m);

    // z0, z1, z2๋ฅผ ๊ฐ๊ฐ ์žฌ๊ท€ํ˜ธ์ถœ
	int z0 = karatsuba(low1, low2);
	int z1 = karatsuba((low1 + high1), (low2 + high2));
	int z2 = karatsuba(high1, high2);

	return (z2 * pow(10 , (m * 2)) + ((z1 - z2 - z0) * pow(10 , m)) + z0);
}

int main() {
	int A, B;
	cin >> A >> B;
	cout << karatsuba(A, B) << endl;
	return 0;
}

 

์ž๋ฃŒํ˜•์ด int๋ผ์„œ ํฐ ์—ฐ์‚ฐ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒ ์ง€๋งŒ, ์›๋ฆฌ๋ฅผ ๋ณด์ด๊ธฐ ์œ„ํ•ด ์ตœ๋Œ€ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

 

ruhz3/CodingTest

To prepare for coding test. Contribute to ruhz3/CodingTest development by creating an account on GitHub.

github.com