SGU 154 — Factorial

Posted on 五月 25th, 2008.

很简单的二分答案,不过要注意一点,题目所说的自然数不包括0(也就是说,如果输入是0,需要输出1.这里WA了2次).

{
SGU 154; Factorial
- sqybi’s code
- 二分答案
}
//for my winsty
program sgu154_sqybi;
  var
    n, l, r, mid, i, t, ans: int64;

  begin
    readln(n);
    ans := 0;
    l := 1;
    r := n * 5;
    while l <= r do begin
      mid := (l + r) div 2;
      i := 5;
      t := 0;
      while i <= mid do begin
        t := t + mid div i;
        i := i * 5;
      end;
      if t = n then ans := mid;
      if t >= n then
        r := mid - 1
      else
        l := mid + 1;
    end;
    if n = 0 then
      writeln(1)
    else if ans = 0 then
      writeln(’No solution’)
    else
      writeln(ans);
  end.

阅读(155 次)

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...