spoj的第二题令人太不爽了......

spoj的第二题就是求a,b直接的所有质数并且输出.这个很简单,但是1<=a,b<=100000000就太欠扁了.后来我解决了这个问题,结果最后tle.因为每组最多有十组数据.谁能指导指导呢?感谢啊!

题库在第一个超链接

题目链接

 

接下来就是我的程序啦,大家帮忙看一看啊,谢啦:

Program Prime_Generator; Const MaxPrimeNum = 9592; Var Prime : Array[1..MaxPrimeNum]Of LongInt; n,m : Int64; cases : LongInt; casei : LongInt; Procedure Solve; Var start,finish,i,j : LongInt; p : Int64; tail : LongInt; IsPrime : Boolean; r,s,t : Real; Begin FillChar(Prime,SizeOf(Prime),0); Readln(n,m); start:=1; finish:=1+(m-n); For i:=start To finish Do Begin p:=i+n-1; IsPrime:=True; If p=1 Then Continue; If p=2 Then Begin Prime[1]:=i; Writeln(p); Break; End; For j:=2 To Trunc(Sqrt(i))+1 Do Begin r:=p; s:=j; t:=r/s; If t=Trunc(t) Then Begin IsPrime:=False; Break; End; End; If IsPrime Then Begin Prime[1]:=i; Writeln(p); Break; End; End; If Prime[1]=0 Then Begin Writeln; Exit; End; tail:=1; For i:=Prime[1] To finish Do Begin p:=i+n-1; If p=1 Then Continue; IsPrime:=True; For j:=1 To tail Do Begin IsPrime:=True; r:=p; s:=Prime[j]+n-1; t:=r/s; If r/2=Trunc(r/2) Then Begin IsPrime:=False; Break; End; If t=Trunc(t) Then Begin IsPrime:=False; Break; End; End; If IsPrime Then Begin Inc(tail); Prime[tail]:=i; Writeln(p); End; End; End; { Solve } Begin Readln(cases); For casei:=1 To cases-1 Do Begin Solve; Writeln; End; Solve; End. { Discripiton: Name:Prime_Generator Code:2 PRIME1 OJ:spoj Lang:Pascal Website: https://www.spoj.pl/problems/PRIME1/ }  

2010-10-03-18:19:50

你可能感兴趣的:(ACM/OI)