QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#766180#9225. Fibonacci FusionMisuki#TL 1670ms134096kbC++2019.4kb2024-11-20 16:29:462024-11-20 16:29:47

Judging History

你现在查看的是最新测评结果

  • [2024-11-20 16:29:47]
  • 评测
  • 测评结果:TL
  • 用时:1670ms
  • 内存:134096kb
  • [2024-11-20 16:29:46]
  • 提交

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>

//#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#define clock chrono::steady_clock::now().time_since_epoch().count()

using namespace std;

template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(size_t i = 0; T x : arr) {
    os << x;
    if (++i != N) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(size_t i = 0; T x : vec) {
    os << x;
    if (++i != size(vec)) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(size_t i = 0; T x : s) {
    os << x;
    if (++i != size(s)) os << ' ';
  }
  return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const map<T1, T2> &m) {
  for(size_t i = 0; pair<T1, T2> x : m) {
    os << x;
    if (++i != size(m)) os << ' ';
  }
  return os;
}

#ifdef DEBUG
#define dbg(...) cerr << '(', _do(#__VA_ARGS__), cerr << ") = ", _do2(__VA_ARGS__)
template<typename T> void _do(T &&x) { cerr << x; }
template<typename T, typename ...S> void _do(T &&x, S&&...y) { cerr << x << ", "; _do(y...); }
template<typename T> void _do2(T &&x) { cerr << x << endl; }
template<typename T, typename ...S> void _do2(T &&x, S&&...y) { cerr << x << ", "; _do2(y...); }
#else
#define dbg(...)
#endif

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
//#define double ldb

template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using max_heap = priority_queue<T>;

template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP = plus<T>>
void pSum(rng &&v) {
  if (!v.empty())
    for(T p = v[0]; T &x : v | views::drop(1))
      x = p = OP()(p, x);
}
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP>
void pSum(rng &&v, OP op) {
  if (!v.empty())
    for(T p = v[0]; T &x : v | views::drop(1))
      x = p = op(p, x);
}

template<ranges::forward_range rng>
void Unique(rng &v) {
  ranges::sort(v);
  v.resize(unique(v.begin(), v.end()) - v.begin());
}

template<ranges::random_access_range rng>
rng invPerm(rng p) {
  rng ret = p;
  for(int i = 0; i < ssize(p); i++)
    ret[p[i]] = i;
  return ret;
}

template<ranges::random_access_range rng, ranges::random_access_range rng2>
rng Permute(rng v, rng2 p) {
  rng ret = v;
  for(int i = 0; i < ssize(p); i++)
    ret[p[i]] = v[i];
  return ret;
}

template<bool directed>
vector<vector<int>> readGraph(int n, int m, int base) {
  vector<vector<int>> g(n);
  for(int i = 0; i < m; i++) {
    int u, v; cin >> u >> v;
    u -= base, v -= base;
    g[u].emplace_back(v);
    if constexpr (!directed)
      g[v].emplace_back(u);
  }
  return g;
}

template<class T>
void setBit(T &msk, int bit, bool x) {
  msk = (msk & ~(T(1) << bit)) | (T(x) << bit);
}
template<class T> void flipBit(T &msk, int bit) { msk ^= T(1) << bit; }
template<class T> bool getBit(T msk, int bit) { return msk >> bit & T(1); }

template<class T>
T floorDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? a / b : (a - b + 1) / b;
}
template<class T>
T ceilDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? (a + b - 1) / b : a / b;
}

template<class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

//reference: https://github.com/NyaanNyaan/library/blob/master/modint/montgomery-modint.hpp#L10
//note: mod should be a prime less than 2^30.

template<uint32_t mod>
struct MontgomeryModInt {
  using mint = MontgomeryModInt;
  using i32 = int32_t;
  using u32 = uint32_t;
  using u64 = uint64_t;

  static constexpr u32 get_r() {
    u32 res = 1, base = mod;
    for(i32 i = 0; i < 31; i++)
      res *= base, base *= base;
    return -res;
  }

  static constexpr u32 get_mod() {
    return mod;
  }

  static constexpr u32 n2 = -u64(mod) % mod; //2^64 % mod
  static constexpr u32 r = get_r(); //-P^{-1} % 2^32

  u32 a;

  static u32 reduce(const u64 &b) {
    return (b + u64(u32(b) * r) * mod) >> 32;
  }

