출처: https://train.usaco.org/usacogate


문제 설명

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.

입력

A single line with the number N.

출력 

The superprime ribs of length N, printed in ascending order one per line.

입력 예

4

출력 예

2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393


문제 풀이 내용

제일 왼쪽에 올 수 있는 숫자는 2/3/5/7 (한자리수 소수)만 올 수 있다. 또한 두자리수 이상의 수에서 제일 앞을 제외한 모든 자리수는 (1/3/7/9) 만이 가능하다. 이 조건을 만족하도록, 왼쪽에서부터 숫자를 만들어 출력한다.

프로그램 내용

더보기
#define MAX_N 8

int digit_limit[4] = {1,3,7,9};

vector <long> sprimes;
int s_start[MAX_N] = {0};
int s_end[MAX_N] = {0};

bool check_prime(long input);
void build_digit1();
void build_digit2(); /// 1digit rib-prime + digit_limit => check_prime
... 
void build_digit8(); /// 7digit rib-prime + digit_limit => check_prime
void build_numbers(); /// build 1~8digit rib-prime

int main() {
    int digit;

    /// build N digit numbers
    build_numbers();

    for (int idx=0; idx<sprimes.size(); ++idx)
            fout << sprimes[idx] <<endl;

 

Chapter 1. Getting started

'USACO Training' 카테고리의 다른 글

Problem 2.1.4 Ordered Fractions  (0) 2019.09.18
Problem 2.2.5 Runaround Numbers  (0) 2019.09.18
Problem 1.6.3 Prime Palindromes  (0) 2019.09.16
Problem 1.6.2 Number Triangles  (0) 2019.09.16
Problem 1.5.2 Arithmetic Progressions  (0) 2019.09.14

+ Recent posts