Scenes From a Memory (Codeforces 1562B)
Tags: Math, number theory

During the hypnosis session, Nicholas suddenly remembered a positive integer $$$n$$$, which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?For some numbers doing so is impossible: for example, for number $$$53$$$ it's impossible to delete some of its digits to obtain a not prime integer. However, for all $$$n$$$ in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.Note that you cannot remove all the digits from the number.A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. $$$1$$$ is neither a prime nor a composite number.

Input: Each test contains multiple test cases.The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^3$$$), denoting the number of test cases. Description of the test cases follows.The first line of each test case contains one positive integer $$$k$$$ ($$$1 \le k \le 50$$$) — the number of digits in the number.The second line of each test case contains a positive integer $$$n$$$, which doesn't contain zeros in decimal notation ($$$10^{k-1} \le n < 10^{k}$$$). It is guaranteed that it is always possible to remove less than $$$k$$$ digits to make the number not prime.It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.

Output: For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions. If there are multiple solutions, print any.

Note: In the first test case, you can't delete $$$2$$$ digits from the number $$$237$$$, as all the numbers $$$2$$$, $$$3$$$, and $$$7$$$ are prime. However, you can delete $$$1$$$ digit, obtaining a number $$$27 = 3^3$$$.In the second test case, you can delete all digits except one, as $$$4 = 2^2$$$ is a composite number.

Sample Inputs:
7
3
237
5
44444
3
221
2
35
3
773
1
4
30
626221626221626221626221626221

Sample Outputs:
2
27
1
4
1
1
2
35
2
77
1
4
1
6

Solution (Python):
def is_prime(x):
    if x < 2:
        return 0
    limit = int(x ** 0.5)
    for i in range(2, limit + 1):
        if not x % i:
            return 0
    return 1

def solve():
    k = int(input())
    s = str(input())
    answer = '-1'
    for i in range(k):
        if s[i] == '1' or s[i] == '4' or s[i] == '6' or (s[i] == '8') or (s[i] == '9'):
            print(f'1\n{s[i]}')
            return
    for j in range(k):
        for m in range(j + 1, k):
            if not is_prime(int(s[j] + s[m])):
                print(f'2\n{s[j] + s[m]}')
                return
for _ in range(int(input())):
    solve()

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (k, n) = (readInt(), readInt())
    S = list(str(n))
    cnt = [0] * 10
    for v in S:
        cnt[int(v)] += 1
    for v in [1, 4, 6, 8, 9]:
        if cnt[v]:
            print(1)
            print(v)
            return
    for i in range(10):
        if cnt[i] > 1:
            print(2)
            print(10 * i + i)
            return
    if len(S) == 2:
        print(2)
        print(''.join(S))
        return
    tmp = []
    for c in S:
        if c != '3':
            tmp.append(c)
    while tmp and len(tmp) > 2:
        tmp.pop()
    print(len(tmp))
    print(''.join(tmp))
    return
for _ in range(readInt()):
    solve()

Solution (Python):
num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()
for s in [*open(0)][2::2]:
    print((t := ({*'14689'} & {*s})) and '1\n' + t.pop() or '2\n' + ((t := ({*'25'} & {*s[1:]})) and s[0] + t.pop() or [x * 2 for x in (*s, '') if s.count(x) ^ 1][0] or s.replace('3', '')))