๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Baekjoon][Math] 6588๋ฒˆ ๊ณจ๋“œ๋ฐ”ํ์˜ ์ถ”์ธก(feat. C++ ์ž…์ถœ๋ ฅ ์‹œ๊ฐ„ ๋‹จ์ถ•)

ibelieveinme 2022. 4. 3. 09:37
728x90

[๋ฌธ์ œ]

 

6588๋ฒˆ: ๊ณจ๋“œ๋ฐ”ํ์˜ ์ถ”์ธก

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, n = a + b ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋•Œ, a์™€ b๋Š” ํ™€์ˆ˜ ์†Œ์ˆ˜์ด๋‹ค. ์ˆซ์ž์™€ ์—ฐ์‚ฐ์ž๋Š” ๊ณต๋ฐฑ ํ•˜๋‚˜๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ ์žˆ๋‹ค. ๋งŒ์•ฝ, n์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ผ๋ฉด, b-a๊ฐ€ ๊ฐ€์žฅ ํฐ

www.acmicpc.net

"4๋ณด๋‹ค ํฐ ๋ชจ๋“  ์ง์ˆ˜๋Š” ๋‘ ํ™€์ˆ˜ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค." ๋Š” ๊ณจ๋“œ๋ฐ”ํ์˜ ์ถ”์ธก์ด ๋งž๋Š”์ง€ ํ‹€๋ ธ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ.

 

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, n = a + b ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋•Œ, a์™€ b๋Š” ํ™€์ˆ˜ ์†Œ์ˆ˜์ด๋‹ค. 

๋งŒ์•ฝ, n์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ผ๋ฉด, b-a๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฒƒ์„ ์ถœ๋ ฅํ•œ๋‹ค.

๋‘ ํ™€์ˆ˜ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ n์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” "Goldbach's conjecture is wrong."์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

[ํ’€์ด]

์‹œ๊ฐ„ ์ œํ•œ 1์ดˆ.

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜๋Š” 100,000๊ฐœ.

ํ™•์ธํ•ด์•ผํ•  ์ง์ˆ˜ ์ •์ˆ˜ n์˜ ๋ฒ”์œ„. 6 ≤ n ≤ 1000000

 

1์ดˆ์˜ ์ œํ•œ ์‹œ๊ฐ„์„ ๋„˜๊ธฐ์ง€ ์•Š์œผ๋ ค๋ฉด ์—ฐ์‚ฐ ํšŸ์ˆ˜๊ฐ€ 1์–ต ๋ฒˆ์„ ์ดˆ๊ณผํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜๊ฐ€ 100,000๊ฐœ์ด๊ณ  ํ™•์ธํ•ด์•ผํ•  ์ง์ˆ˜ ์ •์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ 1,000,000 ๊นŒ์ง€์ด๋ฏ€๋กœ 1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋ชจ๋“  ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ 100,000 * 1,000,000 ์˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜์–ด 100,000,000,000 = 1,000์–ต ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋•Œ์˜ ์—ฐ์‚ฐ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์€ 1000์ดˆ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ๋ฃจํŠธn๊นŒ์ง€ ํ™•์ธํ•œ๋‹ค๊ณ  ํ•ด๋„ 100,000 * 1,000 = 100,000,000 = 10์–ต ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ์•„์ง 10์ดˆ...

 

๋”ฐ๋ผ์„œ ์—๋ ˆํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์—๋ ˆํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” 2๋ถ€ํ„ฐ ์ž์‹ ์˜ ๋ฐฐ์ˆ˜๋ฅผ ๋‹ค ์ง€์šฐ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์‹œ๊ฐ„๋ณต์žก๋„ O(N(log(logN)))๋ฅผ ๊ฐ€์ง„๋‹ค. 

 

