結果

問題 No.1269 I hate Fibonacci Number
ユーザー stoqstoq
提出日時 2020-10-11 00:20:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 8,930 bytes
コンパイル時間 2,773 ms
コンパイル使用メモリ 222,636 KB
実行使用メモリ 43,136 KB
最終ジャッジ日時 2024-07-20 17:18:53
合計ジャッジ時間 5,839 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
43,008 KB
testcase_01 AC 31 ms
42,880 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 31 ms
42,880 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 39 ms
43,008 KB
testcase_16 AC 84 ms
43,008 KB
testcase_17 AC 32 ms
43,008 KB
testcase_18 AC 87 ms
43,136 KB
testcase_19 AC 72 ms
43,008 KB
testcase_20 AC 41 ms
43,008 KB
testcase_21 WA -
testcase_22 AC 62 ms
42,880 KB
testcase_23 AC 63 ms
43,008 KB
testcase_24 AC 45 ms
42,880 KB
testcase_25 AC 52 ms
43,008 KB
testcase_26 AC 32 ms
43,008 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 WA -
testcase_29 AC 52 ms
42,880 KB
testcase_30 AC 38 ms
42,880 KB
testcase_31 WA -
testcase_32 AC 49 ms
42,880 KB
testcase_33 WA -
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 36 ms
43,008 KB
testcase_36 AC 30 ms
43,136 KB
testcase_37 AC 30 ms
43,008 KB
testcase_38 AC 31 ms
43,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define MOD_TYPE 1

#pragma region Macros

#include <bits/stdc++.h>
using namespace std;

#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 1
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
//constexpr ll MOD = 1;
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-9;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

struct io_init
{
  io_init()
  {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(30) << setiosflags(ios::fixed);
  };
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

#pragma region mint
template <int MOD>
struct Fp
{
  long long val;

  constexpr Fp(long long v = 0) noexcept : val(v % MOD)
  {
    if (val < 0)
      v += MOD;
  }

  constexpr int getmod()
  {
    return MOD;
  }

  constexpr Fp operator-() const noexcept
  {
    return val ? MOD - val : 0;
  }

  constexpr Fp operator+(const Fp &r) const noexcept
  {
    return Fp(*this) += r;
  }

  constexpr Fp operator-(const Fp &r) const noexcept
  {
    return Fp(*this) -= r;
  }

  constexpr Fp operator*(const Fp &r) const noexcept
  {
    return Fp(*this) *= r;
  }

  constexpr Fp operator/(const Fp &r) const noexcept
  {
    return Fp(*this) /= r;
  }

  constexpr Fp &operator+=(const Fp &r) noexcept
  {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }

  constexpr Fp &operator-=(const Fp &r) noexcept
  {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr Fp &operator*=(const Fp &r) noexcept
  {
    val = val * r.val % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr Fp &operator/=(const Fp &r) noexcept
  {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b)
    {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr bool operator==(const Fp &r) const noexcept
  {
    return this->val == r.val;
  }

  constexpr bool operator!=(const Fp &r) const noexcept
  {
    return this->val != r.val;
  }

  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept
  {
    return os << x.val;
  }

  friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept
  {
    return is >> x.val;
  }
};

Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept
{
  if (n == 0)
    return 1;
  auto t = modpow(a, n / 2);
  t = t * t;
  if (n & 1)
    t = t * a;
  return t;
}

using mint = Fp<MOD>;
#pragma endregion

template <char MIN_CHAR = 'a', int ALPHABET = 26>
struct AhoCorasick
{
  struct node
  {
    // suff : 先頭の文字を最小限消してグラフに存在する頂点にするときの行先の頂点
    // dict : 先頭の文字を最小限消して辞書に存在する単語にするときの行先の頂点
    // depth : Trie木における深さ(省略可能)
    // word_index : このノードで終わる最初の単語のindex(なければ-1)
    // word_count : このノードで終わる単語の総数 count_total_matches()で使用
    // link : Trie及びsuffixの辺の接続先頂点(なければ-1)
    int suff = -1, dict = -1, depth = 0;
    int word_index = -1, word_count = 0;
    int link[ALPHABET];
    node() { fill(link, link + ALPHABET, -1); }
    int &operator[](char c) { return link[c - MIN_CHAR]; }
  };

  // nodes : 頂点集合
  // W : 現在の単語数
  // word_location : 各単語の(単語リストの)index
  // word_indices_by_depth : 単語のindexをdepthの降順に並べたもの
  // defer : 各単語のTrie木の最後の頂点のindex
  vector<node> nodes;
  int W;
  vector<int> word_location;
  vector<int> word_indices_by_depth;
  vector<int> defer;

  AhoCorasick(const vector<string> &words = {})
  {
    if (!words.empty())
      build(words);
  }

  int get_or_add_child(int current, char c)
  {
    if (nodes[current][c] >= 0)
      return nodes[current][c];
    int index = int(nodes.size());
    nodes[current][c] = index;
    nodes.emplace_back();
    nodes.back().depth = nodes[current].depth + 1;
    return index;
  }

  int add_word(const string &word, int word_index)
  {
    assert(!nodes.empty());
    int current = 0;
    for (char c : word)
      current = get_or_add_child(current, c);
    if (nodes[current].word_index < 0)
      nodes[current].word_index = word_index;
    nodes[current].word_count++;
    return current;
  }

  // locationからcを追加したときの行き先 O(1)
  int get_suffix_link(int location, char c) const
  {
    if (location >= 0)
      location = nodes[location].link[c - MIN_CHAR];
    return max(location, 0);
  }

  void build(const vector<string> &words)
  {
    nodes = {node()};
    W = int(words.size());
    word_location.resize(W);
    defer.resize(W);
    int max_depth = 0;

    for (int i = 0; i < W; i++)
    {
      word_location[i] = add_word(words[i], i);
      max_depth = max(max_depth, int(words[i].size()));
      defer[i] = nodes[word_location[i]].word_index;
    }

    word_indices_by_depth.resize(W);
    vector<int> depth_freq(max_depth + 1, 0);

    for (int i = 0; i < W; i++)
      depth_freq[words[i].size()]++;

    for (int i = max_depth - 1; i >= 0; i--)
      depth_freq[i] += depth_freq[i + 1];

    for (int i = 0; i < W; i++)
      word_indices_by_depth[--depth_freq[words[i].size()]] = i;

    vector<int> q = {0};

    for (int i = 0; i < int(q.size()); i++)
    {
      int current = q[i];

      for (char c = MIN_CHAR; c < MIN_CHAR + ALPHABET; c++)
      {
        int &index = nodes[current][c];
        if (index >= 0)
        {
          int suffix_parent = get_suffix_link(nodes[current].suff, c);
          nodes[index].suff = suffix_parent;
          nodes[index].word_count += nodes[suffix_parent].word_count;
          nodes[index].dict = nodes[suffix_parent].word_index < 0 ? nodes[suffix_parent].dict : suffix_parent;
          q.push_back(index);
        }
        else
        {
          index = get_suffix_link(nodes[current].suff, c);
        }
      }
    }
  }
};

void solve()
{
  ll n, l, r;
  assert(cin >> n >> l >> r);
  string temp;
  assert(!(cin >> temp));
  assert(1 <= n and n <= 5000);
  assert(1 <= l and r <= 1e18);
  assert(1 <= r and r <= 1e18);
  assert(l <= r);

  vector<string> F;
  ll f = 1, f1 = 1, f2 = 0;
  while (f <= r)
  {
    if (f >= l)
      F.push_back(to_string(f));
    ll temp = f + f1;
    f2 = f1, f1 = f, f = temp;
  }

  if (F.empty())
  {
    cout << modpow(10, n) - 1 << "\n";
    return;
  }

  AhoCorasick<'0', 10> aho(F);
  mint dp[5010][1010] = {};
  dp[0][0] = 1;
  rep(i, n) rep(state, aho.nodes.size()) rep(j, 10)
  {
    int next_state = aho.get_suffix_link(state, '0' + j);
    if (aho.nodes[next_state].word_index == -1)
      dp[i + 1][next_state] += dp[i][state];
  }
  mint sum = 0;
  rep(state, aho.nodes.size()) sum += dp[n][state];
  sum -= 1;
  cout << sum << "\n";
}

int main()
{
  solve();
}
0