πŸ”₯ PS(Problem Solving) πŸ”₯/goorm

[ꡬ름LEVEL] 망가진 μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체

dar0m! 2020. 3. 27. 03:16
λ‚œμ΄λ„ μ •λ‹΅λ₯ 
β˜…β˜… -%


프리미엄 μ•Œκ³ λ¦¬μ¦˜ μœ„ν΄λ¦¬ λΉ„νƒ€μ•Œκ³  μ‹œμ¦Œ3 4μ£Όμ°¨

 

goorm

ꡬ름은 ν΄λΌμš°λ“œ κΈ°μˆ μ„ μ΄μš©ν•˜μ—¬ λˆ„κ΅¬λ‚˜ 코딩을 배우고, μ‹€λ ₯을 ν‰κ°€ν•˜κ³ , μ†Œν”„νŠΈμ›¨μ–΄λ₯Ό κ°œλ°œν•  수 μžˆλŠ” ν΄λΌμš°λ“œ μ†Œν”„νŠΈμ›¨μ–΄ μƒνƒœκ³„μž…λ‹ˆλ‹€.

www.goorm.io

문제

 

ν•΄κ²°

  1. 1은 μ†Œμˆ˜κ°€ μ•„λ‹ˆλ‹ˆ μ œμ™Έν•˜κ³  2λΆ€ν„° μ‹œμž‘.
  2. i둜 λ‚˜λ‰˜μ–΄μ§€λŠ” n에 λŒ€ν•˜μ—¬
    1. iκ°€ μ†Œμˆ˜μ΄κ³ , (n / i)도 μ†Œμˆ˜λΌλ©΄ κ±ΈλŸ¬μ§„λ‹€ β†’ 1
    2. iλ‚˜ (n / i)쀑 ν•˜λ‚˜λΌλ„ μ†Œμˆ˜κ°€ μ•„λ‹ˆλΌλ©΄ ν•΄λ‹Ή n은 망가진 μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체에 κ±ΈλŸ¬μ§€μ§€ μ•ŠλŠ”λ‹€. β†’ 0
  3. i*iκ°€ n일 λ•ŒκΉŒμ§€ λ°˜λ³΅λ¬Έμ„ λŒλ©΄μ„œ ν•œ λ²ˆμ΄λΌλ„ λ‚˜λ‰˜μ–΄μ§€λŠ” iλ₯Ό λ°œκ²¬ν•˜μ§€ λͺ»ν–ˆλ‹€λ©΄ κ·Έ μˆ˜λŠ” κ·Έ 자체둜 μ†Œμˆ˜μ΄λ‹€. β†’ 1

 

μ½”λ“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n;
bool isPrime(ll num) {
    for (ll i = 2; i * i <= num; i++) {
        if (!(num % i)) return 0;
    }
    return 1;
}
int main() {
    scanf("%lld"&n);
    if (n == 1) {
        printf("0");
        return 0;
    }
    for (ll i = 2; i * i <= n; i++) {
        if (!(n % i)) {
            if (isPrime(i) && isPrime(n / i)) {
                printf("1");
                return 0;
            }
            else { // ν•˜λ‚˜λΌλ„ λ‚˜λˆ μ§€λŠ”λ° μ†Œμˆ˜λ“€μ˜ κ³±μ΄ μ•„λ‹ˆλ©΄ 0
                printf("0");
                return 0;
            }
        }
    }
    printf("1");
    return 0;
}
cs

 

1λΆ€ν„° μ‹œμž‘ν•˜λŠ” 경우

1λΆ€ν„° ν™•μΈν•˜λŠ” κ²½μš°μ—λŠ” μ†Œμˆ˜νŒλ³„ν•˜λŠ” ν•¨μˆ˜μΈ isPrimeμ—μ„œ 1일 λ•Œ 0을 λ°˜ν™˜ν•˜λŠ” λ¬Έμž₯을 μΆ”κ°€ν•΄μ•Ό ν•œλ‹€.

 
bool isPrime(ll num) {
	if (num == 1) return 0;
	for (ll i = 2; i * i <= num; i++) {
		if (!(num % i)) return 0;
	}
	return 1;
}