結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー te-shte-sh
提出日時 2017-05-09 15:57:10
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,110 bytes
コンパイル時間 1,032 ms
コンパイル使用メモリ 100,756 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 13:14:06
合計ジャッジ時間 3,180 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags

const maxK = 39;
alias Tuple!(long, long) AB;

void main()
{
  auto xi = readln.split.to!(long[]);
  xi = xi.sort().uniq.array;

  auto fi = new long[](maxK);
  fi[0] = fi[1] = 1;
  foreach (i; 2..maxK)
    fi[i] = fi[i-1] + fi[i-2];

  if (xi.length == 1) {
    auto r = calc1(xi[0], fi);
    writeln(r[0], " ", r[1]);
  } else {
    auto r = calc2(xi[0], xi[1], xi.length == 3 ? xi[2] : -1, fi);
    if (r[0] == -1)
      writeln(-1);
    else
      writeln(r[0], " ", r[1]);
  }
}

auto calc1(long x, long[] fi)
{
  if (x == 1)
    return AB(1, 1);

  auto r = x - 1;
  foreach (i; 3..maxK) {
    auto t = x - fi[i-2];
    if (t <= 0 || t % fi[i-1]) continue;
    r = min(r, t / fi[i-1]);
  }

  return AB(1, r);
}

auto calc2(long x1, long x2, long x3, long[] fi)
{
  AB[] r = [];

  if (check(2, x1, x2, x3, fi)) r ~= AB(x1, x2);
  if (check(2, x2, x1, x3, fi)) r ~= AB(x2, x1);
  
  foreach (j; 2..maxK) {
    auto t = x2 - x1 * fi[j-2];
    if (t <= 0 || t % fi[j-1]) continue;
    auto b = t / fi[j-1];
    if (check(j+1, x1, b, x3, fi)) r ~= AB(x1, b);
  }

  foreach (j; 2..maxK) {
    auto t = x2 - x1 * fi[j-1];
    if (t <= 0 || t % fi[j-2]) continue;
    auto a = t / fi[j-2];
    if (check(j+1, a, x1, x3, fi)) r ~= AB(a, x1);
  }

  foreach (i; 2..maxK-1)
    foreach (j; i+1..maxK) {
      auto t = fi[j-2] * fi[i-1] - fi[i-2] * fi[j-1];
      auto x = x2 * fi[i-1] - x1 * fi[j-1];
      if (t < 0) {
        t = -t;
        x = -x;
      }
      if (x <= 0 || x % t) continue;
      auto a = x / t;

      auto u = x1 - a * fi[i-2];
      if (u <= 0 || u % fi[i-1]) continue;
      auto b = u / fi[i-1];

      if (check(j+1, a, b, x3, fi)) r ~= AB(a, b);
    }

  if (r.empty)
    return AB(-1, -1);
  else
    return r.sort!"a[0] == b[0] ? a[1] < b[1] : a[0] < b[0]".front;
}

auto check(size_t minK, long a, long b, long x3, long[] fi)
{
  if (x3 < 0) return true;

  foreach (k; minK..maxK)
    if (x3 == a * fi[k-2] + b * fi[k-1])
      return true;

  return false;
}
0