結果

問題 No.3416 マッチ棒パズル Extra
コンテスト
ユーザー hos.lyric
提出日時 2025-12-23 00:38:34
言語 C++14
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 2,996 ms / 3,000 ms
コード長 5,367 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,087 ms
コンパイル使用メモリ 115,872 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-12-23 00:39:20
合計ジャッジ時間 45,578 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘__int128 unsigned inUInt128()’:
main.cpp:58:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |   scanf("%s", buf);
      |   ~~~~~^~~~~~~~~~~
main.cpp: In function ‘__int128 inInt128()’:
main.cpp:63:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   63 |   scanf("%s", buf);
      |   ~~~~~^~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:165:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  165 |     scanf("%llu", &N);
      |     ~~~~~^~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


#ifndef LIBRA_OTHER_INT128_H_
#define LIBRA_OTHER_INT128_H_

#include <stdio.h>
#include <iostream>

constexpr unsigned __int128 toUInt128(const char *s) {
  unsigned __int128 x = 0;
  for (; *s; ++s) x = x * 10 + (*s - '0');
  return x;
}
constexpr __int128 toInt128(const char *s) {
  if (*s == '-') return -toInt128(s + 1);
  __int128 x = 0;
  for (; *s; ++s) x = x * 10 + (*s - '0');
  return x;
}
unsigned __int128 inUInt128() {
  static char buf[41];
  scanf("%s", buf);
  return toUInt128(buf);
}
__int128 inInt128() {
  static char buf[41];
  scanf("%s", buf);
  return toInt128(buf);
}

void out(unsigned __int128 x) {
  static char buf[41];
  int len = 0;
  do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
  for (int i = len; --i >= 0; ) putchar(buf[i]);
}
void out(__int128 x) {
  if (x < 0) {
    putchar('-');
    out(-static_cast<unsigned __int128>(x));
  } else {
    out(static_cast<unsigned __int128>(x));
  }
}
std::ostream &operator<<(std::ostream &os, unsigned __int128 x) {
  static char buf[41];
  int len = 0;
  do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
  for (int i = len; --i >= 0; ) os << buf[i];
  return os;
}
std::ostream &operator<<(std::ostream &os, __int128 x) {
  if (x < 0) {
    os << '-' << -static_cast<unsigned __int128>(x);
  } else {
    os << static_cast<unsigned __int128>(x);
  }
  return os;
}

#endif  // LIBRA_OTHER_INT128_H_


__int128 solve(unsigned long long N) {
  if (N == 0) return 0;
  // const unsigned long long N2 = sqrt(static_cast<long double>(N));
  unsigned long long M2 = sqrtl(N);
  for (; (unsigned __int128)2*M2*M2 + M2 + M2 < N; ++M2) {}
  for (; (unsigned __int128)2*M2*M2 + M2 + M2 > N; --M2) {}
  unsigned long long N3 = cbrt(static_cast<long double>(N));
  for (; static_cast<unsigned __int128>(N3) * N3 * N3 < N; ++N3) {}
  for (; static_cast<unsigned __int128>(N3) * N3 * N3 > N; --N3) {}
  unsigned long long M3 = N3;  // ?
  __int128 sum = 0;
  // \sum[1<=y<=floor(N^(1/2))] floor(N/y)
  using Pt = pair<unsigned long long, unsigned long long>;
  // { (x, y) | x y > N }: convex region
  auto inside = [&](unsigned long long x, unsigned long long y) -> bool {
    // return (static_cast<unsigned __int128>(x) * y > N);
    // return ((unsigned __int128)2*x*y + x + y > N);
    return (2*x*y + x + y > N);  // ?
  };
  // -(slope at x) <= p.y/p.x
  auto steep = [&](unsigned long long x, const Pt &p) -> bool {
    // return (static_cast<unsigned __int128>(N) * p.first <= static_cast<unsigned __int128>(p.second) * x * x);
    // (2N+1) / (2x+1)^2 <= p.y/p.x
    return ((unsigned __int128)(2*N + 1) * p.first <= (unsigned __int128)p.second * (2*x + 1) * (2*x + 1));
  };
  vector<Pt> stack{{1, 0}, {1, 1}};
  // (x, y): inside
  // unsigned long long x = N / N2, y = N2 + 1;
  unsigned long long x = (N - M2) / (2*M2 + 1), y = M2 + 1;
  assert(inside(x, y));
  for (; ; ) {
    Pt l = stack.back();
    stack.pop_back();
    for (; inside(x + l.first, y - l.second); x += l.first, y -= l.second) {
      // (x, y) -> (x + l.x, y - l.y)
      // sum for [y - l.y, y)
      sum += x * l.second + (l.first - 1) * (l.second + 1) / 2;
    }
    // if (y <= N3) break;
    if (y <= M3) break;
    Pt r = l;
    for (; l = stack.back(), !inside(x + l.first, y - l.second); stack.pop_back()) r = l;
    // (x, y) + l: inside, (x, y) + r: outside
    for (; ; ) {
      const Pt m(l.first + r.first, l.second + r.second);
      if (inside(x + m.first, y - m.second)) {
        stack.push_back(l = m);
      } else {
        if (steep(x + m.first, l)) break;
        r = m;
      }
    }
  }
  // for (; --y; ) sum += N / y;
  for (; --y; ) sum += (N - y) / (2*y + 1);
  // return 2 * sum - N2 * N2;
  return 2 * sum - M2 * M2;
}

////////////////////////////////////////////////////////////////////////////////


int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    unsigned long long N;
    scanf("%llu", &N);
    const __int128 ans = solve(N);
    out(ans % 998244353);
    puts("");
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}
0