EKG 수열

시간 제한1초메모리 제한128 MB

요약
EKG 수열을 1000000번째 항까지 만들고, 각 질의 정수 n이 처음 나타나는 위치를 1부터 세어 출력한다.
난이도

보통10점 중 6점

유형
정수론, 완전 탐색, 구현, 수학
정답자
아직 제출이 없습니다

문제

EKG 수열은 다음과 같이 만들어지는 양의 정수 수열이다. 수열의 처음 두 항은 1과 2이다. 그 이후의 각 항은, 아직 사용되지 않은 양의 정수 중에서 바로 앞 항과 1보다 큰 공약수를 가지는(즉 서로소가 아닌) 가장 작은 수이다. 따라서 세 번째 항은 4이다(아직 쓰이지 않은 가장 작은 짝수). 그 다음은 6, 그 다음은 3이다. 이 수열의 처음 몇 항은 다음과 같다.

1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, 18, 14, 7, 21, 24, 16, 20, 22, 11, 33, 27

이 수열은 값이 매우 불규칙하게 오르내리기 때문에 EKG(심전도)라는 이름이 붙었다. 이 수열에는 자명하지 않지만 흥미로운 성질이 몇 가지 있다. 하나는 모든 양의 정수가 언젠가 반드시 수열에 나타난다는 것이고, 다른 하나는 모든 소수가 증가하는 순서로 나타난다는 것이다. 주어진 정수가 이 수열에서 몇 번째 위치에 나타나는지 구하여라.

입력

입력은 여러 개의 테스트 케이스로 이루어진다. 각 케이스는 하나의 정수 nn (1≤n≤3000001 \le n \le 300000)이 적힌 한 줄이다. 마지막 테스트 케이스 뒤에는 0이 입력된다. 300,000 이하의 모든 정수를 포함하는 EKG 수열의 앞부분에는 1,000,000보다 큰 정수가 나타나지 않는다.

출력

각 테스트 케이스마다 다음 형식으로 한 줄을 출력한다.

The number n appears in location p.

여기서 nn은 주어진 수이고 pp는 그 수가 EKG 수열에서 나타나는 위치이다. pp는 1,000,000을 넘지 않음이 보장된다.

예제4

  1. 예제 1

    입력
    12
    21
    2
    33
    100000
    299977
    0
    
    예상 출력
    The number 12 appears in location 7.
    The number 21 appears in location 15.
    The number 2 appears in location 2.
    The number 33 appears in location 21.
    The number 100000 appears in location 97110.
    The number 299977 appears in location 584871.
    
  2. 예제 2

    입력
    1
    0
    
    예상 출력
    The number 1 appears in location 1.
    
  3. 예제 3

    입력
    2
    3
    5
    7
    11
    13
    17
    19
    0
    
    예상 출력
    The number 2 appears in location 2.
    The number 3 appears in location 5.
    The number 5 appears in location 10.
    The number 7 appears in location 14.
    The number 11 appears in location 20.
    The number 13 appears in location 28.
    The number 17 appears in location 33.
    The number 19 appears in location 37.
    
  4. 예제 4

    입력
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    0
    
    예상 출력
    The number 1 appears in location 1.
    The number 2 appears in location 2.
    The number 3 appears in location 5.
    The number 4 appears in location 3.
    The number 5 appears in location 10.
    The number 6 appears in location 4.
    The number 7 appears in location 14.
    The number 8 appears in location 8.
    The number 9 appears in location 6.
    The number 10 appears in location 9.