SGU 112 — a^b-b^a

Posted on 五月 9th, 2008.

高精度,求a^b-b^a.

直接发程序.

{
SGU 112; a^b-b^a
- sqybi’s code
}
//for my winsty
program sgu112_sqybi;
  const
    ll = 100;
    rr = 10000;

  type
    arr = array[1..ll+1]of longint;

  var
    a, b, i: longint;
    t1, r1, t2, r2, t: arr;

  procedure mult(a, b: arr; var c: arr);
    var
      i, j: longint;
    begin
      fillchar(c, sizeof(c), 0);
      for i:=1 to ll do
        for j:=1 to ll-i do begin
          c[i+j-1] := c[i+j-1] + a[i] * b[j];
          c[i+j] := c[i+j-1] div rr;
          c[i+j-1] := c[i+j-1] mod rr;
        end;
    end;

  procedure minus(a, b: arr; var c: arr);
    var
      i: longint;
    begin
      fillchar(c, sizeof(c), 0);
      for i:=1 to ll do begin
        if a[i] < b[i] then begin
          a[i] := a[i] + rr;
          a[i+1] := a[i+1] - 1;
        end;
        c[i] := a[i] - b[i];
      end;
    end;

  function bigger(a, b: arr): boolean;
    var
      i: longint;
    begin
      bigger := true;
      for i:=ll downto 1 do
        if a[i] <> b[i] then begin
          bigger := a[i] > b[i];
          exit;
        end;
    end;

  procedure print(a: arr);
    var
      l, i: longint;
    begin
      l := ll;
      while (l > 1) and (a[l] = 0) do dec(l);
      write(a[l]);
      for i:=l-1 downto 1 do begin
        if a[i] < 1000 then write(0);
        if a[i] < 100 then write(0);
        if a[i] < 10 then write(0);
        write(a[i]);
      end;
      writeln;
    end;

  begin
    readln(a, b);
    if a = b then begin
      writeln(0);
      halt;
    end;
    t1[1] := 1;
    r1[1] := a;
    for i:=1 to b do mult(t1, r1, t1);
    t2[1] := 1;
    r2[1] := b;
    for i:=1 to a do mult(t2, r2, t2);
    if bigger(t1, t2) then
      minus(t1, t2, t)
    else begin
      write(’-');
      minus(t2, t1, t);
    end;
    print(t);
  end.

阅读(60 次)

Make a Comment

Make A Comment: ( None so far )

blockquote and a tags work here.

Liked it here?
Why not try sites on the blogroll...