比赛 20101101 评测结果 AAAAAAAATT
题目名称 整数合并 最终得分 80
用户昵称 digital-T 运行时间 4.461 s
代码语言 Pascal 内存使用 1.12 MiB
提交时间 2012-11-05 10:21:11
显示代码纯文本
var
w:boolean;
a,b,p,i,j,ans,q,qq:longint;
pp:array[2..1000]of boolean;
way:array[1..1000]of longint;
co:array[1..1000,1..1000]of boolean;
procedure find(x:longint);
var
k,y:longint;
begin
 for y:=a to b do
  if y<>x then
   if way[x]<>way[y] then
    if co[x,y] or co[y,x]then
     begin
      way[y]:=way[x];
      inc(ans);
      find(y);
     end;

end;
//____________________________________________
begin
assign(input,'setb.in');reset(input);
assign(output,'setb.out');rewrite(output);
read(a,b,p);
//______________________________
fillchar(pp,sizeof(pp),false);
pp[2]:=true;
pp[3]:=true;
pp[5]:=true;
for i:=6 to b do
 begin
 w:=true;
  for j:=2 to trunc(sqrt(i)) do
   begin
    if i mod j=0 then
     begin
      w:=false;
      break;
     end;
   end;
 pp[i]:=w;
 end;
//______________________________
fillchar(co,sizeof(co),false);
for i:=a to b do
 for j:=i+1 to b do
   for qq:=p to i do
    if pp[qq]then if (i mod qq=0)and(j mod qq=0)then
     begin
      co[i,j]:=true;
      break;
     end;

//______________________________
for i:=a to b do way[i]:=i;
ans:=0;
for i:=a to b do if way[i]=i then find(i);

write(b-a+1-ans);
close(input);
close(output);
end.