๊ทธ๋Ÿฌ๋‚˜.......... ์•„๋ฌด๋ฆฌ ์ค„์—ฌ๋„ ์ž…์ถœ๋ ฅ์‹œ๊ฐ„์„ ์ค„์ด์ง€ ๋ชปํ•ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ๊ทธ๋ฆฌ๊ณ  1๋ฒˆ, 2๋ฒˆ ํ’€์ด ๋ชจ๋‘ ์•„๋ž˜ ๊ตฌ๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋‹ˆ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค. ์ด ๊ตฌ๋ฌธ์„ ์•ˆ์“ฐ๊ณ  ํ•ด๊ฒฐํ•˜๋ ค๋ฉด cin, cout ๋Œ€์‹ ์— scanf, prinf๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๊ฒฐ๊ฐ€๋Šฅํ•˜๋‹ค. (4๋ฒˆ ์ฝ”๋“œ ์ฐธ๊ณ . ์ด๋ฏธ C์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ์ƒ๊ด€์—†์Œ!)

cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);

 

๋งˆ์น˜๋ฉด์„œ... C++ ์ž…์ถœ๋ ฅ ์‹œ๊ฐ„ ๋‹จ์ถ•์— ๊ด€ํ•˜์—ฌ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด!

 

1. cin, cout ๋Œ€์‹ ์— scanf, printf๋ฅผ ์“ฐ์ž. scanf๋Š” 0.798์ดˆ์˜ ์ˆ˜ํ–‰์‹œ๊ฐ„์„ ๊ฐ€์ง€๋Š”๋ฐ, cin์€ 2.051์ดˆ์˜ ์‹œ๊ฐ„์„ ๊ฐ–๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.(์ถœ์ฒ˜: https://2heedu.tistory.com/64)

์ฐธ๊ณ ๋กœ ์œ„์˜ ๊ตฌ๋ฌธ์œผ๋กœ c++๊ณผ c์˜ ํ‘œ์ค€ ์ŠคํŠธ๋ฆผ์˜ ๋™๊ธฐํ™” ๋„๋ฉด ์ž…์ถœ๋ ฅ ์†๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ c์™€ c++ ์ž…์ถœ๋ ฅ ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜์ง€ ์•Š์•„ ๋‘ ๋ฌธ๋ฒ•์„ ์„ž์–ด์ผ์„ ๋•Œ, ์ˆœ์„œ๊ฐ€ ๋’ค์„ž์—ฌ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ณ , c++ ์ŠคํŠธ๋ฆผ๋“ค์˜ thread ์•ˆ์ •์„ฑ ์—ญ์‹œ ๋ณด์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

2. endl ๋Œ€์‹  "\n"๋ฅผ ์“ฐ์ž. endl์€ ๊ฐœํ–‰์„ ํ•  ๋ฟ ์•„๋‹ˆ๋ผ ๋‚ด๋ถ€ ๋ฒ„ํผ๋„ ๋น„์›Œ์ฃผ๋ฏ€๋กœ "\n"๋ณด๋‹ค ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค.

 

[์ฝ”๋“œ] 3, 4๋ฒˆ ์ฝ”๋“œ๊ฐ€ ์ •๋‹ต ์ฝ”๋“œ!

 

1๋ฒˆ ํ’€์ด. ์‹œ๊ฐ„์ดˆ๊ณผ.

#include <iostream>
#include <cmath>
using namespace std;

bool canGoldBahGuess(int num);
bool isPrime(int num);

int main() {
    int num;
    while(true) {
        cin >> num;
        if (num == 0) break;
        if (!canGoldBahGuess(num)) cout << "Goldbach's conjecture is wrong.\n";
    }
    return 0;
}

bool canGoldBahGuess(int num) {
    for (int i = 3; i <= num/2; i++) {//3๋ถ€ํ„ฐ ํ™€์ˆ˜&์†Œ์ˆ˜ ํŒ๋ณ„
        if (i % 2 != 0 && isPrime(i) && (num-i) % 2 != 0 && isPrime(num - i)){
            cout << num << " = " << i << " + " << num - i << "\n";
            return true;
        }
    }
    return false;
}

bool isPrime(int num) {
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) return false;
    }
    return true;
}

 

2๋ฒˆ ํ’€์ด. ์‹œ๊ฐ„์ดˆ๊ณผ

#include <iostream>
#include <cmath>
using namespace std;

void MakeDecimalTable();
bool canGoldbach(int num);

const int MAX_SIZE = 1000000;
int decimalTable[MAX_SIZE + 1];

