Scenes From a Memory

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 5353 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. 11 is neither a prime nor a composite number.

Input

Each test contains multiple test cases.

The first line contains one positive integer t� (1≤t≤1031≤�≤103), 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≤k≤501≤�≤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 (10k−1≤n<10k10�−1≤�<10�). 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 104104.

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.

Sample 1

InputcopyOutputcopy
7
3
237
5
44444
3
221
2
35
3
773
1
4
30
626221626221626221626221626221
2
27
1
4
1
1
2
35
2
77
1
4
1
6

题目:从一个数字中去掉多少位数,使这个数字不是质数,也就是合数或等于1

解题:

如果数字中有1,4,6,8,9,则直接输出

如果有两个相同的数字,则输出两个相同的数字,例如22,33,55,77

如果数字中有2:可以输出32,52,25,72,27

如果没有2,有5,那就35,75

以上就是所有情况。

def solve():  
    k = int(input())  
    s = input()  
    cnt = [0] * 10  
    pos = [0] * 10  
      
    for i, ch in enumerate(s):  
        digit = int(ch)  
        cnt[digit] += 1  
        pos[digit] = i  
      
    for digit in [1, 4, 6, 8, 9]:  
        if cnt[digit] > 0:  
            print(1)  
            print(digit)  
            return  
      
    for digit in range(1, 10):  
        if cnt[digit] >= 2:  
            print(2)  
            print(digit*10+digit)  
            return  
      
    if cnt[2] > 0:  
        if cnt[3] > 0 and pos[3] < pos[2]:  
            print(2)  
            print(32)  
            return  
        if cnt[5] > 0:  
            print(2)  
            if pos[5] < pos[2]:  
                print(52)  
            else:  
                print(25)  
            return  
        if cnt[7] > 0:  
            print(2)  
            if pos[7] < pos[2]:  
                print(72)  
            else:  
                print(27)  
            return  
      
    if cnt[5] > 0:  
        if cnt[3] > 0 and pos[3] < pos[5]:  
            print(2)  
            print(35)  
            return  
        if cnt[7] > 0:  
            print(2)  
            if pos[7] < pos[5]:  
                print(75)  
            else:  
                print(57)  
            return  
  
# Main function  
if __name__ == "__main__":  
    t = int(input())  
    for _ in range(t):  
        solve()  

运行结果:

7
3
237
2
27
5
44444
1
4
3
221
1
1
2
35
2
35
3
773
2
77
1
4
1
4
30
626221626221626221626221626221
1
1

  • 16
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值