結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-10 16:58:00
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,138 bytes
コンパイル時間 790 ms
コンパイル使用メモリ 115,604 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 21:55:30
合計ジャッジ時間 2,032 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) throw new EOFException; tokens = readln.split; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


immutable INF = 10^^9 + 10;

long[] X;

void main() {
  long[] P = [0, 1, 0];
  long[] Q = [0, 0, 1];
  for (; ; ) {
    const p = P[$ - 1] + P[$ - 2];
    const q = Q[$ - 1] + Q[$ - 2];
    if (p + q > INF) {
      break;
    }
    P ~= p;
    Q ~= q;
  }
  const len = cast(int)(P.length);
  debug {
    writefln("len = %s", len);
    writefln("P = %s", P);
    writefln("Q = %s", Q);
  }
  
  try {
    for (; ; ) {
      X = new long[3];
      foreach (h; 0 .. 3) {
        X[h] = readLong();
      }
      X = X.sort.uniq.array;
      auto ans = Tuple!(long, long)(INF, INF);
      if (X.length == 1) {
        // a = 1 ok
        if (X[0] == 1) {
          ans = tuple(1L, 1L);
        } else {
          foreach (i; 2 .. len) {
            /*
              P[i] + Q[i] b = X[0]
            */
            if ((X[0] - P[i]) % Q[i] == 0) {
              auto b = (X[0] - P[i]) / Q[i];
              if (b > 0) {
                chmin(ans, tuple!(long, long)(1L, b));
              }
            }
          }
        }
      } else {
        foreach (i; 1 .. len) foreach (j; i + 1 .. len) {
          /*
            P[i] a + Q[i] b = X[0]
            P[j] a + Q[j] b = X[1]
          */
          const dnm = P[i] * Q[j] - Q[i] * P[j];
          const nmrA = X[0] * Q[j] - Q[i] * X[1];
          const nmrB = P[i] * X[1] - X[0] * P[j];
          assert(dnm != 0);
          if (nmrA % dnm == 0 && nmrB % dnm == 0) {
            const a = nmrA / dnm;
            const b = nmrB / dnm;
            if (a > 0 && b > 0) {
              bool ok = true;
              foreach (h; 2 .. X.length) {
                bool found;
                foreach (k; 1 .. len) {
                  found = found || (P[k] * a + Q[k] * b == X[h]);
                }
                ok = ok && found;
              }
              if (ok) {
                chmin(ans, tuple!(long, long)(a, b));
              }
            }
          }
        }
      }
      if (ans >= tuple(INF, INF)) {
        writeln(-1);
      } else {
        writeln(ans[0], " ", ans[1]);
      }
    }
  } catch (EOFException e) {
  }
}
0