int main() {
    int num;
    bool answer = true;

    MakeDecimalTable();

    while(true) {
        cin >> num;
        if (num == 0) break;
        if (!canGoldbach(num)) cout << "Goldbach's conjecture is wrong.\n";
    }
    return 0;
}

void MakeDecimalTable() {
    fill_n(decimalTable, sizeof(decimalTable)/sizeof(int), 1);
    for (int i = 2; i <= sqrt(MAX_SIZE ); i++) {//2๋ถ€ํ„ฐ ์†Œ์ˆ˜ํŒ๋ณ„ํ•˜๋Ÿฌ gogo
        if (decimalTable[i] != 0) {
            for (int j = 2; i*j <= MAX_SIZE ; j++) {//๊ทธ ๋ฐฐ์ˆ˜๋ฅผ ๋ชจ๋‘ 0์œผ๋กœ ๋ฐ”๊ฟˆ
                decimalTable[i*j] = 0;
            }
        }
    }
}

bool canGoldbach(int num) {
    for (int i = 3; i <= num / 2; i++) {//3๋ถ€ํ„ฐ ์†Œ์ˆ˜ ํŒ๋ณ„
        if (decimalTable[i] && decimalTable[num - i]) {
            cout << num << " = " << i << " + " << num - i << "\n";
            return true;
        }
    }
    return false;
}

 

3๋ฒˆ ํ’€์ด. ํ†ต๊ณผ.

#include <iostream>
#include <cmath>
using namespace std;

void MakePrimeTable();
bool canGoldbach(int num);

const int MAX_SIZE = 1000000;
int primeTable[MAX_SIZE + 1];

int main() {
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);

    int num;
    bool answer = true;

    MakePrimeTable();

    while(true) {
        cin >> num;
        if (num == 0) break;
        if (!canGoldbach(num)) cout << "Goldbach's conjecture is wrong.\n";
    }
    return 0;
}

void MakePrimeTable() {//์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด
    for (int i = 2; i <= sqrt(MAX_SIZE); i++) {//2๋ถ€ํ„ฐ ์†Œ์ˆ˜ํŒ๋ณ„ํ•˜๋Ÿฌ gogo
        if (primeTable[i] == 0) {
            for (int j = 2; i*j <= MAX_SIZE; j++) {//๊ทธ ๋ฐฐ์ˆ˜๋ฅผ ๋ชจ๋‘ 0์œผ๋กœ ๋ฐ”๊ฟˆ
                primeTable[i*j] = 1;
            }
        }
    }
}

bool canGoldbach(int num) {
    for (int i = 3; i <= num / 2; i++) {//3๋ถ€ํ„ฐ ์†Œ์ˆ˜ ํŒ๋ณ„
        if (primeTable[i] == 0 && primeTable[num - i] == 0) {
            cout << num << " = " << i << " + " << num - i << "\n";
            return true;
        }
    }
    return false;
}

 

4๋ฒˆ ํ’€์ด. ํ†ต๊ณผ

#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

const int MAX_SIZE = 1000000;

int main() {
	vector<int> primeTable;
	bool primeNoCheckTable[MAX_SIZE + 1];
	for (int i = 2; i*i <= MAX_SIZE; i++) {//2๋ถ€ํ„ฐ ์†Œ์ˆ˜ํŒ๋ณ„ํ•˜๋Ÿฌ gogo
		if (!primeNoCheckTable[i]) {
			if(i != 2) primeTable.push_back(i);
			for (int j = i + i; j <= MAX_SIZE; j += i) {//๊ทธ ๋ฐฐ์ˆ˜๋ฅผ ๋ชจ๋‘ ์†Œ์ˆ˜ ํ™•์ธ์„ ๋๋ƒˆ๋‹ค๋Š” ์˜๋ฏธ๋กœ true๋กœ ๋ฐ”๊ฟˆ
				primeNoCheckTable[j] = true;
			}
		}
	}

	int num;
	bool answer;
	while (true) {
		scanf("%d", &num);
		if (num == 0) break;
		for (int prime : primeTable) {
			if (!primeNoCheckTable[num - prime]) {
				printf("%d = %d + %d\n", num, prime, num - prime);
				answer = true;
				break;
			}
		}
		if (!answer) printf("Goldbach's conjecture is wrong.\n");
	}
	return 0;
}
728x90