結果

問題 No.1086 桁和の桁和2
ユーザー KoDKoD
提出日時 2020-06-19 22:45:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 3,000 ms
コード長 4,944 bytes
コンパイル時間 823 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 5,068 KB
最終ジャッジ日時 2023-09-29 23:47:19
合計ジャッジ時間 5,965 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 131 ms
4,964 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 83 ms
4,384 KB
testcase_11 AC 23 ms
4,376 KB
testcase_12 AC 6 ms
4,380 KB
testcase_13 AC 104 ms
4,512 KB
testcase_14 AC 66 ms
4,380 KB
testcase_15 AC 26 ms
4,380 KB
testcase_16 AC 145 ms
4,380 KB
testcase_17 AC 14 ms
4,376 KB
testcase_18 AC 200 ms
4,792 KB
testcase_19 AC 165 ms
4,556 KB
testcase_20 AC 21 ms
4,376 KB
testcase_21 AC 142 ms
4,440 KB
testcase_22 AC 199 ms
4,772 KB
testcase_23 AC 122 ms
4,380 KB
testcase_24 AC 86 ms
4,376 KB
testcase_25 AC 168 ms
4,548 KB
testcase_26 AC 138 ms
4,380 KB
testcase_27 AC 100 ms
4,376 KB
testcase_28 AC 83 ms
4,376 KB
testcase_29 AC 93 ms
4,380 KB
testcase_30 AC 205 ms
4,904 KB
testcase_31 AC 206 ms
4,732 KB
testcase_32 AC 206 ms
4,776 KB
testcase_33 AC 206 ms
4,944 KB
testcase_34 AC 207 ms
4,776 KB
testcase_35 AC 132 ms
5,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { ++i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { --i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

template <uint32_t Modulus>
class modular {
public:
  using value_type = uint32_t;
  using max_type = uint64_t;

  static constexpr value_type mod = Modulus;
  static constexpr value_type get_mod() { return mod; }
  static_assert(mod >= 2, "invalid mod :: smaller than 2");
  static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31");

  template <class T>
  static constexpr value_type normalize(T value_) {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= mod;
      if (value_ == 0) return 0;
      return mod - value_;
    }
    return value_ % mod;
  }

private:
  value_type value;

public:
  constexpr modular(): value(0) { }
  template <class T>
  explicit constexpr modular(T value_): value(normalize(value_)) { }
  template <class T>
  explicit constexpr operator T() { return static_cast<T>(value); }

  constexpr value_type get() const { return value; }
  constexpr modular operator - () const { return modular(mod - value); }
  constexpr modular operator ~ () const { return inverse(); }

  constexpr value_type &extract() { return value; }
  constexpr modular inverse() const { return power(mod - 2); }
  constexpr modular power(max_type exp) const {
    modular res(1), mult(*this);
    while (exp > 0) {
      if (exp & 1) res *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return res;
  }

  constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; }
  constexpr modular& operator += (const modular &rhs) { 
    if ((value += rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; }
  constexpr modular& operator -= (const modular &rhs) { 
    if ((value += mod - rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; }
  constexpr modular& operator *= (const modular &rhs) { 
    value = (max_type) value * rhs.value % mod;
    return *this;
  }

  constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; }
  constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); }

  constexpr bool zero() const { return value == 0; }
  constexpr bool operator == (const modular &rhs) const { return value == rhs.value; }
  constexpr bool operator != (const modular &rhs) const { return value != rhs.value; }
  friend std::ostream& operator << (std::ostream &stream, const modular &rhs) {
    return stream << rhs.value;
  }

};

using m32 = modular<1000000007>;

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

template <class T>
void read(T &vec) {
  for (auto &x: vec) {
    std::cin >> x;
  }
}

int main() {
  size_t N;
  std::cin >> N;
  std::vector<u64> L(N), R(N);
  std::vector<u32> D(N);
  read(L);
  read(R);
  read(D);
  size_t start = 0;
  for (auto i: range(0, N)) {
    if (D[i] == 0) {
      start = i + 1;
    }
  }
  for (auto i: range(0, start)) {
    if (D[i] != 0) {
      std::cout << "0\n";
      return 0;
    }
  }
  u32 last = 0;
  m32 ans(1);
  for (auto i: range(start, N)) {
    u32 next = (D[i] + 9 - last) % 9;
    m32 coeff(next == 0 && i != start);
    coeff += (m32(10).power(R[i]) - m32(10).power(L[i])) / m32(9);
    last = D[i];
    ans *= coeff;
  }
  std::cout << ans << '\n';
  return 0;
}
0