比赛 20101101 评测结果 AAAAAAAATT
题目名称 整数合并 最终得分 80
用户昵称 FrCsKOH 运行时间 2.665 s
代码语言 Pascal 内存使用 0.96 MiB
提交时间 2012-11-05 09:33:28
显示代码纯文本
program C0487;
 var S:array[1..9592] of longint;
     f,cup:array[1..100000] of longint;
     a,b,p,i,j,t,x,y,ans:longint;

 function father(x:longint):longint;
  begin
   if f[x]=x then exit(x);
   f[x]:=father(f[x]);
   father:=f[x];
  end;

 function check(s:longint):boolean;
  var i:longint;
  begin
   if s=1 then exit(false);
   if s=2 then exit(true);

   for i:=2 to trunc(sqrt(s)) do
    if s mod i=0 then exit(false);
   check:=true;
  end;

 function checkPr(u,v:longint):boolean;
  var i:longint;
  begin
   for i:=1 to t do
    if (u mod S[i]=0) and (v mod S[i]=0) then exit(true);

   checkPr:=false;
  end;

 begin
  assign(input,'setb.in');
  reset(input);
  assign(output,'setb.out');
  rewrite(output);

  readln(a,b,p);
  t:=0;

  for i:=p to b do
   if check(i) then begin
    inc(t);
    S[t]:=i;
   end;

  for i:=a to b do f[i]:=i;

  for i:=a to b-1 do
   for j:=i+1 to b do begin
    x:=father(i);
    y:=father(j);
    if x<>y then
     if checkPr(i,j) then f[x]:=y;
   end;

  for i:=a to b do f[i]:=father(f[i]);


  fillchar(cup,sizeof(cup),0);
  for i:=a to b do inc(cup[f[i]]);

  ans:=0;
  for i:=a to b do
   if cup[i]<>0 then inc(ans);
  writeln(ans);

  close(input);
  close(output);
 end.