QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#305305 | #8005. Crossing the Border | ucup-team159 | AC ✓ | 4385ms | 454344kb | C++20 | 36.5kb | 2024-01-15 03:57:23 | 2024-01-15 03:57:23 |
Judging History
answer
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("Ofast")
//#undef LOCAL
#include <bit>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cstring>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include <bit>
#include <cstdint>
#include <cassert>
#include <numeric>
#include <type_traits>
namespace yosupo {
namespace internal {
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral =
typename std::conditional<std::is_integral<T>::value ||
internal::is_signed_int128<T>::value ||
internal::is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
template <class T>
using is_integral_t = std::enable_if_t<is_integral<T>::value>;
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace yosupo
namespace yosupo {
struct Scanner {
public:
Scanner(const Scanner&) = delete;
Scanner& operator=(const Scanner&) = delete;
Scanner(FILE* fp) : fd(fileno(fp)) { line[0] = 127; }
void read() {}
template <class H, class... T> void read(H& h, T&... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
int read_unsafe() { return 0; }
template <class H, class... T> int read_unsafe(H& h, T&... t) {
bool f = read_single(h);
if (!f) return 0;
return 1 + read_unsafe(t...);
}
int close() { return ::close(fd); }
private:
static constexpr int SIZE = 1 << 15;
int fd = -1;
std::array<char, SIZE + 1> line;
int st = 0, ed = 0;
bool eof = false;
bool read_single(std::string& ref) {
if (!skip_space()) return false;
ref = "";
while (true) {
char c = top();
if (c <= ' ') break;
ref += c;
st++;
}
return true;
}
bool read_single(double& ref) {
std::string s;
if (!read_single(s)) return false;
ref = std::stod(s);
return true;
}
template <class T,
std::enable_if_t<std::is_same<T, char>::value>* = nullptr>
bool read_single(T& ref) {
if (!skip_space<50>()) return false;
ref = top();
st++;
return true;
}
template <class T,
internal::is_signed_int_t<T>* = nullptr,
std::enable_if_t<!std::is_same<T, char>::value>* = nullptr>
bool read_single(T& sref) {
using U = internal::to_unsigned_t<T>;
if (!skip_space<50>()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
U ref = 0;
do {
ref = 10 * ref + (line[st++] & 0x0f);
} while (line[st] >= '0');
sref = neg ? -ref : ref;
return true;
}
template <class U,
internal::is_unsigned_int_t<U>* = nullptr,
std::enable_if_t<!std::is_same<U, char>::value>* = nullptr>
bool read_single(U& ref) {
if (!skip_space<50>()) return false;
ref = 0;
do {
ref = 10 * ref + (line[st++] & 0x0f);
} while (line[st] >= '0');
return true;
}
bool reread() {
if (ed - st >= 50) return true;
if (st > SIZE / 2) {
std::memmove(line.data(), line.data() + st, ed - st);
ed -= st;
st = 0;
}
if (eof) return false;
auto u = ::read(fd, line.data() + ed, SIZE - ed);
if (u == 0) {
eof = true;
line[ed] = '\0';
u = 1;
}
ed += int(u);
line[ed] = char(127);
return true;
}
char top() {
if (st == ed) {
bool f = reread();
assert(f);
}
return line[st];
}
template <int TOKEN_LEN = 0> bool skip_space() {
while (true) {
while (line[st] <= ' ') st++;
if (ed - st > TOKEN_LEN) return true;
if (st > ed) st = ed;
for (auto i = st; i < ed; i++) {
if (line[i] <= ' ') return true;
}
if (!reread()) return false;
}
}
};
struct Printer {
public:
template <char sep = ' ', bool F = false> void write() {}
template <char sep = ' ', bool F = false, class H, class... T>
void write(const H& h, const T&... t) {
if (F) write_single(sep);
write_single(h);
write<true>(t...);
}
template <char sep = ' ', class... T> void writeln(const T&... t) {
write<sep>(t...);
write_single('\n');
}
Printer(FILE* _fp) : fd(fileno(_fp)) {}
~Printer() { flush(); }
int close() {
flush();
return ::close(fd);
}
void flush() {
if (pos) {
auto res = ::write(fd, line.data(), pos);
assert(res != -1);
pos = 0;
}
}
private:
static std::array<std::array<char, 2>, 100> small;
static std::array<unsigned long long, 20> tens;
static constexpr size_t SIZE = 1 << 15;
int fd;
std::array<char, SIZE> line;
size_t pos = 0;
std::stringstream ss;
template <class T,
std::enable_if_t<std::is_same<char, T>::value>* = nullptr>
void write_single(const T& val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T,
internal::is_signed_int_t<T>* = nullptr,
std::enable_if_t<!std::is_same<char, T>::value>* = nullptr>
void write_single(const T& val) {
using U = internal::to_unsigned_t<T>;
if (val == 0) {
write_single('0');
return;
}
if (pos > SIZE - 50) flush();
U uval = val;
if (val < 0) {
write_single('-');
uval = -uval;
}
write_unsigned(uval);
}
template <class U, internal::is_unsigned_int_t<U>* = nullptr>
void write_single(U uval) {
if (uval == 0) {
write_single('0');
return;
}
if (pos > SIZE - 50) flush();
write_unsigned(uval);
}
static int calc_len(uint64_t x) {
int i = ((63 - std::countl_zero(x)) * 3 + 3) / 10;
if (x < tens[i])
return i;
else
return i + 1;
}
template <class U,
internal::is_unsigned_int_t<U>* = nullptr,
std::enable_if_t<2 >= sizeof(U)>* = nullptr>
void write_unsigned(U uval) {
size_t len = calc_len(uval);
pos += len;
char* ptr = line.data() + pos;
while (uval >= 100) {
ptr -= 2;
memcpy(ptr, small[uval % 100].data(), 2);
uval /= 100;
}
if (uval >= 10) {
memcpy(ptr - 2, small[uval].data(), 2);
} else {
*(ptr - 1) = char('0' + uval);
}
}
template <class U,
internal::is_unsigned_int_t<U>* = nullptr,
std::enable_if_t<4 == sizeof(U)>* = nullptr>
void write_unsigned(U uval) {
std::array<char, 8> buf;
memcpy(buf.data() + 6, small[uval % 100].data(), 2);
memcpy(buf.data() + 4, small[uval / 100 % 100].data(), 2);
memcpy(buf.data() + 2, small[uval / 10000 % 100].data(), 2);
memcpy(buf.data() + 0, small[uval / 1000000 % 100].data(), 2);
if (uval >= 100000000) {
if (uval >= 1000000000) {
memcpy(line.data() + pos, small[uval / 100000000 % 100].data(),
2);
pos += 2;
} else {
line[pos] = char('0' + uval / 100000000);
pos++;
}
memcpy(line.data() + pos, buf.data(), 8);
pos += 8;
} else {
size_t len = calc_len(uval);
memcpy(line.data() + pos, buf.data() + (8 - len), len);
pos += len;
}
}
template <class U,
internal::is_unsigned_int_t<U>* = nullptr,
std::enable_if_t<8 == sizeof(U)>* = nullptr>
void write_unsigned(U uval) {
size_t len = calc_len(uval);
pos += len;
char* ptr = line.data() + pos;
while (uval >= 100) {
ptr -= 2;
memcpy(ptr, small[uval % 100].data(), 2);
uval /= 100;
}
if (uval >= 10) {
memcpy(ptr - 2, small[uval].data(), 2);
} else {
*(ptr - 1) = char('0' + uval);
}
}
template <
class U,
std::enable_if_t<internal::is_unsigned_int128<U>::value>* = nullptr>
void write_unsigned(U uval) {
static std::array<char, 50> buf;
size_t len = 0;
while (uval > 0) {
buf[len++] = char((uval % 10) + '0');
uval /= 10;
}
std::reverse(buf.begin(), buf.begin() + len);
memcpy(line.data() + pos, buf.data(), len);
pos += len;
}
void write_single(const std::string& s) {
for (char c : s) write_single(c);
}
void write_single(const char* s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write_single(s[i]);
}
template <class T> void write_single(const std::vector<T>& val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write_single(' ');
write_single(val[i]);
}
}
};
std::array<std::array<char, 2>, 100> Printer::small = [] {
std::array<std::array<char, 2>, 100> table;
for (int i = 0; i <= 99; i++) {
table[i][1] = char('0' + (i % 10));
table[i][0] = char('0' + (i / 10 % 10));
}
return table;
}();
std::array<unsigned long long, 20> Printer::tens = [] {
std::array<unsigned long long, 20> table;
for (int i = 0; i < 20; i++) {
table[i] = 1;
for (int j = 0; j < i; j++) {
table[i] *= 10;
}
}
return table;
}();
} // namespace yosupo
#include <cassert>
#include <numeric>
#include <type_traits>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#include <utility>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}
struct barrett {
unsigned int _m;
unsigned long long im;
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
unsigned int umod() const { return _m; }
unsigned int mul(unsigned int a, unsigned int b) const {
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned long long y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0) m0 += b / s;
return {s, m0};
}
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
unsigned long long y_max = a * n + b;
if (y_max < m) break;
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}
} // namespace internal
} // namespace atcoder
#include <cassert>
#include <numeric>
#include <type_traits>
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
namespace atcoder {
namespace internal {
struct modint_base {};
struct static_modint_base : modint_base {};
template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;
} // namespace internal
template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;
public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};
template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;
public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;
namespace internal {
template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
} // namespace internal
} // namespace atcoder
#include <iostream>
namespace atcoder {
template <int MOD>
std::ostream& operator<<(std::ostream& os, const static_modint<MOD>& x) {
return os << x.val();
}
template <int ID>
std::ostream& operator<<(std::ostream& os, const dynamic_modint<ID>& x) {
return os << x.val();
}
} // namespace atcoder
namespace yosupo {
template <int MOD> using static_modint = atcoder::static_modint<MOD>;
template <int ID> using dynamic_modint = atcoder::dynamic_modint<ID>;
using modint998244353 = atcoder::modint998244353;
using modint1000000007 = atcoder::modint1000000007;
using modint = atcoder::modint;
struct modint61 {
using mint = modint61;
public:
static constexpr long long mod() { return (1ULL << 61) - 1; }
static mint raw(long long v) {
mint x;
x._v = v;
return x;
}
modint61() : _v(0) {}
template <class T, atcoder::internal::is_signed_int_t<T>* = nullptr>
modint61(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned long long)(x);
}
template <class T, atcoder::internal::is_unsigned_int_t<T>* = nullptr>
modint61(T v) {
_v = (unsigned long long)(v % umod());
}
unsigned long long val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
__uint128_t t = (__uint128_t) _v * rhs._v;
_v = (unsigned long long)((t >> 61) + (t & umod()));
_v = (_v >= umod()) ? _v - umod() : _v;
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
assert(_v);
return pow(umod() - 2);
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned long long _v;
static constexpr unsigned long long umod() { return mod(); }
};
} // namespace yosupo
using namespace yosupo;
using mint = modint998244353;
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <memory>
#include <utility>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
#ifdef LOCAL
ostream& operator<<(ostream& os, __int128_t x) {
if (x < 0) {
os << "-";
x *= -1;
}
if (x == 0) {
return os << "0";
}
string s;
while (x) {
s += char(x % 10 + '0');
x /= 10;
}
reverse(s.begin(), s.end());
return os << s;
}
ostream& operator<<(ostream& os, __uint128_t x) {
if (x == 0) {
return os << "0";
}
string s;
while (x) {
s += char(x % 10 + '0');
x /= 10;
}
reverse(s.begin(), s.end());
return os << s;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p);
template <class T> ostream& operator<<(ostream& os, const V<T>& v);
template <class T> ostream& operator<<(ostream& os, const deque<T>& v);
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& a);
template <class T> ostream& operator<<(ostream& os, const set<T>& s);
template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& m);
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
return os << "P(" << p.first << ", " << p.second << ")";
}
template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
os << "[";
bool f = false;
for (auto d : v) {
if (f) os << ", ";
f = true;
os << d;
}
return os << "]";
}
template <class T> ostream& operator<<(ostream& os, const deque<T>& v) {
os << "[";
bool f = false;
for (auto d : v) {
if (f) os << ", ";
f = true;
os << d;
}
return os << "]";
}
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& a) {
os << "[";
bool f = false;
for (auto d : a) {
if (f) os << ", ";
f = true;
os << d;
}
return os << "]";
}
template <class T> ostream& operator<<(ostream& os, const set<T>& s) {
os << "{";
bool f = false;
for (auto d : s) {
if (f) os << ", ";
f = true;
os << d;
}
return os << "}";
}
template <class T> ostream& operator<<(ostream& os, const multiset<T>& s) {
os << "{";
bool f = false;
for (auto d : s) {
if (f) os << ", ";
f = true;
os << d;
}
return os << "}";
}
template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& s) {
os << "{";
bool f = false;
for (auto p : s) {
if (f) os << ", ";
f = true;
os << p.first << ": " << p.second;
}
return os << "}";
}
struct PrettyOS {
ostream& os;
bool first;
template <class T> auto operator<<(T&& x) {
if (!first) os << ", ";
first = false;
os << x;
return *this;
}
};
template <class... T> void dbg0(T&&... t) {
(PrettyOS{cerr, true} << ... << t);
}
#define dbg(...) \
do { \
cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
dbg0(__VA_ARGS__); \
cerr << endl; \
} while (false);
#else
#define dbg(...)
#endif
Scanner sc = Scanner(stdin);
Printer pr = Printer(stdout);
using Pol32 = array<int, 22>;
using Pol64 = array<ll, 22>;
template <class Pol>
void zeta(int n, V<Pol>& dp) {
assert((1 << n) == ssize(dp));
for (int h = 0; h < n; h++) {
for (int ph = 0; ph < (1 << n); ph++) {
if (ph & (1 << h)) continue;
for (int k = 0; k < 22; k++) {
dp[ph | (1 << h)][k] += dp[ph][k];
}
}
}
}
int main() {
int n, W;
sc.read(n, W);
using P = pair<int, int>;
V<P> items(n);
for (int i = 0; i < n; i++) {
sc.read(items[i].first, items[i].second);
}
sort(items.begin(), items.end(), [&](P l, P r) {
return l.second < r.second;
});
V<bool> ok(1 << n);
for (int f = 0; f < (1 << n); f++) {
int sum = 0;
for (int i = 0; i < n; i++) {
if (f & (1 << i)) sum += items[i].first;
}
ok[f] = sum <= W;
}
V<Pol64> dp(1 << (n - 1));
for (int f = 0; f < (1 << (n - 1)); f++) {
if (ok[f | (1 << (n - 1))]) {
dp[f][popcount(uint(f))] = 1;
}
}
zeta(n - 1, dp);
for (int ph = n - 2; ph >= 0; ph--) {
V<Pol32> mul(1 << ph);
for (int f = 0; f < (1 << ph); f++) {
if (ok[f | (1 << ph)]) {
mul[f][popcount(uint(f)) + 1] = 1;
}
}
zeta(ph, mul);
for (int f = 0; f < (1 << (n - 1)); f++) {
if (f & (1 << ph)) continue;
for (int k = 0; k < 22; k++) {
dp[f | (1 << ph)][k] -= dp[f][k];
}
Pol64 pol;
pol.fill(0);
for (int k1 = 0; k1 < 22; k1++) {
for (int k2 = 0; k1 + k2 < 22; k2++) {
pol[k1 + k2] += dp[f][k1] * mul[f % (1 << ph)][k2];
}
}
dp[f] = pol;
}
}
int cost = TEN(9) + TEN(8) + TEN(7);
mint ans = 0;
for (int f = 0; f < (1 << (n - 1)); f++) {
ll way = dp[f][n - 1];
if (way == 0) continue;
int now = items[n - 1].second;
for (int i = 0; i < n - 1; i++) {
if (f & (1 << i)) continue;
now += items[i].second;
}
if (now < cost) {
cost = now;
ans = way;
} else if (now == cost) {
ans += way;
}
}
pr.writeln(cost, " ", ans.val() % 998244353);
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3580kb
input:
5 5 3 5 1 4 2 3 2 2 2 1
output:
9 4
result:
ok 2 number(s): "9 4"
Test #2:
score: 0
Accepted
time: 205ms
memory: 31388kb
input:
18 10000000 956231 904623 1692946 1796774 1081323 1170319 3218792 2542661 3183376 3037270 1869132 1442561 35436 35018 1564635 1939950 1847344 2006043 755870 899310 1671882 2057413 1369264 1338951 3132483 3504034 2056224 1825640 1840949 1562071 1514040 1405352 2300821 2421801 2466540 3004920
output:
9391997 70
result:
ok 2 number(s): "9391997 70"
Test #3:
score: 0
Accepted
time: 961ms
memory: 115768kb
input:
20 10000000 1289384 1416015 1692778 1966748 1747794 1708650 2885387 2925290 2516650 2410838 2202363 2092667 368691 407497 1897764 1902790 180541 224758 1089173 1075924 2005212 1743637 702568 566295 465783 369143 2722863 2902398 174068 150211 513930 519657 1634023 1313239 1133070 1040937 961394 11066...
output:
6331196 89
result:
ok 2 number(s): "6331196 89"
Test #4:
score: 0
Accepted
time: 2037ms
memory: 228568kb
input:
21 10000000 1432782 1230128 1693282 1456826 605524 521515 2742745 3427204 2231114 2129928 2345527 2397808 511783 521160 2041234 2313965 2323807 2603481 1232121 1410811 719508 850004 416942 495559 2180169 2579591 1580089 1786914 2317568 2292171 1514260 1143717 1348703 1495001 562052 525544 2818854 23...
output:
9336572 5
result:
ok 2 number(s): "9336572 5"
Test #5:
score: 0
Accepted
time: 4314ms
memory: 454116kb
input:
22 10000000 1562592 1176882 1693226 1513484 2293770 2757728 2612851 3010518 1971354 2392268 2475363 2035487 641627 684375 2171036 2181775 1544541 1633457 1361981 1060447 2277948 2792254 157192 141039 1011327 1139897 541119 577682 1538276 1451191 2423314 2061841 1088919 1154927 42526 43789 1779858 16...
output:
8019829 516
result:
ok 2 number(s): "8019829 516"
Test #6:
score: 0
Accepted
time: 4348ms
memory: 454312kb
input:
22 50000000 9900000 50000000 9800000 50000000 9700000 50000000 9600000 50000000 9500000 50000000 9400000 50000000 9300000 50000000 9200000 50000000 9100000 50000000 9190000 50000000 9180000 50000000 9170000 50000000 9160000 50000000 9150000 50000000 9140000 50000000 9130000 50000000 9120000 50000000...
output:
250000000 659827482
result:
ok 2 number(s): "250000000 659827482"
Test #7:
score: 0
Accepted
time: 4260ms
memory: 454068kb
input:
22 49989999 9515787 13633636 7804670 14201704 4490825 15337840 10846676 15905908 4649834 16473976 13564408 17042044 26330177 17610112 31079612 18178180 9508811 18746248 11012937 19314316 9221231 19882384 35630511 20450452 33570222 21018520 33987302 22154656 28961982 22722724 29610359 23290792 342743...
output:
297099616 239005143
result:
ok 2 number(s): "297099616 239005143"
Test #8:
score: 0
Accepted
time: 4358ms
memory: 454224kb
input:
22 49889999 4418358 4535448 7690530 4724425 3667793 4913402 8304776 5102379 2539846 5291356 2404119 5480333 2368750 5669310 3896314 5858287 6349833 6047264 10839169 6425218 10867512 6614195 9018761 6803172 5396983 6992149 2026994 7181126 6093366 7370103 10403853 7559080 5578332 7748057 13492459 7937...
output:
28157573 762
result:
ok 2 number(s): "28157573 762"
Test #9:
score: 0
Accepted
time: 4324ms
memory: 454180kb
input:
22 48889995 670320 2222256 1480754 2407444 2099421 2592632 3936255 2777820 959515 2963008 4781765 3333384 5446683 3518572 1207621 3703760 5965481 3888948 5353960 4074136 3991352 4259324 3814761 4629700 7642867 4814888 6776227 5000076 7737927 5185264 6271909 5370452 8235670 5555640 5720862 5740828 94...
output:
15926168 295
result:
ok 2 number(s): "15926168 295"
Test #10:
score: 0
Accepted
time: 4365ms
memory: 454084kb
input:
22 48889995 3609129 2222256 4285697 2407444 1507969 2592632 1231013 2777820 3176763 2963008 3473072 3148196 7482780 3518572 2891596 3703760 8705206 3888948 7267923 4074136 7325365 4259324 2847372 4444512 5957854 4629700 6070319 4814888 5000188 5000076 6651643 5185264 4854109 5370452 5385621 5555640 ...
output:
15000228 109
result:
ok 2 number(s): "15000228 109"
Test #11:
score: 0
Accepted
time: 4341ms
memory: 453988kb
input:
22 48889995 1216303 2222260 1359130 2415500 3584343 2608740 5573601 2801980 6497030 2995220 961106 3188460 7210397 3381700 7437984 3574940 3653310 3768180 8153230 3961420 9078129 4154660 9339995 4347900 9546227 4541140 5979651 4927620 8415111 5120860 2355876 5314100 5268961 5507340 5513979 5700580 5...
output:
14976100 7
result:
ok 2 number(s): "14976100 7"
Test #12:
score: 0
Accepted
time: 4355ms
memory: 454116kb
input:
22 48579995 995517 4416345 4162361 4608360 689494 4800375 1751672 4992390 3152622 5184405 6822783 5376420 9853978 5568435 8144298 5760450 403922 5952465 4762546 6144480 96738 6336495 3332168 6528510 3046767 6720525 612840 6912540 12718394 7104555 12544817 7296570 932656 7488585 13902077 7680600 1518...
output:
21505680 2766
result:
ok 2 number(s): "21505680 2766"
Test #13:
score: 0
Accepted
time: 4334ms
memory: 454080kb
input:
22 48579995 1487345 4416345 2506907 4608360 4972266 4800375 5406373 4992390 7889100 5184405 9894143 5376420 3260719 5568435 3583436 5760450 2820541 5952465 8370133 6144480 491071 6336495 2885341 6528510 2489830 6720525 2929470 6912540 6880314 7296570 4020302 7488585 1341652 7680600 13092883 7872615 ...
output:
21889710 2841
result:
ok 2 number(s): "21889710 2841"
Test #14:
score: 0
Accepted
time: 4324ms
memory: 454140kb
input:
22 48579995 2167618 4416345 8701572 4608360 8901658 4800375 2606668 4992390 10331331 5184405 473563 5376420 9425532 5568435 2521056 5760450 7409167 5952465 5163980 6144480 11569213 6336495 2178000 6528510 3109142 6720525 9789269 6912540 4418312 7104555 4345642 7296570 8296204 7680600 7135437 7872615...
output:
21121650 8
result:
ok 2 number(s): "21121650 8"
Test #15:
score: 0
Accepted
time: 4363ms
memory: 454004kb
input:
22 48579995 185663 4416345 6058344 4608360 331879 4800375 1065291 4992390 8966703 5184405 8105358 5376420 5217326 5568435 8799150 5760450 9027629 5952465 8032986 6144480 4436166 6336495 12199531 6528510 8750754 6720525 82844 7104555 1853547 7296570 5898163 7488585 1934865 7680600 1839565 7872615 633...
output:
20545605 26
result:
ok 2 number(s): "20545605 26"
Test #16:
score: 0
Accepted
time: 4345ms
memory: 454124kb
input:
22 48579995 15151 4416345 734549 4608360 7319925 4800375 2129913 4992390 853818 5376420 10141848 5568435 767573 5760450 3980988 5952465 3315870 6144480 2531255 6336495 5624435 6528510 6571293 6720525 40928 6912540 10363672 7104555 1284631 7296570 14701697 7488585 14642456 7680600 322573 7872615 1043...
output:
21697695 4991
result:
ok 2 number(s): "21697695 4991"
Test #17:
score: 0
Accepted
time: 4338ms
memory: 454192kb
input:
22 49898989 7739655 4536267 3933324 4733496 8591811 4930725 9087440 5325183 7835571 5522412 4318275 5719641 4661526 5916870 4200111 6114099 5057614 6311328 921304 6508557 4668507 6705786 5557323 6903015 4273093 7100244 6563270 7297473 9919216 7494702 3400922 7691931 11079804 7889160 100670 8086389 1...
output:
20906274 1
result:
ok 2 number(s): "20906274 1"
Test #18:
score: 0
Accepted
time: 4333ms
memory: 454068kb
input:
22 49898989 6061313 4536267 716781 4733496 305819 4930725 6315187 5127954 4257670 5325183 2230859 5522412 1085663 5719641 5327241 5916870 1902077 6114099 4171313 6311328 10963222 6508557 1747036 6903015 10948695 7100244 3677364 7297473 11817601 7494702 11781634 7691931 3295539 7889160 1325434 808638...
output:
20511816 50
result:
ok 2 number(s): "20511816 50"
Test #19:
score: 0
Accepted
time: 4385ms
memory: 454216kb
input:
22 49898979 1647035 4536250 175922 4717700 8292866 4899150 2096187 5080600 10066004 5262050 961380 5443500 1647976 5624950 5110920 5987850 2028528 6169300 8462757 6713650 1216165 6895100 1260925 7076550 8167307 7258000 7304251 7439450 11040257 7620900 5536709 7802350 4725071 7983800 3148347 8165250 ...
output:
21592550 54
result:
ok 2 number(s): "21592550 54"
Test #20:
score: 0
Accepted
time: 4383ms
memory: 454176kb
input:
22 49898919 4880960 3402175 5824531 3674349 2692142 3810436 219059 3946523 3955872 4082610 1597316 4354784 6797847 4490871 1286029 4626958 3989070 4763045 6096677 4899132 6378309 5035219 3025127 5171306 7099640 5307393 9936888 5443480 7942273 5579567 5288035 5715654 6910366 5851741 588733 5987828 88...
output:
15377831 2
result:
ok 2 number(s): "15377831 2"
Test #21:
score: 0
Accepted
time: 4329ms
memory: 454040kb
input:
22 49898919 5878918 3066492 6028670 3184434 1748377 3302376 3583403 3420318 61448 3538260 4692284 3656202 5129355 3774144 6440565 4010028 1002813 4127970 1728560 4245912 4721603 4363854 6446718 4481796 4118552 4599738 4479205 4717680 8610916 4835622 63195 4953564 9769813 5071506 5123156 5189448 4790...
output:
13445388 2
result:
ok 2 number(s): "13445388 2"
Test #22:
score: 0
Accepted
time: 4370ms
memory: 454156kb
input:
22 49898919 4757745 2721750 1190291 2830620 122673 2939490 5285455 3048360 5154097 3157230 3426786 3266100 2776798 3374970 6808419 3483840 7113299 3592710 4948131 3701580 4444440 3810450 1054709 3919320 4310982 4028190 8200366 4137060 1136653 4245930 4064806 4354800 3259693 4463670 5015241 4572540 8...
output:
12084570 61
result:
ok 2 number(s): "12084570 61"
Test #23:
score: 0
Accepted
time: 4332ms
memory: 454108kb
input:
22 49898919 4700657 2598900 3031551 2702856 2474511 2806812 5253209 2910768 4420840 3014724 5145346 3118680 2600255 3222636 1962472 3326592 6224012 3430548 2681629 3534504 2745527 3638460 5992867 3742416 2067411 3846372 6330981 3950328 5060577 4054284 6543128 4262196 5788423 4366152 5569899 4470108 ...
output:
11435160 17
result:
ok 2 number(s): "11435160 17"
Test #24:
score: 0
Accepted
time: 4341ms
memory: 454024kb
input:
22 49898919 3161907 2948554 1025254 3076752 5761371 3204950 508930 3333148 4685707 3461346 5326061 3589544 5702566 3717742 6533903 3845940 4153649 4102336 1132247 4230534 8560233 4358732 8679182 4486930 4205825 4615128 739891 4743326 7020873 4871524 6479109 4999722 5459256 5127920 6220335 5256118 33...
output:
13332592 3
result:
ok 2 number(s): "13332592 3"
Test #25:
score: 0
Accepted
time: 4344ms
memory: 454136kb
input:
22 49998920 5432634 2499936 863708 2604100 2752108 2708264 5483524 2812428 3143863 2916592 3862668 3020756 3252628 3124920 5204497 3229084 1249648 3333248 3953145 3541576 1507722 3645740 8393362 3749904 6439438 3854068 4362518 3958232 4233238 4062396 5523854 4166560 9201858 4270724 2455377 4374888 8...
output:
11145548 2
result:
ok 2 number(s): "11145548 2"
Test #26:
score: 0
Accepted
time: 4364ms
memory: 454344kb
input:
22 49998920 6166390 2727208 5195387 2851172 2483572 2975136 6640764 3099100 2033798 3223064 5969501 3347028 7968425 3470992 857128 3594956 6854182 3718920 2064030 3842884 8893787 3966848 8849735 4090812 743604 4214776 2540982 4338740 7410235 4462704 2247651 4586668 785337 4710632 8843312 4834596 508...
output:
12024508 5
result:
ok 2 number(s): "12024508 5"
Test #27:
score: 0
Accepted
time: 4376ms
memory: 454236kb
input:
22 49998920 1851866 2499926 1743639 2613559 58569 2727192 6420362 2840825 3807544 2954458 2258556 3068091 7054799 3181724 3607029 3295357 5664438 3408990 7048880 3522623 6084252 3636256 4253765 3749889 3691283 3863522 4746341 3977155 4213709 4090788 5646732 4204421 8240035 4318054 9886432 4431687 48...
output:
11249667 4
result:
ok 2 number(s): "11249667 4"
Test #28:
score: 0
Accepted
time: 4347ms
memory: 454296kb
input:
22 15 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
output:
4 736056326
result:
ok 2 number(s): "4 736056326"
Test #29:
score: 0
Accepted
time: 4283ms
memory: 454092kb
input:
22 50000000 25000001 100001 25000002 100002 25000003 100003 1 10001 2 10002 3 10003 4 10004 5 10005 6 10006 7 10007 8 10008 9 10009 10 10010 11 10011 12 10012 13 10013 14 10014 15 10015 16 10016 17 10017 18 10018 19 10019
output:
300006 164017114
result:
ok 2 number(s): "300006 164017114"
Extra Test:
score: 0
Extra Test Passed