OJ Primed Subsequence

原题链接:传送锚点

1.题目

Description

        Given a sequence of positive integers of length n, we define a primed subsequence as a consecutive subsequence of length at least two that sums to a prime number greater than or equal to two.
        For example, given the sequence:
                                                                3 5 6 3 8

        There are two primed subsequences of length 2 (5 + 6 = 11 and 3 + 8 = 11), one primed subsequence of length 3 (6 + 3 + 8 = 17), and one primed subsequence of length 4 (3 + 5 + 6 + 3 = 17).


Input
        Input consists of a series of test cases. The first line consists of an integer t (1 < t < 21), the number of test cases. Each test case consists of one line. The line begins with the integer n, 0 < n < 10001, followed by n non-negative numbers less than 10000 comprising the sequence. You should note that 80% of the test cases will have at most 1000 numbers in the sequence.


Output
        For each sequence, print the ‘Shortest primed subsequence is length x:’, where x is the length of the shortest primed subsequence, followed by the shortest primed subsequence, separated by spaces. If there are multiple such sequences, print the one that occurs first. If there are no such sequences, print ‘This sequence is anti-primed.’.


Sample Input

3
5 3 5 6 3 8
5 6 4 5 4 12
21 15 17 16 32 28 22 26 30 34 29 31 20 24 18 33 35 25 27 23 19 21


Sample Output

Shortest primed subsequence is length 2: 5 6
Shortest primed subsequence is length 3: 4 5 4
This sequence is anti-primed.

2.中文翻译

题目描述

给定长度为n的正整数序列,我们将素数子序列定义为长度至少为2的连续子序列,而且其和为大于或等于2的素数。

例如,给定序列:

3 5 6 3 8

有两个长度为2(5+6+11和3+8=11)的素数子序列,一个长度为3(6+3+8=17)的素数子序列,和一个长度4(3+5+6+3=17)的素数子序列。

输入

        输入由一系列测试用例组成。第一行包含一个整数t(1<t<21),即测试用例的数量。每个测试用例由一行组成。该行以整数n开始,0<n<10001,后跟n个小于10000的非负数,构成序列。您应该注意,80%的测试用例在序列中最多有1000个数字。

输出

        对于每个序列,打印 ‘Shortest primed subsequence is length x:’ ,其中x是最短素数子序列的长度,后面是最短的素数子序列,用空格分隔。如果有多个这样的序列,请打印最先出现的序列。如果没有这样的序列,请打印 ‘This sequence is anti-primed.’ 。

样例输入

3
5 3 5 6 3 8
5 6 4 5 4 12
21 15 17 16 32 28 22 26 30 34 29 31 20 24 18 33 35 25 27 23 19 21

样例输出

Shortest primed subsequence is length 2: 5 6
Shortest primed subsequence is length 3: 4 5 4
This sequence is anti-primed.

3.思路

        AC办法: 欧拉筛:传送锚点

        一般办法:素数判定用埃氏就够了

4.代码

import math
#素数判定
def isprime(num):
    if num<2:
        return False
    else:
        for i in range(2,int(math.sqrt(num))+1):
            if num%i==0:
                return False
        return True

#计算最短序列长度并给出第一个出现的最短序列
def shortest_subsequense(seq):
    shortest = float("inf")
    result=[]
    for i in range(len(seq)):
        sum1=seq[i]
        for j in range(i+1,len(seq)):
            sum1 += seq[j]
            subseq=seq[i:j+1]
            if sum1>=2 and isprime(sum1) and len(subseq)<shortest:
                shortest=len(subseq)
                result=subseq
    return (shortest,result)

#主程序
t = int(input())
for i in range(t):
    n,*seq=map(int,input().split())
    (shortestlen,result) =shortest_subsequense(seq)
    if shortestlen == float("inf"):
        print("This sequence is anti-primed.")
    else:
        print("Shortest primed subsequence is length {}: {}".format(shortestlen," ".join(map(str,result))))

5.知识点

        5.1 join方法的使用

a=[1,2,3,4,5]
print("连接符".join(map(str,a)))

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值