QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#728976 | #9572. Bingo | ucup-team5243# | AC ✓ | 113ms | 15296kb | C++17 | 27.3kb | 2024-11-09 16:17:56 | 2024-11-09 16:18:01 |
Judging History
answer
#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <array>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
using namespace std;
#include <iterator>
#include <functional>
#include <utility>
#include <type_traits>
template<class Elem> struct vec;
template<class Iter>
struct seq_view{
using Ref = typename std::iterator_traits<Iter>::reference;
using Elem = typename std::iterator_traits<Iter>::value_type;
Iter a, b;
Iter begin() const { return a; }
Iter end() const { return b; }
int size() const { return (int)(b-a); }
seq_view(Iter first, Iter last) : a(first), b(last) {}
seq_view sort() const { std::sort(a, b); return *this; }
Ref& operator[](int x) const { return *(a+x); }
template<class F = std::less<Elem>, class ret = vec<int>> ret sorti(F f = F()) const {
ret x(size()); for(int i=0; i<size(); i++) x[i] = i;
x().sort([&](int l, int r){ return f(a[l],a[r]); });
return x;
}
template<class ret = vec<Elem>> ret col() const { return ret(begin(), end()); }
template<class ret = vec<Elem>> ret cumsum() const {
auto res = ret(size() + 1, Elem(0));
for(int i=0; i<size(); i++) res[i+1] = res[i] + operator[](i);
return res;
}
template<class F = std::equal_to<Elem>, class ret = vec<std::pair<Elem, int>>>
ret rle(F eq = F()) const {
auto x = ret();
for(auto& a : (*this)){
if(x.size() == 0 || !eq(x[x.size()-1].first, a)) x.emp(a, 1); else x[x.size()-1].second++;
} return x;
}
template<class F> seq_view sort(F f) const { std::sort(a, b, f); return *this; }
Iter uni() const { return std::unique(a, b); }
Iter lb(const Elem& x) const { return std::lower_bound(a, b, x); }
Iter ub(const Elem& x) const { return std::upper_bound(a, b, x); }
int lbi(const Elem& x) const { return lb(x) - a; }
int ubi(const Elem& x) const { return ub(x) - a; }
seq_view bound(const Elem& l, const Elem& r) const { return { lb(l), lb(r) }; }
template<class F> Iter lb(const Elem& x, F f) const { return std::lower_bound(a, b, x, f); }
template<class F> Iter ub(const Elem& x, F f) const { return std::upper_bound(a, b, x, f); }
template<class F> Iter when_true_to_false(F f) const {
if(a == b) return a;
return std::lower_bound(a, b, *a,
[&](const Elem& x, const Elem&){ return f(x); });
}
seq_view same(Elem x) const { return { lb(x), ub(x) }; }
template<class F> auto map(F f) const {
vec<decltype(f(std::declval<const typename Iter::value_type&>()))> r;
for(auto& x : *this) r.emp(f(x));
return r;
}
Iter max() const { return std::max_element(a, b); }
Iter min() const { return std::min_element(a, b); }
template<class F = std::less<Elem>>
Iter min(F f) const { return std::min_element(a, b, f); }
seq_view rev() const { std::reverse(a, b); return *this; }
};
template<class Elem>
struct vec {
public:
using Base = typename std::vector<Elem>;
using Iter = typename Base::iterator;
using CIter = typename Base::const_iterator;
using View = seq_view<Iter>;
using CView = seq_view<CIter>;
vec(){}
explicit vec(int n, const Elem& value = Elem()) : a(0<n?n:0, value) {}
template <class I2> vec(I2 first, I2 last) : a(first, last) {}
vec(std::initializer_list<Elem> il) : a(std::move(il)) {}
vec(Base b) : a(std::move(b)) {}
operator Base() const { return a; }
Iter begin(){ return a.begin(); }
CIter begin() const { return a.begin(); }
Iter end(){ return a.end(); }
CIter end() const { return a.end(); }
int size() const { return a.size(); }
bool empty() const { return a.empty(); }
Elem& back(){ return a.back(); }
const Elem& back() const { return a.back(); }
vec& sortuni(){ (*this)().sort(); a.erase((*this)().uni(), end()); return *this; }
vec sortunied(){ vec x = *this; x.sortuni(); return x; }
Iter operator()(int x){ return a.begin() + x; }
CIter operator()(int x) const { return a.begin() + x; }
View operator()(int l, int r){ return { (*this)(l), (*this)(r) }; }
CView operator()(int l, int r) const { return { (*this)(l), (*this)(r) }; }
View operator()(){ return (*this)(0,size()); }
CView operator()() const { return (*this)(0,size()); }
Elem& operator[](int x){ return a[x]; }
const Elem& operator[](int x) const { return a[x]; }
Base& operator*(){ return a; }
const Base& operator*() const { return a; }
vec& push(Elem args){
a.push_back(std::move(args));
return *this;
}
template<class... Args>
vec& emp(Args &&... args){
a.emplace_back(std::forward<Args>(args) ...);
return *this;
}
template<class Range>
vec& app(Range& x){ for(auto& v : x){ emp(v); } return *this; }
Elem pop(){ Elem x = std::move(a.back()); a.pop_back(); return x; }
void resize(int sz){ a.resize(sz); }
void reserve(int sz){ a.reserve(sz); }
bool operator==(const vec& r) const { return a == r.a; }
bool operator!=(const vec& r) const { return a != r.a; }
bool operator<(const vec& r) const { return a < r.a; }
bool operator<=(const vec& r) const { return a <= r.a; }
bool operator>(const vec& r) const { return a > r.a; }
bool operator>=(const vec& r) const { return a >= r.a; }
vec<vec<Elem>> pile(int n) const { return vec<vec<Elem>>(n, *this); }
template<class F> vec& filter(F f){
int p = 0;
for(auto& x : a) if(f(x)) std::swap(a[p++],x);
resize(p); return *this;
}
vec& operator+=(const vec& r){ return app(r); }
vec operator+(const vec& r) const { vec x = *this; x += r; return x; }
vec operator*(int t) const { vec res; while(t--){ res += *this; } return res; }
private: Base a;
};
template<class IStr, class U, class T>
IStr& operator>>(IStr& is, vec<std::pair<U,T>>& v){ for(auto& x:v){ is >> x.first >> x.second; } return is; }
template<class IStr, class T>
IStr& operator>>(IStr& is, vec<T>& v){ for(auto& x:v){ is >> x; } return is; }
template<class OStr, class T>
OStr& operator<<(OStr& os, const vec<T>& v){
for(int i=0; i<v.size(); i++){
if(i){ os << ' '; } os << v[i];
} return os;
}
template<class OStr, class U, class T>
OStr& operator<<(OStr& os, const vec<std::pair<U,T>>& v){
for(int i=0; i<v.size(); i++){
if(i){ os << ' '; } os << '(' << v[i].first << ',' << v[i].second << ')';
} return os;
}
#include <cassert>
namespace nachia{
// ax + by = gcd(a,b)
// return ( x, - )
std::pair<long long, long long> ExtGcd(long long a, long long b){
long long x = 1, y = 0;
while(b){
long long u = a / b;
std::swap(a-=b*u, b);
std::swap(x-=y*u, y);
}
return std::make_pair(x, a);
}
} // namespace nachia
namespace nachia{
template<unsigned int MOD>
struct StaticModint{
private:
using u64 = unsigned long long;
unsigned int x;
public:
using my_type = StaticModint;
template< class Elem >
static Elem safe_mod(Elem x){
if(x < 0){
if(0 <= x+MOD) return x + MOD;
return MOD - ((-(x+MOD)-1) % MOD + 1);
}
return x % MOD;
}
StaticModint() : x(0){}
StaticModint(const my_type& a) : x(a.x){}
StaticModint& operator=(const my_type&) = default;
template< class Elem >
StaticModint(Elem v) : x(safe_mod(v)){}
unsigned int operator*() const noexcept { return x; }
my_type& operator+=(const my_type& r) noexcept { auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
my_type operator+(const my_type& r) const noexcept { my_type res = *this; return res += r; }
my_type& operator-=(const my_type& r) noexcept { auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
my_type operator-(const my_type& r) const noexcept { my_type res = *this; return res -= r; }
my_type operator-() const noexcept { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; }
my_type& operator*=(const my_type& r)noexcept { x = (u64)x * r.x % MOD; return *this; }
my_type operator*(const my_type& r) const noexcept { my_type res = *this; return res *= r; }
my_type pow(unsigned long long i) const noexcept {
my_type a = *this, res = 1;
while(i){ if(i & 1){ res *= a; } a *= a; i >>= 1; }
return res;
}
my_type inv() const { return my_type(ExtGcd(x, MOD).first); }
unsigned int val() const noexcept { return x; }
static constexpr unsigned int mod() { return MOD; }
static my_type raw(unsigned int val) noexcept { auto res = my_type(); res.x = val; return res; }
my_type& operator/=(const my_type& r){ return operator*=(r.inv()); }
my_type operator/(const my_type& r) const { return operator*(r.inv()); }
};
} // namespace nachia
using Modint = nachia::StaticModint<998244353>;
namespace nachia{
template<class Modint>
class Comb{
private:
std::vector<Modint> F;
std::vector<Modint> iF;
public:
void extend(int newN){
int prevN = (int)F.size() - 1;
if(prevN >= newN) return;
F.resize(newN+1);
iF.resize(newN+1);
for(int i=prevN+1; i<=newN; i++) F[i] = F[i-1] * Modint::raw(i);
iF[newN] = F[newN].inv();
for(int i=newN; i>prevN; i--) iF[i-1] = iF[i] * Modint::raw(i);
}
Comb(int n = 1){
F.assign(2, Modint(1));
iF.assign(2, Modint(1));
extend(n);
}
Modint factorial(int n) const { return F[n]; }
Modint invFactorial(int n) const { return iF[n]; }
Modint invOf(int n) const { return iF[n] * F[n-1]; }
Modint comb(int n, int r) const {
if(n < 0 || n < r || r < 0) return Modint(0);
return F[n] * iF[r] * iF[n-r];
}
Modint invComb(int n, int r) const {
if(n < 0 || n < r || r < 0) return Modint(0);
return iF[n] * F[r] * F[n-r];
}
Modint perm(int n, int r) const {
if(n < 0 || n < r || r < 0) return Modint(0);
return F[n] * iF[n-r];
}
Modint invPerm(int n, int r) const {
if(n < 0 || n < r || r < 0) return Modint(0);
return iF[n] * F[n-r];
}
Modint operator()(int n, int r) const { return comb(n,r); }
};
} // namespace nachia
namespace nachia{
template<unsigned int MOD>
struct PrimitiveRoot{
using u64 = unsigned long long;
static constexpr u64 powm(u64 a, u64 i) {
u64 res = 1, aa = a;
for( ; i; i /= 2){
if(i & 1) res = res * aa % MOD;
aa = aa * aa % MOD;
}
return res;
}
static constexpr bool ExamineVal(unsigned int g){
u64 t = MOD - 1;
for(u64 d=2; d*d<=t; d+=1+(d&1)) if(t % d == 0){
if(powm(g, (MOD - 1) / d) == 1) return false;
while(t % d == 0) t /= d;
}
if(t != 1) if(powm(g, (MOD - 1) / t) == 1) return false;
return true;
}
static constexpr unsigned int GetVal(){
for(u64 x=2; x<MOD; x++) if(ExamineVal(x)) return x;
return 0;
}
static const unsigned int val = GetVal();
};
} // namespace nachia
namespace nachia{
int Popcount(unsigned long long c) noexcept {
#ifdef __GNUC__
return __builtin_popcountll(c);
#else
c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3));
c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5));
c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17));
c = (c * (~0ull/257)) >> 56;
return c;
#endif
}
// please ensure x != 0
int MsbIndex(unsigned long long x) noexcept {
#ifdef __GNUC__
return 63 - __builtin_clzll(x);
#else
using u64 = unsigned long long;
int q = (x >> 32) ? 32 : 0;
auto m = x >> q;
constexpr u64 hi = 0x8888'8888;
constexpr u64 mi = 0x1111'1111;
m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35;
m = (((m | ~(hi - (x & ~hi))) & hi) * mi) >> 31;
q += (m & 0xf) << 2;
q += 0x3333'3333'2222'1100 >> (((x >> q) & 0xf) << 2) & 0xf;
return q;
#endif
}
// please ensure x != 0
int LsbIndex(unsigned long long x) noexcept {
#ifdef __GNUC__
return __builtin_ctzll(x);
#else
return MsbIndex(x & -x);
#endif
}
}
namespace nachia {
template<class mint>
struct NttInterface{
template<class Iter>
void Butterfly(Iter, int) const {}
template<class Iter>
void IButterfly(Iter, int) const {}
template<class Iter>
void BitReversal(Iter a, int N) const {
for(int i=0, j=0; j<N; j++){
if(i < j) std::swap(a[i], a[j]);
for(int k = N>>1; k > (i^=k); k>>=1);
}
}
};
} // namespace nachia
namespace nachia{
template <class mint>
struct Ntt : NttInterface<mint> {
using u32 = unsigned int;
using u64 = unsigned long long;
static int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (u32)(n)) x++;
return x;
}
static constexpr int bsf_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
struct fft_info {
static constexpr u32 g = nachia::PrimitiveRoot<mint::mod()>::val;
static constexpr int rank2 = bsf_constexpr(mint::mod()-1);
using RootTable = std::array<mint, rank2+1>;
RootTable root, iroot, rate3, irate3;
fft_info(){
root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
iroot[rank2] = root[rank2].inv();
for(int i=rank2-1; i>=0; i--){
root[i] = root[i+1] * root[i+1];
iroot[i] = iroot[i+1] * iroot[i+1];
}
mint prod = 1, iprod = 1;
for(int i=0; i<=rank2-3; i++){
rate3[i] = root[i+3] * prod;
irate3[i] = iroot[i+3] * iprod;
prod *= iroot[i+3];
iprod *= root[i+3];
}
}
};
template<class RandomAccessIterator>
void ButterflyLayered(RandomAccessIterator a, int n, int stride, int repeat) const {
static const fft_info info;
int h = n * stride;
while(repeat--){
int len = 1;
int p = h;
if(ceil_pow2(n)%2 == 1){
p >>= 1;
for(int i=0; i<p; i++){
mint l = a[i], r = a[i+p];
a[i] = l+r; a[i+p] = l-r;
}
len <<= 1;
}
for( ; p > stride; ){
p >>= 2;
mint rot = 1, imag = info.root[2];
u64 mod2 = u64(mint::mod()) * mint::mod();
int offset = p;
for(int s=0; s<len; s++){
if(s) rot *= info.rate3[LsbIndex(~(u32)(s-1))];
mint rot2 = rot * rot;
mint rot3 = rot2 * rot;
for(int i=offset-p; i<offset; i++){
u64 a0 = u64(a[i].val());
u64 a1 = u64(a[i+p].val()) * rot.val();
u64 a2 = u64(a[i+2*p].val()) * rot2.val();
u64 a3 = u64(a[i+3*p].val()) * rot3.val();
u64 a1na3imag = u64(mint(a1 + mod2 - a3).val()) * imag.val();
u64 na2 = mod2 - a2;
a[i] = a0 + a2 + a1 + a3;
a[i+1*p] = a0 + a2 + (2 * mod2 - (a1 + a3));
a[i+2*p] = a0 + na2 + a1na3imag;
a[i+3*p] = a0 + na2 + (mod2 - a1na3imag);
}
offset += p << 2;
}
len <<= 2;
}
a += h;
}
}
template<class RandomAccessIterator>
void Butterfly(RandomAccessIterator a, int n) const {
ButterflyLayered(a, n, 1, 1);
}
template<class RandomAccessIterator>
void IButterflyLayered(RandomAccessIterator a, int n, int stride, int repeat) const {
static const fft_info info;
constexpr int MOD = mint::mod();
while(repeat--){
int len = n;
int p = stride;
for( ; 2 < len; ){
len >>= 2;
mint irot = 1, iimag = info.iroot[2];
int offset = p;
for(int s=0; s<len; s++){
if(s) irot *= info.irate3[LsbIndex(~(u32)(s-1))];
mint irot2 = irot * irot;
mint irot3 = irot2 * irot;
for(int i=offset-p; i<offset; i++){
u64 a0 = a[i].val();
u64 a1 = a[i+p].val();
u64 a2 = a[i+2*p].val();
u64 a3 = a[i+3*p].val();
u64 a2na3iimag = mint((a2 + MOD - a3) * iimag.val()).val();
a[i] = a0 + a1 + a2 + a3;
a[i+p] = (a0 + (MOD - a1) + a2na3iimag) * irot.val();
a[i+2*p] = (a0 + a1 + (MOD - a2) + (MOD - a3)) * irot2.val();
a[i+3*p] = (a0 + (MOD - a1) + (MOD - a2na3iimag)) * irot3.val();
}
offset += p << 2;
}
p <<= 2;
}
if(len == 2){
for(int i=0; i<p; i++){
mint l = a[i], r = a[i+p];
a[i] = l+r; a[i+p] = l-r;
}
p <<= 1;
}
a += p;
}
}
template<class RandomAccessIterator>
void IButterfly(RandomAccessIterator a, int n) const {
IButterflyLayered(a, n, 1, 1);
}
};
} // namespace nachia
namespace nachia {
template<class Elem, class NttInst = Ntt<Elem>>
struct FpsNtt {
public:
using Fps = FpsNtt;
using ElemTy = Elem;
static constexpr unsigned int MOD = Elem::mod();
static constexpr int CONV_THRES = 30;
static const NttInst nttInst;
static const unsigned int zeta = nachia::PrimitiveRoot<MOD>::GetVal();
private:
using u32 = unsigned int;
static Elem ZeroElem() noexcept { return Elem(0); }
static Elem OneElem() noexcept { return Elem(1); }
static Comb<Elem> comb;
std::vector<Elem> a;
int RSZ(int& sz) const { return sz = (sz < 0 ? size() : sz); }
public:
int size() const noexcept { return a.size(); }
Elem& operator[](int x) noexcept { return a[x]; }
const Elem& operator[](int x) const noexcept { return a[x]; }
Elem getCoeff(int x) const noexcept { return (0 <= x && x < size()) ? a[x] : ZeroElem(); }
static Comb<Elem>& GetComb() { return comb; }
static int BestNttSize(int x) noexcept { assert(x); return 1 << MsbIndex(x*2-1); }
Fps move(){ return std::move(*this); }
Fps& set(int i, Elem c){ a[i] = c; return *this; }
Fps& removeLeadingZeros(){
int newsz = size();
while(newsz && a[newsz-1].val() == 0) newsz--;
a.resize(newsz);
if((int)a.capacity() / 4 > newsz) a.shrink_to_fit();
return *this;
}
FpsNtt(){}
FpsNtt(int sz) : a(sz, ZeroElem()) {}
FpsNtt(int sz, Elem e) : a(sz, e) {}
FpsNtt(std::vector<Elem>&& src) : a(std::move(src)) {}
FpsNtt(const std::vector<Elem>& src) : a(src) {}
Fps& ntt() {
capSize(BestNttSize(size()));
nttInst.Butterfly(a.begin(), size());
return *this;
}
Fps& intt() {
nttInst.IButterfly(a.begin(), a.size());
return times(Elem::raw(size()).inv());
}
Fps nttDouble(Fps vanilla) const {
int n = size();
assert(n != 0 && n == (n&-n)); // n is a power of 2
Elem q = Elem::raw(zeta).pow((Elem::mod() - 1) / (n*2));
Elem qq = OneElem();
for(int i=0; i<n; i++){ vanilla[i] *= qq; qq *= q; }
vanilla.ntt();
Fps res = clip(0, n*2);
for(int i=0; i<n; i++) res[n+i] = vanilla[i];
return res;
}
Fps nttDouble() const { return nttDouble(clip().intt().move()); }
// Fps res(resSz);
// for(int j=0; j<resSz-destL && j+srcL < srcR; j++) res[j+destL] = a.getCoeff(j+srcL)
// if srcR is unspecified -> srcR = max(srcL, size());
// if resSz is unspecified -> resSz = destL + srcR - srcL
Fps clip(int srcL, int srcR = -1, int destL = 0, int resSz = -1) const {
srcR = RSZ(srcR);
if(resSz < 0) resSz = destL + srcR - srcL;
int rj = std::min(std::min(srcR, size()) - srcL, resSz - destL);
Fps res(resSz);
for(int j=std::max(0, -srcL); j<rj; j++) res[j+destL] = a[j+srcL];
return res;
}
Fps clip() const { return *this; }
Fps& capSize(int l, int r) {
if(r <= (int)size()) a.resize(r);
if(size() <= l) a.resize(l, ZeroElem());
return *this;
}
Fps& capSize(int z){ a.resize(RSZ(z), ZeroElem()); return *this; }
Fps& times(Elem x){ for(int i=0; i<size(); i++){ a[i] *= x; } return *this; }
Fps& timesFactorial(int z = -1){ comb.extend(RSZ(z)); for(int i=0; i<z; i++){ a[i] *= comb.factorial(i); } return *this; }
Fps& timesInvFactorial(int z = -1){ comb.extend(RSZ(z)); for(int i=0; i<z; i++){ a[i] *= comb.invFactorial(i); } return *this; }
Fps& clrRange(int l, int r){ for(int i=l; i<r; i++){ a[i] = ZeroElem(); } return *this; }
Fps& negate(){ for(auto& e : a){ e = -e; } return *this; }
Fps& mulEach(const Fps& other, int maxi = -1){
maxi = std::min(RSZ(maxi), std::min(size(), other.size()));
for(int i=0; i<maxi; i++) a[i] *= other[i];
return *this;
}
Fps& reverse(int sz = -1){ RSZ(sz); std::reverse(a.begin(), a.begin() + sz); return *this; }
Fps& revRange(int l, int r = -1){ RSZ(r); std::reverse(a.begin() + l, a.begin() + r); return *this; }
static Fps convolution(const Fps& a, const Fps& b, int sz = -1){
if(std::min(a.size(), b.size()) <= CONV_THRES){
if(a.size() > b.size()) return convolution(b, a, sz);
if(sz < 0) sz = std::max(0, a.size() + b.size() - 1);
std::vector<Elem> res(sz);
for(int i=0; i<a.size(); i++) for(int j=0; j<b.size() && i+j<sz; j++) res[i+j] += a[i] * b[j];
return res;
}
int Z = BestNttSize(a.size() + b.size() - 1);
return a.clip(0, Z).ntt().mulEach(b.clip(0, Z).ntt()).intt().capSize(sz).move();
}
Fps convolve(const Fps& r, int sz = -1) const { return convolution(*this, r, sz); }
// 1
// ----- = 1 + f + f^2 + f^3 + ...
// 1-f
Fps powerSum(int sz) const {
RSZ(sz);
if(sz == 0) return {};
int q = std::min(sz, 32);
Fps x = Fps(q).set(0, OneElem()).move();
for(int i=1; i<q; i++) for(int j=1; j<=std::min(i,(int)a.size()-1); j++) x[i] += x[i-j] * a[j];
while(x.size() < sz){
int hN = x.size(), N = hN*2;
Fps a = x.clip(0, N).ntt().move();
Fps b = clip(0, N).ntt().mulEach(a).intt().clrRange(0,hN).ntt().mulEach(a).intt().move();
for(int i=0; i<hN; i++) b[i] = x[i];
std::swap(b, x);
}
return x.capSize(sz).move();
}
Fps inv(int sz = -1) const {
RSZ(sz);
Elem iA0 = a[0].inv();
return clip(0, std::min(sz, size())).times(-iA0).set(0, ZeroElem()).powerSum(sz).times(iA0).move();
}
Fps& difference(){
if(size() == 0) return *this;
for(int i=0; i+1<size(); i++) a[i] = a[i+1] * Elem::raw(i+1);
return capSize(size()-1);
}
Fps& integral(){
if(size() == 0) return capSize(1);
capSize(size()+1);
comb.extend(size());
for(int i=size()-1; i>=1; i--) a[i] = a[i-1] * comb.invOf(i);
return set(0, ZeroElem());
}
Fps log(int sz = -1){
RSZ(sz);
assert(sz != 0);
assert(a[0].val() == 1);
return convolution(inv(sz), clip().difference(), sz-1).integral();
}
Fps exp(int sz = -1){
RSZ(sz);
Fps res = Fps(1).set(0, OneElem());
while(res.size() < sz){
auto z = res.size();
auto tmp = res.capSize(z*2).log().set(0, -OneElem()).move();
for(int i=0; i<z*2 && i<size(); i++) tmp[i] -= a[i];
auto resntt = res.clip().ntt().mulEach(tmp.ntt()).intt().move();
for(int i=z; i<z*2; i++) res[i] = -resntt[i];
}
return res.capSize(0, sz).move();
}
Fps pow(unsigned long long k, int sz = -1){
int n = RSZ(sz);
if(k == 0) return Fps(n).set(0, OneElem()).move();
int ctz = 0;
while(ctz<n && a[ctz].val() == 0) ctz++;
if((unsigned long long)ctz >= (n-1) / k + 1) return Fps(n);
Elem a0 = a[ctz];
return clip(ctz, ctz+n-ctz*k).times(a0.inv()).log().times(Elem(k)).exp().times(a0.pow(k)).clip(0, -1, ctz*k);
}
auto begin(){ return a.begin(); }
auto end(){ return a.end(); }
auto begin() const { return a.begin(); }
auto end() const { return a.end(); }
std::string toString(std::string beg = "[ ", std::string delim = " ", std::string en = " ]") const {
std::string res = beg;
bool f = false;
for(auto x : a){ if(f){ res += delim; } f = true; res += std::to_string(x.val()); }
res += en;
return res;
}
std::vector<Elem> getVectorMoved(){ return std::move(a); }
Fps& operator+=(const Fps& r){
capSize(std::max(size(), r.size()));
for(int i=0; i<r.size(); i++) a[i] += r[i];
return *this;
}
Fps& operator-=(const Fps& r){
capSize(std::max(size(), r.size()));
for(int i=0; i<r.size(); i++) a[i] -= r[i];
return *this;
}
Fps operator+(const Fps& r) const { return (clip(0, std::max(size(), r.size())) += r).move(); }
Fps operator-(const Fps& r) const { return (clip(0, std::max(size(), r.size())) -= r).move(); }
Fps operator-() const { return (clip().negate()).move(); }
Fps operator*(const Fps& r) const { return convolve(r).removeLeadingZeros().move(); }
Fps& operator*=(const Fps& r){ return (*this) = operator*(r); }
Fps& operator*=(Elem m){ return times(m); }
Fps operator*(Elem m) const { return (clip() *= m).move(); }
Elem eval(Elem x) const {
Elem res = 0;
for(int i=size()-1; i>=0; i--) res = res * x + a[i];
return res;
}
};
template<class Elem, class NttInst> Comb<Elem> FpsNtt<Elem, NttInst>::comb;
template<class Elem, class NttInst> const NttInst FpsNtt<Elem, NttInst>::nttInst;
} // namespace nachia
namespace nachia{
template<class Fps>
Fps PolynomialTaylorShift(Fps f, typename Fps::ElemTy c){
int n = f.size();
Fps C = Fps(n).set(0,1);
for(int i=1; i<n; i++) C[i] = C[i-1] * c;
return f.timesFactorial().convolve(
C.timesInvFactorial().reverse()).clip(n-1,2*n-1).timesInvFactorial().move();
}
template<class Fps>
Fps FpsAntiTaylorShift(Fps f, typename Fps::ElemTy c){
int n = f.size();
Fps C = Fps(n).set(0,1);
for(int i=1; i<n; i++) C[i] = C[i-1] * c;
return f.timesInvFactorial().convolve(
C.timesInvFactorial(),n).timesFactorial().move();
}
} // namespace nachia
using Fps = nachia::FpsNtt<Modint>;
void testcase(){
i64 N, M; cin >> N >> M;
vec<i64> A(N*M); cin >> A;
auto comb = nachia::Comb<Modint>(N*M);
A().sort();
for(i64 i=N*M-1; i>=1; i--) A[i] -= A[i-1];
Fps fps(N*M+1);
rep(r,N+1) rep(c,M+1) fps[r*c] += Modint((N+M+r+c)%2 ? -1 : 1) * comb(N,r) * comb(M,c);
fps = nachia::PolynomialTaylorShift(fps, 1);
fps.reverse();
rep(i,N*M+1) fps[i] *= comb.factorial(i) * comb.factorial(N*M-i);
Modint ans = 0;
rep(i,N*M) ans += fps[i] * A[i];
cout << ans.val() << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T;
rep(t,T) testcase();
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
4 2 2 1 3 2 4 3 1 10 10 10 1 3 20 10 30 3 4 1 1 4 5 1 4 1 9 1 9 8 10
output:
56 60 60 855346687
result:
ok 4 number(s): "56 60 60 855346687"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
1 2 2 0 0 998244352 998244352
output:
998244345
result:
ok 1 number(s): "998244345"
Test #3:
score: 0
Accepted
time: 43ms
memory: 4056kb
input:
900 1 1 810487041 1 2 569006976 247513378 1 3 424212910 256484544 661426830 1 4 701056586 563296095 702740883 723333858 1 5 725786515 738465053 821758167 170452477 34260723 1 6 204184507 619884535 208921865 898995024 768400582 369477346 1 7 225635227 321139203 724076812 439129905 405005469 369864252...
output:
810487041 495026756 540662911 541929691 118309348 270925149 575366228 709974238 761347712 304011276 14811741 366145628 638305530 240546928 484276475 603344008 926633861 161768904 239961447 329781933 315752584 578075668 259941994 600147169 402221164 890998500 154285994 181862417 47930994 273729619 64...
result:
ok 900 numbers
Test #4:
score: 0
Accepted
time: 60ms
memory: 3860kb
input:
400 1 995 548100968 635656907 177366910 971271357 314579375 529572241 948721678 455918644 95745688 164857981 499083775 827896554 496889261 111294651 646048809 758286431 163045934 917399362 189372614 267754648 966443706 921589740 228089960 473153545 482816423 37567957 495730380 864445591 568695110 78...
output:
954668084 677509135 636173666 415373646 477286237 209886549 398423120 24466622 672440292 390142124 498517438 305197486 239833057 500821845 475519894 347179487 974036742 810602822 75196204 48378743 393961176 290898056 957916898 434124418 663457674 225283495 704304053 338701802 670053839 137083082 165...
result:
ok 400 numbers
Test #5:
score: 0
Accepted
time: 89ms
memory: 4104kb
input:
40 92 99 14480275 12892621 932457558 584861415 926346518 101583802 498448003 884757899 463949215 661256632 872663851 651132350 565253214 18404795 810166895 145370572 123351313 298382303 777283720 775900024 613503856 817112784 713304484 541301622 595768594 550989875 960159831 571815058 777864097 3608...
output:
614712898 16225927 313765200 824491114 60971514 769510634 58341639 808667102 527187053 319496150 267177120 409701892 245708713 115397703 928197397 533118123 931076329 448328887 672878477 180728812 385639338 504093069 846218180 981546177 906805965 315620628 863877552 348963788 781585156 982673320 275...
result:
ok 40 numbers
Test #6:
score: 0
Accepted
time: 81ms
memory: 4184kb
input:
40 86 92 479103936 690362573 387313968 428679987 770097821 67859949 744428797 919332570 530162857 389639443 851979342 310332074 863845681 155743453 442066584 996725021 385646576 447381083 64960590 818019444 260564007 16381359 36238584 609449698 12466296 532193395 262308857 279184524 454814687 400578...
output:
147127348 995441625 947321329 200561175 846810174 626764591 235790337 30932003 384829067 254218916 20342301 451884441 634808121 241161955 246093492 515701050 978130791 502129313 3431507 775910032 464454612 153447682 53092548 316439192 101505498 40191013 225025922 133184210 209384134 330521977 360716...
result:
ok 40 numbers
Test #7:
score: 0
Accepted
time: 102ms
memory: 15172kb
input:
2 447 447 790583748 764745604 779691526 67598288 308196334 738524513 685610494 336125979 294155123 651917356 898366384 420012139 529304950 133567869 630219750 62853597 606184670 383809162 43962071 826608376 652871696 860138865 675639996 444122802 823442992 841633845 125418467 211407031 726738308 984...
output:
506347658 891054810
result:
ok 2 number(s): "506347658 891054810"
Test #8:
score: 0
Accepted
time: 106ms
memory: 15172kb
input:
2 100 2000 414412015 610256524 548060717 581032168 761297097 50124687 831351681 906381893 842125801 82512258 734351512 844649420 370666628 791011946 232557748 968208094 238084359 933173727 306257334 509581201 774756848 370039888 322700146 641635730 8423279 909781921 238370638 28574480 725141803 9941...
output:
380238486 147107381
result:
ok 2 number(s): "380238486 147107381"
Test #9:
score: 0
Accepted
time: 109ms
memory: 15224kb
input:
2 2000 100 432504867 225538929 546658423 260257767 682179463 892187612 142884587 872658039 89862243 117086929 104310686 342803717 47992235 148221787 695186286 875318238 264248632 320257869 568552597 54600213 364423602 412159309 666014765 235168890 795627687 977929998 351322809 9778000 723545298 1693...
output:
807761546 460321345
result:
ok 2 number(s): "807761546 460321345"
Test #10:
score: 0
Accepted
time: 111ms
memory: 15228kb
input:
2 10 20000 450597719 675029617 315027614 635737834 439025757 505777670 590615658 142679716 637832921 847916068 472514213 71186529 723562195 273447466 297524284 782428382 428366719 869622434 528857976 735817391 650344824 152288845 779100871 130691934 584587742 513859676 996493379 687235989 189730396 ...
output:
579362183 459093435
result:
ok 2 number(s): "579362183 459093435"
Test #11:
score: 0
Accepted
time: 107ms
memory: 15176kb
input:
2 20000 10 770680455 822530420 615615204 314963433 892126521 815622197 900392916 410945746 187559247 278510970 538727855 101559225 98897919 326911775 760152822 689538526 60266407 256706575 791153240 418790216 772229976 194408266 426161021 328204862 71557913 976272337 111201197 504403438 188133891 58...
output:
30164141 385139412
result:
ok 2 number(s): "30164141 385139412"
Test #12:
score: 0
Accepted
time: 112ms
memory: 15296kb
input:
2 100000 2 224212357 458006968 163025536 269449920 699657932 932776912 420937536 166351734 685658904 344666962 946460500 884461444 228370491 174980092 646368291 854381092 617669653 366836379 717071379 463349902 749408189 163205331 665200568 666647060 230069001 195048922 357469436 37819596 212080713 ...
output:
188269415 372357321
result:
ok 2 number(s): "188269415 372357321"
Test #13:
score: 0
Accepted
time: 111ms
memory: 15156kb
input:
2 2 100000 242305209 73289374 463613125 946919872 154514343 546366969 34460325 132627880 629649815 379241632 14429790 612844256 207685982 530434285 412742360 761491236 249569341 450174989 677376758 146322726 339074943 507314636 10270834 864159988 715283525 729222953 772411491 19023116 374520280 8624...
output:
178386797 319825470
result:
ok 2 number(s): "178386797 319825470"
Test #14:
score: 0
Accepted
time: 113ms
memory: 15172kb
input:
2 1 200000 562387945 522780061 928236786 626145471 377386592 856211496 180201513 702883794 179376140 808080887 382633317 110998553 883255942 655659964 711334827 668601380 413687428 303285085 939672021 525550020 460960094 549434056 957565221 759683032 202253696 797371030 885363662 532445034 674913659...
output:
499141558 710898596
result:
ok 2 number(s): "499141558 710898596"
Extra Test:
score: 0
Extra Test Passed