  static u32 transform(const u64 &b) {
    return reduce(u64(b) * n2);
  }

  MontgomeryModInt() : a(0) {}
  MontgomeryModInt(const int64_t &b) 
    : a(transform(b % mod + mod)) {}

  mint pow(u64 k) const {
    mint res(1), base(*this);
    while(k) {
      if (k & 1) 
        res *= base;
      base *= base, k >>= 1;
    }
    return res;
  }

  mint inverse() const { return (*this).pow(mod - 2); }

  u32 get() const {
    u32 res = reduce(a);
    return res >= mod ? res - mod : res;
  }

  mint& operator+=(const mint &b) {
    if (i32(a += b.a - 2 * mod) < 0) a += 2 * mod;
    return *this;
  }

  mint& operator-=(const mint &b) {
    if (i32(a -= b.a) < 0) a += 2 * mod;
    return *this;
  }

  mint& operator*=(const mint &b) {
    a = reduce(u64(a) * b.a);
    return *this;
  }

  mint& operator/=(const mint &b) {
    a = reduce(u64(a) * b.inverse().a);
    return *this;
  }

  mint operator-() { return mint() - mint(*this); }
  bool operator==(mint b) const {
    return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
  }
  bool operator!=(mint b) const {
    return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
  }

  friend mint operator+(mint c, mint d) { return c += d; }
  friend mint operator-(mint c, mint d) { return c -= d; }
  friend mint operator*(mint c, mint d) { return c *= d; }
  friend mint operator/(mint c, mint d) { return c /= d; }

  friend ostream& operator<<(ostream& os, const mint& b) {
    return os << b.get();
  }
  friend istream& operator>>(istream& is, mint& b) {
    int64_t val;
    is >> val;
    b = mint(val);
    return is;
  }
};

using mint = MontgomeryModInt<998244353>;

//reference: https://judge.yosupo.jp/submission/69896
//remark: MOD = 2^K * C + 1, R is a primitive root modulo MOD
//remark: a.size() <= 2^K must be satisfied
//some common modulo: 998244353  = 2^23 * 119 + 1, R = 3
//                    469762049  = 2^26 * 7   + 1, R = 3
//                    1224736769 = 2^24 * 73  + 1, R = 3

template<int32_t k = 23, int32_t c = 119, int32_t r = 3, class Mint = MontgomeryModInt<998244353>>
struct NTT {

  using u32 = uint32_t;
  static constexpr u32 mod = (1 << k) * c + 1;
  static constexpr u32 get_mod() { return mod; }

  static void ntt(vector<Mint> &a, bool inverse) {
    static array<Mint, 30> w, w_inv;
    if (w[0] == 0) {
      Mint root = 2;
      while(root.pow((mod - 1) / 2) == 1) root += 1;
      for(int i = 0; i < 30; i++)
        w[i] = -(root.pow((mod - 1) >> (i + 2))), w_inv[i] = 1 / w[i];
    }
    int n = ssize(a);
    if (not inverse) {
      for(int m = n; m >>= 1; ) {
        Mint ww = 1;
        for(int s = 0, l = 0; s < n; s += 2 * m) {
          for(int i = s, j = s + m; i < s + m; i++, j++) {
            Mint x = a[i], y = a[j] * ww;
            a[i] = x + y, a[j] = x - y;
          }
          ww *= w[__builtin_ctz(++l)];
        }
      }
    } else {
      for(int m = 1; m < n; m *= 2) {
        Mint ww = 1;
        for(int s = 0, l = 0; s < n; s += 2 * m) {
          for(int i = s, j = s + m; i < s + m; i++, j++) {
            Mint x = a[i], y = a[j];
            a[i] = x + y, a[j] = (x - y) * ww;
          }
          ww *= w_inv[__builtin_ctz(++l)];
        }
      }
      Mint inv = 1 / Mint(n);
      for(Mint &x : a) x *= inv;
    }
  }

  static vector<Mint> conv(vector<Mint> a, vector<Mint> b) {
    int sz = ssize(a) + ssize(b) - 1;
    int n = bit_ceil((u32)sz);

    a.resize(n, 0);
    ntt(a, false);
    b.resize(n, 0);
    ntt(b, false);

    for(int i = 0; i < n; i++)
      a[i] *= b[i];

    ntt(a, true);

    a.resize(sz);

    return a;
  }
};

//#include<modint/MontgomeryModInt.cpp>
//#include<poly/NTTmint.cpp>

