結果

問題 No.3136 F,B in FizzBuzzString16
ユーザー Kude
提出日時 2025-05-02 22:48:17
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 3,074 bytes
コンパイル時間 3,616 ms
コンパイル使用メモリ 300,368 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-02 22:48:22
合計ジャッジ時間 4,857 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 7
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

template <class T>
struct cntsum {
  T a, b;  // a+bx mod x^2
  friend cntsum operator+(const cntsum& lhs, const cntsum& rhs) { return {lhs.a + rhs.a, lhs.b + rhs.b}; }
  friend cntsum operator-(const cntsum& lhs, const cntsum& rhs) { return {lhs.a - rhs.a, lhs.b - rhs.b}; }
  friend cntsum operator*(const cntsum& lhs, const cntsum& rhs) { return {lhs.a * rhs.a, lhs.a * rhs.b + lhs.b * rhs.a}; }
  friend cntsum operator*(const T& c, const cntsum& x) { return {x.a, x.b + c * x.a}; }
  cntsum& operator+=(const cntsum& x) { return *this = *this + x; }
  cntsum& operator-=(const cntsum& x) { return *this = *this - x; }
  cntsum& operator*=(const cntsum& x) { return *this = *this * x; }
};
using CS = cntsum<ll>;

ll h(ll x) {
  // [1, x]
  ll ans = 0;
  ans += x / 3 + x / 5;
  x++;
  // [0, x)
  assert(x < 1LL << 40);
  CS now{1, 0};
  int r_now = 0;
  CS dp[15]{};
  rrep(k, 10) {
    int xk = x >> (4 * k) & 15;
    CS ndp[15]{};
    rep(r, 15) rep(v, 16) {
      ndp[(r * 16 + v) % 15] += (v == 0xB || v == 0xF) * dp[r];
    }
    rep(v, xk) ndp[(r_now * 16 + v) % 15] += (v == 0xB || v == 0xF) * now;
    r_now = (r_now * 16 + xk) % 15;
    if (xk == 0xB || xk == 0xF) now = 1 * now;
    copy(all(ndp), dp);
  }
  rep(r, 15) if (r % 3 && r % 5) ans += dp[r].b;
  return ans;
}

ll g(ll x) {
  // [1, x]
  int k = 0;
  ll k16 = 1;
  ll res = 0;
  while (k16 <= x) {
    ll ub = min(16 * k16 - 1, x);
    ll lb = k16 - 1;
    // (lb, ub]
    ll c15 = ub / 15 - lb / 15;
    ll c3 = ub / 3 - lb / 3;
    ll c5 = ub / 5 - lb / 5;
    res += (k + 1) * (ub - lb - c3 - c5 + c15);
    res += (c3 + c5) * 4;
    k16 *= 16;
    k++;
  }
  return res;
}

ll f(ll x) {
  ll l = 0, r = 1;
  while (g(r) <= x) l = r, r *= 2;
  while (r - l > 1) {
    ll c = (l + r) / 2;
    (g(c) <= x ? l : r) = c;
  }
  ll ans = h(l);
  string rs;
  if (r % 3 && r % 5) {
    ll tmp = r;
    while (tmp) {
      int v = tmp % 16;
      tmp /= 16;
      rs += v < 10 ? '0' + v : 'A' + (v - 10);
    }
    reverse(all(rs));
  } else {
    if (r % 3 == 0) rs += "Fizz";
    if (r % 5 == 0) rs += "Buzz";
  }
  rs = rs.substr(0, x - g(l));
  ans += count(all(rs), 'F');
  ans += count(all(rs), 'B');
  return ans;
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll x, y;
  cin >> x >> y;
  x--;
  ll ans = f(y) - f(x);
  cout << ans << '\n';
}
0