結果

問題 No.1417 100の倍数かつ正整数(2)
ユーザー kkishikkishi
提出日時 2021-03-10 08:25:52
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 3,000 ms
コード長 11,768 bytes
コンパイル時間 3,226 ms
コンパイル使用メモリ 167,040 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-10-11 23:58:40
合計ジャッジ時間 4,023 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 1 ms
6,816 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 1 ms
6,816 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,816 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 1 ms
6,820 KB
testcase_14 AC 2 ms
6,816 KB
testcase_15 AC 2 ms
6,820 KB
testcase_16 AC 2 ms
6,820 KB
testcase_17 AC 2 ms
6,816 KB
testcase_18 AC 2 ms
6,816 KB
testcase_19 AC 2 ms
6,820 KB
testcase_20 AC 2 ms
6,816 KB
testcase_21 AC 2 ms
6,816 KB
testcase_22 AC 2 ms
6,816 KB
testcase_23 AC 2 ms
6,820 KB
testcase_24 AC 2 ms
6,816 KB
testcase_25 AC 2 ms
6,820 KB
testcase_26 AC 2 ms
6,816 KB
testcase_27 AC 2 ms
6,816 KB
testcase_28 AC 2 ms
6,820 KB
testcase_29 AC 2 ms
6,816 KB
testcase_30 AC 3 ms
6,816 KB
testcase_31 AC 3 ms
6,816 KB
testcase_32 AC 4 ms
6,816 KB
testcase_33 AC 9 ms
6,820 KB
testcase_34 AC 15 ms
9,344 KB
testcase_35 AC 16 ms
9,492 KB
testcase_36 AC 16 ms
9,472 KB
testcase_37 AC 16 ms
9,600 KB
testcase_38 AC 16 ms
9,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template <typename T>
std::map<T, int> Factors(T x) {
  std::map<T, int> m;
  for (T i = 2; i * i <= x;) {
    if (x % i == 0) {
      m[i]++;
      x /= i;
    } else {
      ++i;
    }
  }
  if (x > 1) m[x]++;
  return m;
}

namespace {
using i32 = int32_t;
using i64 = int64_t;
}  // namespace

#define BIN_OPS(F) F(+) F(-) F(*) F(/)
#define CMP_OPS(F) F(!=) F(<) F(<=) F(==) F(>) F(>=)

template <i32 Mod = 1000000007>
class ModInt {
 public:
  ModInt() : n_(0) {}
  ModInt(i64 n) : n_(n % Mod) {
    if (n_ < 0) {
      // In C++, (-n)%m == -(n%m).
      n_ += Mod;
    }
  }
  ModInt& operator+=(const ModInt& m) {
    n_ += m.n_;
    if (n_ >= Mod) {
      n_ -= Mod;
    }
    return *this;
  }
  ModInt& operator++() { return (*this) += 1; }
  ModInt& operator-=(const ModInt& m) {
    n_ -= m.n_;
    if (n_ < 0) {
      n_ += Mod;
    }
    return *this;
  }
  ModInt& operator--() { return (*this) -= 1; }
  ModInt& operator*=(const ModInt& m) {
    n_ = i64(n_) * m.n_ % Mod;
    return *this;
  }
  ModInt& operator/=(const ModInt& m) {
    *this *= m.Inv();
    return *this;
  }
#define DEFINE(op) \
  ModInt operator op(const ModInt& m) const { return ModInt(*this) op## = m; }
  BIN_OPS(DEFINE)
#undef DEFINE
#define DEFINE(op) \
  bool operator op(const ModInt& m) const { return n_ op m.n_; }
  CMP_OPS(DEFINE)
#undef DEFINE
  ModInt operator-() const { return ModInt(-n_); }
  ModInt Pow(i64 n) const {
    if (n < 0) {
      return Inv().Pow(-n);
    }
    // a * b ^ n = answer.
    ModInt a = 1, b = *this;
    while (n != 0) {
      if (n & 1) {
        a *= b;
      }
      n /= 2;
      b *= b;
    }
    return a;
  }
  ModInt Inv() const {
#if DEBUG
    assert(n_ != 0);
#endif
    if (n_ > kMaxCacheSize) {
      // Compute the inverse based on Fermat's little theorem. Note that this
      // only works when n_ and Mod are relatively prime. The theorem says that
      // n_^(Mod-1) = 1 (mod Mod). So we can compute n_^(Mod-2).
      return Pow(Mod - 2);
    }
    for (i64 i = inv_.size(); i <= n_; ++i) {
      inv_.push_back(i <= 1 ? i : (Mod / i * -inv_[Mod % i]));
    }
    return inv_[n_];
  }
  i64 value() const { return n_; }

  static ModInt Fact(i64 n) {
    for (i64 i = fact_.size(); i <= n; ++i) {
      fact_.push_back(i == 0 ? 1 : fact_.back() * i);
    }
    return fact_[n];
  }
  static ModInt InvFact(i64 n) {
    for (i64 i = inv_fact_.size(); i <= n; ++i) {
      inv_fact_.push_back(i == 0 ? 1 : inv_fact_.back() / i);
    }
    return inv_fact_[n];
  }
  static ModInt Comb(i64 n, i64 k) { return Perm(n, k) * InvFact(k); }
  static ModInt CombSlow(i64 n, i64 k) { return PermSlow(n, k) * InvFact(k); }
  static ModInt Perm(i64 n, i64 k) {
#if DEBUG
    assert(n <= kMaxCacheSize &&
           "n is too large. If k is small, consider using PermSlow.");
#endif
    return Fact(n) * InvFact(n - k);
  }
  static ModInt PermSlow(i64 n, i64 k) {
    ModInt p = 1;
    for (i64 i = 0; i < k; ++i) {
      p *= (n - i);
    }
    return p;
  }

 private:
  i32 n_;
  inline static std::vector<ModInt> fact_;
  inline static std::vector<ModInt> inv_fact_;
  inline static std::vector<ModInt> inv_;
  static const i64 kMaxCacheSize = 1000000;
};

#define DEFINE(op)                                            \
  template <i32 Mod, typename T>                              \
  ModInt<Mod> operator op(const T& t, const ModInt<Mod>& m) { \
    return ModInt<Mod>(t) op m;                               \
  }
BIN_OPS(DEFINE)
CMP_OPS(DEFINE)
#undef DEFINE

template <i32 Mod>
std::ostream& operator<<(std::ostream& out, const ModInt<Mod>& m) {
  out << m.value();
  return out;
}
#include <boost/hana/functional/fix.hpp>

template <typename T, typename = void>
struct is_dereferenceable : std::false_type {};
template <typename T>
struct is_dereferenceable<T, std::void_t<decltype(*std::declval<T>())>>
    : std::true_type {};

template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
                                  decltype(std::end(std::declval<T>()))>>
    : std::true_type {};

template <typename T, typename = void>
struct is_applicable : std::false_type {};
template <typename T>
struct is_applicable<T, std::void_t<decltype(std::tuple_size<T>::value)>>
    : std::true_type {};

template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args);
template <typename T>
void debug(const T& v) {
  if constexpr (is_dereferenceable<T>::value) {
    std::cerr << "{";
    if (v) {
      debug(*v);
    } else {
      std::cerr << "nil";
    }
    std::cerr << "}";
  } else if constexpr (is_iterable<T>::value &&
                       !std::is_same<T, std::string>::value) {
    std::cerr << "{";
    for (auto it = std::begin(v); it != std::end(v); ++it) {
      if (it != std::begin(v)) std::cerr << ", ";
      debug(*it);
    }
    std::cerr << "}";
  } else if constexpr (is_applicable<T>::value) {
    std::cerr << "{";
    std::apply([](const auto&... args) { debug(args...); }, v);
    std::cerr << "}";
  } else {
    std::cerr << v;
  }
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
  debug(value);
  std::cerr << ", ";
  debug(args...);
}
#if DEBUG
#define dbg(...)                        \
  do {                                  \
    cerr << #__VA_ARGS__ << ": ";       \
    debug(__VA_ARGS__);                 \
    cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);
#define ints(...) rd(int, __VA_ARGS__);
#define strings(...) rd(string, __VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  if constexpr (std::is_same<T, bool>::value) {
    std::cout << (value ? "Yes" : "No");
  } else if constexpr (is_iterable<T>::value &&
                       !std::is_same<T, std::string>::value) {
    for (auto it = std::begin(value); it != std::end(value); ++it) {
      if (it != std::begin(value)) std::cout << " ";
      std::cout << *it;
    }
  } else {
    std::cout << value;
  }
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  write_to_cout(value);
  std::cout << ' ';
  write_to_cout(args...);
}
#define wt(...)                 \
  do {                          \
    write_to_cout(__VA_ARGS__); \
    cout << '\n';               \
  } while (0)

#define all(x) (x).begin(), (x).end()
#define eb(...) emplace_back(__VA_ARGS__)
#define pb(...) push_back(__VA_ARGS__)