template<bool fast_mul = true>
struct bigint {
  int sgn;
  vector<int> val;
  static constexpr int LOG = fast_mul ? 1 : 9;
  static constexpr int W = fast_mul ? 10 : 1'000'000'000;

  bigint(string s) {
    if (!s.empty() and s[0] == '-') {
      sgn = -1;
      s.erase(s.begin());
    } else {
      sgn = 1;
    }
    s.insert(0, (LOG - ssize(s) % LOG) % LOG, '0');
    if (s.empty()) s = string(LOG, '0');
    val.resize(size(s) / LOG);
    ranges::reverse(s);
    for(int i = ssize(s) - 1; i >= 0; i--)
      val[i / LOG] = val[i / LOG] * 10 + (s[i] - '0');
  }

  void norm() {
    if (sgn == -1 and ssize(val) == 1 and val[0] == 0)
      sgn = 1;
  }

  bool abs_less(const bigint &b) const {
    if (size(val) != size(b.val))
      return size(val) < size(b.val);
    for(int i = ssize(val) - 1; i >= 0; i--)
      if (val[i] != b.val[i])
        return val[i] < b.val[i];
    return false;
  }

  bigint& operator+=(const bigint &b) {
    if (sgn != b.sgn) {
      *this -= -b;
    } else if (abs_less(b)) {
      *this = b + *this;
    } else {
      for(int i = 0; i < min(ssize(val), ssize(b.val)); i++) {
        val[i] += b.val[i];
        if (int q = val[i] / W; q > 0) {
          if (i + 1 == ssize(val)) val.emplace_back();
          val[i] -= q * W, val[i + 1] += q;
        }
      }
      int j = min(ssize(val), ssize(b.val));
      while(j < ssize(val) and val[j] >= W) {
        int q = val[j] / W;
        if (j + 1 == ssize(val)) val.emplace_back();
        val[j] -= q * W, val[j + 1] += q, j++;
      }
    }
    norm();
    return *this;
  }

  bigint& operator-=(const bigint &b) {
    if (sgn != b.sgn) {
      *this += -b;
    } else if (abs_less(b)) {
      *this = b - *this, sgn = -sgn;
    } else {
      for(int i = 0; i < min(ssize(val), ssize(b.val)); i++) {
        val[i] -= b.val[i];
        if (val[i] < 0)
          val[i] += W, val[i + 1] -= 1;
      }
      int j = min(ssize(val), ssize(b.val));
      while(j < ssize(val) and val[j] < 0)
        val[j] += W, val[j + 1] -= 1, j++;
      while(ssize(val) > 1 and val.back() == 0) val.pop_back();
    }
    norm();
    return *this;
  }

  bigint& operator*=(const bigint &b) {
    if constexpr (LOG == 1) {
      static NTT ntt;
      vector<mint> c(size(val)), d(size(b.val));
      for(int i = 0; i < ssize(c); i++) c[i] = val[i];
      for(int i = 0; i < ssize(d); i++) d[i] = b.val[i];
      c = ntt.conv(c, d);
      vector<int> tmp(ssize(c));
      for(int i = 0; i < ssize(c); i++)
        tmp[i] = c[i].get();
      for(int i = 0; i < ssize(tmp); i++) {
        if (int q = tmp[i] / W; q > 0) {
          if (i + 1 == ssize(tmp)) tmp.emplace_back();
          tmp[i] -= q * W, tmp[i + 1] += q;
        }
      }
      val.swap(tmp);
    } else {
      vector<int> tmp(ssize(val) + ssize(b.val) + 1);
      for(int i = 0; i < ssize(val); i++) {
        for(int j = 0; j < ssize(b.val); j++) {
          if (int q = tmp[i + j] / W; q > 0)
            tmp[i + j] -= q * W, tmp[i + j + 1] += q;
          ll x = (ll)val[i] * b.val[j];
          tmp[i + j] += x % W, tmp[i + j + 1] += x / W;
          if (int q = tmp[i + j] / W; q > 0)
            tmp[i + j] -= q * W, tmp[i + j + 1] += q;
        }
      }
      val.swap(tmp);
    }
    while(ssize(val) > 1 and val.back() == 0) val.pop_back();
    sgn *= b.sgn;
    norm();
    return *this;
  }

