BestCoder Round #54

传送门

http://acm.hdu.edu.cn/showproblem.php?pid=5427
http://acm.hdu.edu.cn/showproblem.php?pid=5428
http://acm.hdu.edu.cn/showproblem.php?pid=5429
http://acm.hdu.edu.cn/showproblem.php?pid=5430
http://acm.hdu.edu.cn/showproblem.php?pid=5431

T1 A problem of sorting

模拟,没什么说的

var
 b:array[0..100]of string;
 a:array[0..100]of longint;
 i,j,k:longint;
 t,n:longint;
procedure sort(l,r:longint);
var i,j,x,y:longint; z:string;
begin
 i:=l; j:=r; x:=a[(l+r) div 2];
 repeat
  while a[i]<x do inc(i);
  while x<a[j] do dec(j);
  if not(i>j) then
   begin
    y:=a[i]; a[i]:=a[j]; a[j]:=y;
    z:=b[i]; b[i]:=b[j]; b[j]:=z;
    inc(i); dec(j);
   end;
 until i>j;
 if l<j then sort(l,j);
 if i<r then sort(i,r);
end;

begin
 readln(t);
 for j:=1 to t do
  begin
   readln(n);
   for i:=1 to n do
    begin
     readln(b[i]);
     val(copy(b[i],length(b[i])-3,4),a[i]);
     b[i]:=copy(b[i],1,length(b[i])-5);
    end;
   sort(1,n);
   for i:=n downto 1 do
    writeln(b[i]);
  end;
end.

T2 The Factor

题目大意

n(n<100),a[i]<2109,a[i]

题解

任意一个合数可以写成若干个质数的乘积,所以最小因子的两个因子应为质数,我们对a[i]分解成若干质数,取所有质数中最小的两个乘积即可,不足两个即为无解,输出-1

var
 sum,prime:array[0..100000]of longint;
 check:array[0..100000]of boolean;
 a:array[0..100]of longint;
 i,j,k:longint;
 n,t,len,ans:longint;
procedure prepare;
begin
 for i:=2 to 100000 do
  begin
   if check[i]=false then begin inc(len); prime[len]:=i; end;
   for j:=1 to len do
    begin
     if i*prime[j]>100000 then break;
     check[i*prime[j]]:=true;
     if i mod prime[j]=0 then break;
    end;
  end;
end;

procedure apart(x:longint);
var i:longint;
begin
 for i:=1 to len do
  begin
   if x=1 then break;
   while x mod prime[i]=0 do
    begin inc(sum[i]); x:=x div prime[i]; end;
  end;
end;

begin
 prepare;
 readln(t);
 for k:=1 to t do
  begin
   readln(n);
   fillchar(sum,sizeof(sum),0);
   for i:=1 to n do
    begin read(a[i]); apart(a[i]); end;
   j:=2; ans:=1;
   for i:=1 to len do
    while sum[i]<>0 do
     if j=0 then break
     else begin ans:=ans*prime[i]; dec(j); dec(sum[i]); end;
   if j=0 then writeln(ans) else writeln(-1);
  end;
end.

你可能感兴趣的:(BestCoder Round #54)