#define dispatch(_1, _2, _3, name, ...) name

#define as_i64(x)                                                          \
  (                                                                        \
      [] {                                                                 \
        static_assert(                                                     \
            std::is_integral<                                              \
                typename std::remove_reference<decltype(x)>::type>::value, \
            "rep macro supports std integral types only");                 \
      },                                                                   \
      static_cast<std::int64_t>(x))

#define rep3(i, a, b) for (std::int64_t i = as_i64(a); i < as_i64(b); ++i)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(_loop_variable_, n)
#define rep(...) dispatch(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep3(i, a, b) for (std::int64_t i = as_i64(b) - 1; i >= as_i64(a); --i)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep1(n) rrep2(_loop_variable_, n)
#define rrep(...) dispatch(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define each3(k, v, c) for (auto&& [k, v] : c)
#define each2(e, c) for (auto&& e : c)
#define each(...) dispatch(__VA_ARGS__, each3, each2)(__VA_ARGS__)

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
  for (T& vi : v) is >> vi;
  return is;
}

template <typename T, typename U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T, typename U>
bool chmax(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, typename U>
bool chmin(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, typename U>
auto max(T a, U b) {
  return a > b ? a : b;
}

template <typename T, typename U>
auto min(T a, U b) {
  return a < b ? a : b;
}

template <typename T>
std::int64_t sz(const T& v) {
  return std::size(v);
}

template <typename T>
std::int64_t popcount(T i) {
  return std::bitset<std::numeric_limits<T>::digits>(i).count();
}

template <typename T>
bool hasbit(T s, int i) {
  return std::bitset<std::numeric_limits<T>::digits>(s)[i];
}

template <typename T, typename U>
auto div_floor(T n, U d) {
  if (d < 0) {
    n = -n;
    d = -d;
  }
  if (n < 0) {
    return -((-n + d - 1) / d);
  }
  return n / d;
};

template <typename T, typename U>
auto div_ceil(T n, U d) {
  if (d < 0) {
    n = -n;
    d = -d;
  }
  if (n < 0) {
    return -(-n / d);
  }
  return (n + d - 1) / d;
}

template <typename T>
bool even(T x) {
  return x % 2 == 0;
}

const std::int64_t big = std::numeric_limits<std::int64_t>::max() / 4;

using i64 = std::int64_t;
using i32 = std::int32_t;

template <typename T>
using low_priority_queue =
    std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

void Main();

int main() {
  Main();
  return 0;
}

const auto& Fix = boost::hana::fix;

using namespace std;

#define int i64

using mint = ModInt<>;

void Main() {
  strings(n);
  each(e, n) e -= '0';
  int N = sz(n);
  vector dp(N + 1, vector(3, vector(3, vector(3, mint(0)))));
  dp[0][0][0][0] = 1;
  V<int> two(10), five(10);
  rep(i, 10) {
    auto fs = Factors(i);
    two[i] = fs[2];
    five[i] = fs[5];
  }
  rep(i, N) {
    // j == 0: all zeros on the left
    // j == 1: equal to the input on the left
    // j == 2: non zero but smaller than the input on the left
    rep(j, 3) {
      // k: the number of 2s. k == 2 indicates that it may be more than 2.
      rep(k, 3) {
        // l: the number of 5s. l == 2 indicates that it may be more than 2.
        rep(l, 3) {
          if (dp[i][j][k][l] == 0) continue;
          rep(m, 10) {
            int nj;
            if (j == 0) {
              if (m == 0) {
                nj = 0;
              } else {
                if (m == 0) continue;
                if (i == 0) {
                  if (m < n[0]) {
                    nj = 2;
                  } else if (m == n[0]) {
                    nj = 1;
                  } else {
                    continue;
                  }
                } else {
                  nj = 2;
                }
              }
            } else if (j == 1) {
              if (m == 0) continue;
              if (m < n[i]) {
                nj = 2;
              } else if (m == n[i]) {
                nj = 1;
              } else {
                continue;
              }
            } else {
              if (m == 0) continue;
              nj = 2;
            }
            int nk = m == 0 ? 0 : min(2, k + two[m]);
            int nl = m == 0 ? 0 : min(2, l + five[m]);
            dp[i + 1][nj][nk][nl] += dp[i][j][k][l];
          }
        }
      }
    }
  }
  mint ans = 0;
  rep(i, 3) ans += dp[N][i][2][2];
  wt(ans);
}
0