  bool operator<(const bigint &b) const {
    if (sgn != b.sgn) return sgn == -1;
    else if (sgn == 1) return abs_less(b);
    else return b.abs_less(*this);
  }
  bool operator>(const bigint &b) const { return b < *this; }
  bool operator<=(const bigint &b) { return !(*this > b); }
  bool operator>=(const bigint &b) { return !(*this < b); }
  bool operator==(const bigint &b) const { return sgn == b.sgn and val == b.val; }
  friend bigint operator+(bigint a, bigint b) { return a += b; }
  friend bigint operator-(bigint a, bigint b) { return a -= b; }
  friend bigint operator*(bigint a, bigint b) { return a *= b; }

  bigint operator-() const {
    bigint b = *this;
    b.sgn = -b.sgn;
    return b;
  }

  string to_string() const {
    string s;
    for(int i = 0; i < ssize(val); i++) {
      int x = val[i];
      for(int j = 0; j < LOG; j++)
        s += '0' + (x % 10), x /= 10;
    }
    while(ssize(s) > 1 and s.back() == '0') s.pop_back();
    if (sgn == -1) s += '-';
    ranges::reverse(s);
    return s;
  }

  friend ostream& operator<<(ostream& os, const bigint& b) {
    return os << b.to_string();
  }
};

const bigint<true> zero("0");

//source: KACTL(for det() and inv())

template<class Mint>
struct matrix : vector<vector<Mint>> {
  matrix(int n, int m) : vector<vector<Mint>>(n, vector<Mint>(m, Mint("0"))) {}
  //matrix(int n) : vector<vector<Mint>>(n, vector<Mint>(n, 0)) {}

  int n() const { return ssize(*this); }
  int m() const { return ssize((*this)[0]); }

  static matrix I(int n) {
    auto res = matrix(n, n);
    for(int i = 0; i < n; i++)
      res[i][i] = 1;
    return res;
  }

  matrix& operator+=(const matrix &b) {
    assert(n() == b.n());
    assert(m() == b.m());
    for(int i = 0; i < n(); i++)
      for(int j = 0; j < m(); j++)
        (*this)[i][j] += b[i][j];
    return *this;
  }

  matrix& operator-=(const matrix &b) {
    assert(n() == b.n());
    assert(m() == b.m());
    for(int i = 0; i < n(); i++)
      for(int j = 0; j < m(); j++)
        (*this)[i][j] -= b[i][j];
    return *this;
  }

  matrix& operator*=(const matrix &b) {
    assert(m() == b.n());
    auto res = matrix(n(), b.m());
    for(int i = 0; i < n(); i++)
      for(int k = 0; k < m(); k++)
        for(int j = 0; j < b.m(); j++)
          if ((i != 0 or j != 1) and (*this)[i][k] != zero and b[k][j] != zero)
            res[i][j] += (*this)[i][k] * b[k][j];
    res[0][1] = res[1][0];
    this -> swap(res);
    return *this;
  }

  matrix pow(ll k) const {
    assert(n() == m());
    auto res = I(n()), base = *this;
    while(k) {
      if (k & 1) res *= base;
      base *= base, k >>= 1;
    }
    return res;
  }

  Mint det() const {
    Mint res = 1;
    auto a = *this;
    for(int i = 0; i < n(); i++) {
      for(int j = i + 1; j < m(); j++) {
        while(a[j][i] != 0) {
          Mint t = a[i][i] / a[j][i];
          if (t != 0)
            for(int k = i; k < n(); k++)
              a[i][k] -= a[j][k] * t;
          swap(a[i], a[j]);
          res = -res;
        }
      }
      res *= a[i][i];
      if (res == 0) return 0;
    }
    return res;
  }

  matrix inv() const {
    assert(n() == m());
    matrix a = *this, tmp = I(n());
    vector<int> col(n());
    for(int i = 0; i < n(); i++) col[i] = i;

    for(int i = 0; i < n(); i++) {
      int r = i, c = i;
      for(int j = i; j < n(); j++) {
        for(int k = i; k < n(); k++) {
          if (a[j][k] != 0) {
            r = j, c = k;
            goto found;
          }
        }
      }
      return matrix(0);
      found:
      a[i].swap(a[r]), tmp[i].swap(tmp[r]);
      for(int j = 0; j < n(); j++)
        swap(a[j][i], a[j][c]), swap(tmp[j][i], tmp[j][c]);
      swap(col[i], col[c]);
      Mint v = 1 / a[i][i];
      for(int j = i + 1; j < n(); j++) {
        Mint f = a[j][i] * v;
        a[j][i] = 0;
        for(int k = i + 1; k < n(); k++)
          a[j][k] -= f * a[i][k];
        for(int k = 0; k < n(); k++)
          tmp[j][k] -= f * tmp[i][k];
      }
      for(int j = i + 1; j < n(); j++) 
        a[i][j] *= v;
      for(int j = 0; j < n(); j++) 
        tmp[i][j] *= v;
      a[i][i] = 1;
    }

    for(int i = n() - 1; i > 0; i--) {
      for(int j = 0; j < i; j++) {
        Mint v = a[j][i];
        for(int k = 0; k < n(); k++)
          tmp[j][k] -= v * tmp[i][k];
      }
    }

    for(int i = 0; i < n(); i++)
      for(int j = 0; j < n(); j++)
        a[col[i]][col[j]] = tmp[i][j];
    return a;
  }

  matrix operator-() { return matrix(n(), m()) - (*this); }
  
  friend matrix operator+(matrix a, matrix b) { return a += b; }
  friend matrix operator-(matrix a, matrix b) { return a -= b; }
  friend matrix operator*(matrix a, matrix b) { return a *= b; }
  
  friend ostream& operator<<(ostream& os, const matrix& b) {
    for(int i = 0; i < b.n(); i++) {
      os << '\n';
      for(int j = 0; j < b.m(); j++)
        os << b[i][j] << ' ';
    }
    return os;
  }
  friend istream& operator>>(istream& is, matrix& b) {
    for(int i = 0; i < b.n(); i++)
      for(int j = 0; j < b.m(); j++)
        is >> b[i][j];
    return is;
  }
};

using M = matrix<bigint<true>>;
vector<M> ms;
M fib(int w) {
  M v = ms[0];
  int len = w - 2;
  int msk = 0;
  for(int i = ssize(ms) - 1; i >= 0; i--)
    if (len >= ssize(ms[i][0][0].val))
      msk |= 1 << i, len -= ssize(ms[i][0][0].val);
  for(int i = 0; i < ssize(ms); i++)
    if (msk >> i & 1)
      v *= ms[i];
  return v;
}

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  ms.emplace_back(2, 2);
  ms.back()[0][0] = ms.back()[0][1] = ms.back()[1][0] = bigint<true>("1");
  while(2 * ssize(ms.back()[0][0].val) <= 2'000'000) {
    ms.emplace_back(ms.back() * ms.back());
    dbg(ms.back()[0][0].val.size());
  }

  dbg(ms.size());

  int n; cin >> n;
  vector<bigint<true>> a;
  for(int i = 0; i < n; i++) {
    string s; cin >> s;
    a.emplace_back(s);
  }

  sort(a.begin(), a.end(), [](auto &x, auto &y) { return x < y; });

  vector<int> nxt(n, -1);

  ll ans = 0;
  for(int i = 0, j = 0; i < n; i = j) {
    while(j < n and a[i] == a[j]) j++;
    nxt[i] = j;
    auto m = fib(ssize(a[i].val));
    bigint<true> c = m[0][0], d = m[0][1];
    dbg(a[i]);
    while(ssize(c.val) <= ssize(a[i].val) or
          (ssize(c.val) == ssize(a[i].val) + 1 and c.val.back() == 1)) {
      if (c > a[i] and ssize(c.val) >= ssize(a[i].val)) {
        auto tar = c - a[i];
        int l = lower_bound(a.begin(), a.begin() + i, tar) - a.begin();
        if (a[l] == tar) {
          int r = nxt[l];
          if (l == i) ans += (ll)(j - i) * (j - i - 1) / 2;
          else ans += (ll)(r - l) * (j - i);
        }
      }
      dbg(c - a[i], ans);
      bigint<true> e = c + d;
      d = c, c = e;
    }
    dbg(a[i], ans);
  }

  cout << ans << '\n';

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1670ms
memory: 134096kb

input:

6
50
8
8
5
72
354224848179261915070

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: -100
Time Limit Exceeded

input:

28
200878223506436882933619847964496455022155117513398820563747455993172799881403389571477889821109288771413214004090719097929400406252135763028179112130390003528046316900603668569910008417315162907579003880220844686222148696041857432602133894827753998572080650383305777912447151917272483538029469449...

output:


result: