Table of Contents

ExplanationImplementation

Explanation

Let's consider ff when we've added two equations to it, {a,b},{c,d}\{a, b\}, \{c, d\}.

  1. c(ax+b)+dc(ax + b) + d
  2. acx+bc+dacx + bc + d

From this, we can make two observations.

  1. In the expanded equation, xx is just multiplied by all of its coefficients.
  2. Any constant is multiplied by all future coefficients.

We can calculate all of this on the fly as we process "add" and "compute" queries, but how do we deal with "removal" queries under a modulus?

Handling Removal Queries

Implementation

Time Complexity: O(logn)\mathcal{O}(\log n)

C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
// source: https://usaco.guide/gold/modular#solution---exponentiation
ll bin_exp(ll x, ll n) {
assert(n >= 0);

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!