Python3全MLE咋办,求指导

P3383 【模板】线性筛素数

MC_xjhjdA @ 2024-08-18 11:33:27

def oss(n):
    z=[True for _ in range(n+1)]
    p=2
    while (p*p<=n):
        if (z[p]==True):
            for i in range(p*p,n+1,p):
                z[i]=False
        p+=1
    zl=[]
    for p in range(2,n+1):
        if z[p]:
            zl.append(p)
    return zl
n,q=map(int,input().split())
l=oss(n)
o=[]
for _ in range(q):
    o.append(l[int(input())-1])
for x in range(q):
    print(o[x])

by MC_xjhjdA @ 2024-08-18 12:03:37

@_zhang 不打只是做来玩玩


by MC_xjhjdA @ 2024-08-18 12:07:48

@AK_hai 哎raw input好用的


by _zhang @ 2024-08-18 12:08:27

@MC_xjhjdA 不过相信我,你的代码解决了MLE一定有TLE(


by MC_xjhjdA @ 2024-08-18 12:11:18

@_zhang Python3只有一个人AC了XD


by MC_xjhjdA @ 2024-08-23 09:32:11

@_zhang 这回改了

def oss(n):
    status=[True]*(n+1)
    primes=[]
    for x in range(2,n+1):
        if status[x]:
            primes.append(x)
        for y in primes:
            if y*x>n:
                break
            status[y*x]=False
            if x%y==0:
                break
    return primes
n,q=map(int,input().split())
l=oss(n)
o=[]
for _ in range(q):
    o.append(l[int(input())-1])
for x in range(q):
    print(o[x])

by MC_xjhjdA @ 2024-08-23 09:32:41

@_zhang 但是还是MLE


by _zhang @ 2024-08-23 10:15:56

@MC_xjhjdA 啊lz你还没放弃啊,勇气可嘉


by _zhang @ 2024-08-23 10:23:00

@MC_xjhjdA

for _ in range(q):
    o.append(l[int(input())-1])
for x in range(q):
    print(o[x])

最后四行优化一下试试?

for _ in range(q):
  print(l[int(input()) - 1])

再不打打Python就不会写了


by MC_xjhjdA @ 2024-08-23 10:35:25

@_zhang luogu能边输入边输出吗,我试试


by MC_xjhjdA @ 2024-08-23 10:36:34

@_zhang 还是寄了XD


上一页 | 下一页