[USACO] Superprime Rib
Posted 2008/01/22 15:49Super Prime은 예를 들어 설명하자면 2333은 prime이다. 2333에서 맨 뒷자리를 제거한 233도 prime이고 여기서 또 뒷자리를 제거한 23도 Prime, 2도 Prime이다. 이러한 경우 Super prime이라고 한다. 이번 문제의 핵심은 역시 입력의 수를 최대한 줄이고 super prime을 검사해야 한다.
해결책은 다음과 같다.
가장 앞자리 부터 prime인지 확인한다. 그러므로 2, 3, 5, 7로 시작하는 숫자만 검사하면 된다. 이런식으로 점점 n 자리수까지 뻗어나가면 최소한의 노력으로 Super prime을 구할 수 있다. 간만에 한번에 통과했다. ^^
Superprime Rib
Butchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John ensures that a purchaser of his prime ribs gets really prime ribs because when sliced from the right, the numbers on the ribs continue to stay prime right down to the last rib, e.g.:
7 3 3 1
The set of ribs denoted by 7331 is prime; the three ribs 733 are prime; the two ribs 73 are prime, and, of course, the last rib, 7, is prime. The number 7331 is called a superprime of length 4.
Write a program that accepts a number N 1 <=N<=8 of ribs and prints all the superprimes of that length.
The number 1 (by itself) is not a prime number.
PROGRAM NAME: sprime
INPUT FORMAT
A single line with the number N.
SAMPLE INPUT (file sprime.in)
4
OUTPUT FORMAT
The superprime ribs of length N, printed in ascending order one per line.
SAMPLE OUTPUT (file sprime.out)
2333 2339 2393 2399 2939 3119 3137 3733 3739 3793 3797 5939 7193 7331 7333 7393
'C.S.E > Algorithm' 카테고리의 다른 글
| UVA에서 JAVA Submit하기 (2) | 2008/01/25 |
|---|---|
| [USACO] Checker Challenge (0) | 2008/01/23 |
| [USACO] Superprime Rib (0) | 2008/01/22 |
| [USACO] Prime Palindromes (0) | 2008/01/22 |
| [USACO] Number Triangle (0) | 2008/01/21 |
| [USACO] Mother's Milk (0) | 2008/01/21 |
- Filed under : C.S.E/Algorithm
- Tag : Algorithm, USACO
- Comment Trackback
이올린에 북마크하기