結果

問題 No.1501 酔歩
ユーザー noshi91noshi91
提出日時 2021-05-07 22:32:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 410 ms / 2,000 ms
コード長 2,587 bytes
コンパイル時間 9,003 ms
コンパイル使用メモリ 410,244 KB
実行使用メモリ 54,836 KB
最終ジャッジ日時 2023-10-13 13:49:56
合計ジャッジ時間 23,551 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 300 ms
37,000 KB
testcase_06 AC 227 ms
27,520 KB
testcase_07 AC 294 ms
36,700 KB
testcase_08 AC 72 ms
11,404 KB
testcase_09 AC 259 ms
33,420 KB
testcase_10 AC 100 ms
13,884 KB
testcase_11 AC 223 ms
31,728 KB
testcase_12 AC 164 ms
23,996 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 390 ms
42,920 KB
testcase_23 AC 382 ms
45,992 KB
testcase_24 AC 386 ms
42,828 KB
testcase_25 AC 410 ms
45,272 KB
testcase_26 AC 389 ms
50,860 KB
testcase_27 AC 386 ms
43,368 KB
testcase_28 AC 388 ms
49,276 KB
testcase_29 AC 387 ms
49,752 KB
testcase_30 AC 388 ms
54,820 KB
testcase_31 AC 387 ms
49,496 KB
testcase_32 AC 379 ms
47,324 KB
testcase_33 AC 385 ms
46,424 KB
testcase_34 AC 378 ms
42,632 KB
testcase_35 AC 379 ms
45,104 KB
testcase_36 AC 392 ms
44,360 KB
testcase_37 AC 388 ms
51,340 KB
testcase_38 AC 374 ms
47,952 KB
testcase_39 AC 371 ms
52,972 KB
testcase_40 AC 369 ms
47,172 KB
testcase_41 AC 393 ms
46,616 KB
testcase_42 AC 341 ms
52,196 KB
testcase_43 AC 1 ms
4,348 KB
testcase_44 AC 2 ms
4,352 KB
testcase_45 AC 339 ms
42,832 KB
testcase_46 AC 353 ms
48,952 KB
testcase_47 AC 335 ms
54,836 KB
testcase_48 AC 364 ms
49,828 KB
testcase_49 AC 356 ms
47,612 KB
testcase_50 AC 365 ms
50,064 KB
testcase_51 AC 371 ms
50,016 KB
testcase_52 AC 371 ms
51,088 KB
testcase_53 AC 1 ms
4,348 KB
testcase_54 AC 1 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <boost/integer/common_factor.hpp>
#include <boost/multiprecision/cpp_int.hpp>

#include <array>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <queue>

using u64 = boost::multiprecision::cpp_int;

struct rational {
  u64 num;
  u64 den;

  rational() : num(0), den(1) {}

  rational(u64 n, u64 d) : num(n), den(d) {
    u64 g = boost::integer::gcd(n, d);
    num /= g;
    den /= g;
  }

  friend rational operator+(const rational &l, const rational &r) {
    return {l.num * r.den + r.num * l.den, l.den * r.den};
  }

  friend rational operator-(const rational &l, const rational &r) {
    return {l.num * r.den - r.num * l.den, l.den * r.den};
  }

  friend rational operator*(const rational &l, const rational &r) {
    return {l.num * r.num, l.den * r.den};
  }

  friend rational operator/(const rational &l, const rational &r) {
    return {l.num * r.den, l.den * r.num};
  }
};

using gate = std::array<std::array<rational, 2>, 2>;

gate concat(const gate &l, const gate &r) {
  gate res = {};
  res[0][1] =
      (rational(1, 1) - l[0][0]) * r[0][1] / (r[0][0] * l[1][0] + r[0][1]);
  res[0][0] = rational(1, 1) - res[0][1];
  res[1][0] =
      (rational(1, 1) - r[1][1]) * l[1][0] / (l[1][1] * r[0][1] + l[1][0]);
  res[1][1] = rational(1, 1) - res[1][0];
  return res;
}

int main() {
  int n, k;
  std::cin >> n >> k;
  if (n == k) {
    std::cout << "1/1\n";
    return 0;
  }
  if (k == 1) {
    std::cout << "0\n";
    return 0;
  }
  k -= 2;

  std::vector<int> a(n);
  for (auto &e : a) {
    std::cin >> e;
  }
  std::vector<gate> g(n - 2);
  for (int i = 0; i != n - 2; i += 1) {
    g[i][0][0] = g[i][1][0] = rational(a[i], a[i] + a[i + 2]);
    g[i][0][1] = g[i][1][1] = rational(a[i + 2], a[i] + a[i + 2]);
  }

  const auto aggregate = [&](const int l, const int r) {
    std::queue<gate> que;
    {
      gate t;
      t[0][0] = t[1][1] = rational(0, 1);
      t[0][1] = t[1][0] = rational(1, 1);
      que.push(t);
    }
    for (int i = l; i != r; i += 1) {
      que.push(g[i]);
    }
    while (que.size() != 1) {
      std::queue<gate> next;
      while (que.size() >= 2) {
        gate x = que.front();
        que.pop();
        gate y = que.front();
        que.pop();
        next.push(concat(x, y));
      }
      if (!que.empty()) {
        next.push(que.front());
      }
      que = std::move(next);
    }
    return que.front();
  };

  gate l = aggregate(0, k);
  gate r = aggregate(k, n - 2);

  rational ans = r[0][1] / (r[0][1] + r[0][0] * l[1][0]);

  std::cout << ans.num << "/" << ans.den << "\n";

  return 0;
}
0