跳到正文

题解

题解:P15375 Soso 的模法矩阵 / modmat

yukimianyan约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年2月24日
更新
2026年2月24日
篇幅
约 7 分钟
题目
P15375

版本

  1. v2当前
  2. v1

希望大家喜欢这个题目。

题目描述

记 {ai}\{a_i\} 的前缀积数组为 AA,{bj}\{b_j\} 的前缀积数组为 BB。对每对 i,ji, j 求 (Ai mod Bj) mod 998244353(A_i\bmod B_j)\bmod 998244353。

n,m≤5000,ai,bj≤109n, m\leq 5000, a_i, b_j\leq 10^9。

subtask 1

答案要么是 aia_i 的一段前缀的积,要么是 00。

subtask 2

观察到 1515≤101815^{15}\leq 10^{18}。暴力计算答案。

subtask 5

用十进制高精度计算 AA,则 Ai mod 10jA_i\bmod 10^j 可以表示为 AA 的低 jj 位。精细实现应该可以 O(nm)O(nm)。

subtask 7

预期使用高精度取模通过,复杂度根据实现决定,Python 等自带高精度的语言写的程序有可能可以通过。

subtask 10

维护变进制数。从小到大枚举 ii,时刻维护一个长为 m+1m+1 的非负整数数组 cic_i 使得

  • 对所有 1≤i≤m1\leq i\leq m,都有 0≤ci<bi0\leq c_i<b_i。
  • ∏i=1i0ai=∑j=1m+1cj∏k=1j−1bk\prod_{i=1}^{i_0}a_i=\sum_{j=1}^{m+1}c_j\prod_{k=1}^{j-1}b_k。

当 ii 加一时,考虑从小到大枚举 jj,将 cjc_j 乘上 ai+1a_{i+1},若 cj≥bjc_j\geq b_j,则使 cj+1c_{j+1} 加上 ⌊cj/bj⌋\left\lfloor c_j/b_j\right\rfloor,并将 cjc_j 改为 cj mod bjc_j\bmod b_j。显然这样仍然满足上述的两条性质。

求解答案时,答案就是 cic_i 的低 jj 个数(c[1..j]c[1..j])组成的变进制数对应的原数。精细实现可以在 O(nm)O(nm) 的时间复杂度内解决原问题。

subtask 8, 9 预留给常数较大或者需要求逆元的程序通过。

std

C++
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
template <class T>
using must_int = enable_if_t<is_integral<T>::value, int>;
template <unsigned umod>
struct modint {/*{{{*/
  static constexpr int mod = umod;
  unsigned v;
  modint() : v(0) {}
  template <class T, must_int<T> = 0>
  modint(T x) {
    x %= mod;
    v = x < 0 ? x + mod : x;
  }
  modint operator+() const { return *this; }
  modint operator-() const { return modint() - *this; }
  friend int raw(const modint &self) { return self.v; }
  friend ostream& operator<<(ostream& os, const modint &self) {
    return os << raw(self);
  }
  modint &operator+=(const modint &rhs) {
    v += rhs.v;
    if (v >= umod) v -= umod;
    return *this;
  }
  modint &operator-=(const modint &rhs) {
    v -= rhs.v;
    if (v >= umod) v += umod;
    return *this;
  }
  modint &operator*=(const modint &rhs) {
    v = 1ull * v * rhs.v % umod;
    return *this;
  }
  modint &operator/=(const modint &rhs) {
    assert(rhs.v);
    return *this *= qpow(rhs, mod - 2);
  }
  template <class T, must_int<T> = 0>
  friend modint qpow(modint a, T b) {
    modint r = 1;
    for (; b; b >>= 1, a *= a)
      if (b & 1) r *= a;
    return r;
  }
  friend modint operator+(modint lhs, const modint &rhs) { return lhs += rhs; }
  friend modint operator-(modint lhs, const modint &rhs) { return lhs -= rhs; }
  friend modint operator*(modint lhs, const modint &rhs) { return lhs *= rhs; }
  friend modint operator/(modint lhs, const modint &rhs) { return lhs /= rhs; }
  bool operator==(const modint &rhs) const { return v == rhs.v; }
  bool operator!=(const modint &rhs) const { return v != rhs.v; }
};/*}}}*/
using mint = modint<998244353>;
using modi = modint<int(1e9 + 7)>;
int n, m, a[5010], b[5010];
LL c[5010];
int main() {
#ifndef LOCAL
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  cin >> m >> n;
  for (int j = 1; j <= m; j++) cin >> b[j];
  for (int i = 1; i <= n; i++) cin >> a[i];
  c[1] = 1;
  for (int j = 1; j <= m; j++) {
    mint ret = 0, pre = 1;
    LL s = 0;
    modi ans = 0;
    for (int i = 1; i <= n; i++) {
      LL tmp = c[i] * b[j] + s;
      c[i] = tmp % a[i];
      ret += pre * c[i];
      pre *= a[i];
      s = tmp / a[i];
      ans = ans * 998244353 + raw(ret);
    }
    cout << ans << endl;
  }
  return 0;
}
原文