Official

B - ABC-DEF Editorial by en_translator


Basically, follow the instructions in the Problem Statement to find the value \((A*B*C-D*E*F)\%998244353\). Notice overflows here. An integer-typed variables in languages like C++ has a limited range; if you try to exceed them, it may cause an unintended behavior. A \(64\)-bit signed integer type can treat up to about \(2^{63}-1=9,223,372,036,854,775,807\simeq 9\times 10^{18}\), so we need to avoid not to exceed the value.

In general, note that for non-negative integers \(A\) and \(B\) and a positive integer \(M\), it holds that \((A*B)\%M=((A\%M)*(B\%M))\%M\); then you can figure out that you first need to find the remainder by \(998244353\) for each variable before computing the product.

Note that the subtraction may result in a negative integer because of taking the modulus firsthand. In such case, you may still obtain the same answers by adding \(998244353\).

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;
const int mod=998244353;

int main() {
  long long a,b,c,d,e,f;
  long long x,y,ans;

  cin >> a >> b >> c >> d >> e >> f;

  x=((a%mod)*(b%mod))%mod;
  x=(x*(c%mod))%mod;
  y=((d%mod)*(e%mod))%mod;
  y=(y*(f%mod))%mod;

  ans=(x+mod-y)%mod;

  cout << ans <<endl;
  return 0;
}

In case of Python, an overflow does not occur. Note however that the execution time bloats as the digits increase, so beware of such issues.

Sample code in Python:

a,b,c,d,e,f=map(int, input().split())
print((a*b*c-d*e*f)%998244353)

posted:
last update: