原题链接:传送锚点
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.
题目描述
给定长度为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 输出 对于每个序列,打印 ‘Shortest primed subsequence is length x:’ ,其中x是最短素数子序列的长度,后面是最短的素数子序列,用空格分隔。如果有多个这样的序列,请打印最先出现的序列。如果没有这样的序列,请打印 ‘This sequence is anti-primed.’ 。 样例输入 样例输出 AC办法: 欧拉筛:传送锚点 一般办法:素数判定用埃氏就够了 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.思路
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)
5.知识点
5.1 join方法的使用
a=[1,2,3,4,5]
print("连接符".join(map(str,a)))