結果

問題 No.2262 Fractions
ユーザー 👑 emthrmemthrm
提出日時 2023-04-08 03:26:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 496 ms / 2,000 ms
コード長 2,286 bytes
コンパイル時間 3,168 ms
コンパイル使用メモリ 245,488 KB
実行使用メモリ 5,820 KB
最終ジャッジ日時 2023-08-18 00:39:01
合計ジャッジ時間 18,823 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 278 ms
5,012 KB
testcase_01 AC 60 ms
4,380 KB
testcase_02 AC 60 ms
4,380 KB
testcase_03 AC 61 ms
4,376 KB
testcase_04 AC 65 ms
4,376 KB
testcase_05 AC 61 ms
4,380 KB
testcase_06 AC 69 ms
4,380 KB
testcase_07 AC 66 ms
4,380 KB
testcase_08 AC 62 ms
4,376 KB
testcase_09 AC 68 ms
4,380 KB
testcase_10 AC 68 ms
4,376 KB
testcase_11 AC 225 ms
4,380 KB
testcase_12 AC 224 ms
4,376 KB
testcase_13 AC 227 ms
4,380 KB
testcase_14 AC 228 ms
4,376 KB
testcase_15 AC 225 ms
4,376 KB
testcase_16 AC 120 ms
4,380 KB
testcase_17 AC 119 ms
4,380 KB
testcase_18 AC 117 ms
4,376 KB
testcase_19 AC 385 ms
4,812 KB
testcase_20 AC 357 ms
4,916 KB
testcase_21 AC 393 ms
4,844 KB
testcase_22 AC 360 ms
4,656 KB
testcase_23 AC 298 ms
4,440 KB
testcase_24 AC 486 ms
5,664 KB
testcase_25 AC 479 ms
5,820 KB
testcase_26 AC 468 ms
5,768 KB
testcase_27 AC 483 ms
5,648 KB
testcase_28 AC 463 ms
5,580 KB
testcase_29 AC 480 ms
5,768 KB
testcase_30 AC 479 ms
5,808 KB
testcase_31 AC 487 ms
5,656 KB
testcase_32 AC 488 ms
5,708 KB
testcase_33 AC 496 ms
5,652 KB
testcase_34 AC 485 ms
5,708 KB
testcase_35 AC 492 ms
5,688 KB
testcase_36 AC 494 ms
5,796 KB
testcase_37 AC 16 ms
5,520 KB
testcase_38 AC 16 ms
5,500 KB
testcase_39 AC 395 ms
4,568 KB
testcase_40 AC 382 ms
4,596 KB
testcase_41 AC 389 ms
4,652 KB
testcase_42 AC 392 ms
4,716 KB
testcase_43 AC 378 ms
4,536 KB
testcase_44 AC 477 ms
5,684 KB
testcase_45 AC 481 ms
5,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {


  int t; cin >> t;
  while (t--) {
    int n; ll k; cin >> n >> k;
    const auto f = [&](const ll k) -> ll {
      // ll res = 0;
      // for (int num = 1; num <= n; ++num) {
      //   for (int den = 1; den <= n; ++den) {
      //     if (gcd(num, den) == 1 && num * n * n <= k * den) ++res;
      //   }
      // }
      // return res;
      vector<ll> g(n + 1, 0);
      for (int den = 1; den <= n; ++den) {
        g[den] += k * den / n / n;
        for (int i = den * 2; i <= n; i += den) {
          g[i] -= g[den];
        }
      }
      // cout << k << ": ";
      // REP(i, n + 1) cout << g[i] << " \n"[i == n];
      return reduce(ALL(g), 0LL);
    };
    const ll nmt1 = f(1LL * n * n);
    if (nmt1 == k) {
      cout << "1/1\n";
      continue;
    }
    if (k >= nmt1 * 2) {
      cout << "-1\n";
      continue;
    }
    bool k_is_large = false;
    if (nmt1 < k) {
      k = nmt1 * 2 - k;
      k_is_large = true;
    }
    ll lb = 0, ub = 1LL * n * n - 1;
    while (ub - lb > 1) {
      const ll mid = midpoint(lb, ub);
      (f(mid) >= k ? ub : lb) = mid;
    }
    for (int den = 1; den <= n; ++den) {
      const ll num = ub * den / n / n;
      if ((ub - 1) * den < num * n * n) {
        if (k_is_large) {
          cout << den << '/' << num << '\n';
        } else {
          cout << num << '/' << den << '\n';
        }
        break;
      }
    }
  }
  return 0;
}
0