結果
問題 | No.2457 Stampaholic (Easy) |
ユーザー | Aeren |
提出日時 | 2023-09-02 02:33:51 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,696 ms / 4,000 ms |
コード長 | 10,872 bytes |
コンパイル時間 | 4,124 ms |
コンパイル使用メモリ | 367,304 KB |
実行使用メモリ | 190,976 KB |
最終ジャッジ日時 | 2024-06-11 09:13:07 |
合計ジャッジ時間 | 25,555 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1,251 ms
50,304 KB |
testcase_02 | AC | 234 ms
23,424 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2,696 ms
190,848 KB |
testcase_06 | AC | 1,567 ms
147,328 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 245 ms
24,960 KB |
testcase_09 | AC | 134 ms
15,488 KB |
testcase_10 | AC | 435 ms
38,528 KB |
testcase_11 | AC | 600 ms
51,584 KB |
testcase_12 | AC | 989 ms
81,536 KB |
testcase_13 | AC | 1,298 ms
100,992 KB |
testcase_14 | AC | 2,209 ms
168,704 KB |
testcase_15 | AC | 2,696 ms
190,848 KB |
testcase_16 | AC | 2,694 ms
190,976 KB |
testcase_17 | AC | 2,532 ms
175,488 KB |
testcase_18 | AC | 161 ms
17,664 KB |
testcase_19 | AC | 4 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 607 ms
64,896 KB |
ソースコード
#include <bits/stdc++.h> #include <x86intrin.h> using namespace std; using namespace numbers; using uint = unsigned int; template<uint _mod> struct modular_fixed_base{ static constexpr uint mod(){ return _mod; } template<class T> static vector<modular_fixed_base> precalc_power(T base, int SZ){ vector<modular_fixed_base> res(SZ + 1, 1); for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base; return res; } static vector<modular_fixed_base> _INV; static void precalc_inverse(int SZ){ if(_INV.empty()) _INV.assign(2, 1); for(auto x = _INV.size(); x <= SZ; ++ x) _INV.push_back(_mod / x * -_INV[_mod % x]); } // _mod must be a prime static modular_fixed_base _primitive_root; static modular_fixed_base primitive_root(){ if(_primitive_root) return _primitive_root; if(_mod == 2) return _primitive_root = 1; if(_mod == 998244353) return _primitive_root = 3; uint divs[20] = {}; divs[0] = 2; int cnt = 1; uint x = (_mod - 1) / 2; while(x % 2 == 0) x /= 2; for(auto i = 3; 1LL * 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(auto g = 2; ; ++ g){ bool ok = true; for(auto i = 0; i < cnt; ++ i){ if((modular_fixed_base(g).power((_mod - 1) / divs[i])) == 1){ ok = false; break; } } if(ok) return _primitive_root = g; } } constexpr modular_fixed_base(): data(){ } modular_fixed_base(const double &x){ data = normalize(llround(x)); } modular_fixed_base(const long double &x){ data = normalize(llround(x)); } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base(const T &x){ data = normalize(x); } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> static uint normalize(const T &x){ int sign = x >= 0 ? 1 : -1; uint v = _mod <= sign * x ? sign * x % _mod : sign * x; if(sign == -1 && v) v = _mod - v; return v; } const uint &operator()() const{ return data; } template<class T> operator T() const{ return data; } modular_fixed_base &operator+=(const modular_fixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; } modular_fixed_base &operator-=(const modular_fixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base &operator+=(const T &otr){ return *this += modular_fixed_base(otr); } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base &operator-=(const T &otr){ return *this -= modular_fixed_base(otr); } modular_fixed_base &operator++(){ return *this += 1; } modular_fixed_base &operator--(){ return *this += _mod - 1; } modular_fixed_base operator++(int){ modular_fixed_base result(*this); *this += 1; return result; } modular_fixed_base operator--(int){ modular_fixed_base result(*this); *this += _mod - 1; return result; } modular_fixed_base operator-() const{ return modular_fixed_base(_mod - data); } modular_fixed_base &operator*=(const modular_fixed_base &rhs){ data = (unsigned long long)data * rhs.data % _mod; return *this; } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base &inplace_power(T e){ if(e < 0) *this = 1 / *this, e = -e; modular_fixed_base res = 1; for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this; return *this = res; } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base power(T e) const{ return modular_fixed_base(*this).inplace_power(e); } modular_fixed_base &operator/=(const modular_fixed_base &otr){ int a = otr.data, m = _mod, u = 0, v = 1; if(a < _INV.size()) return *this *= _INV[a]; while(a){ int t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return *this *= u; } uint data; }; template<uint _mod> vector<modular_fixed_base<_mod>> modular_fixed_base<_mod>::_INV; template<uint _mod> modular_fixed_base<_mod> modular_fixed_base<_mod>::_primitive_root; template<uint _mod> bool operator==(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return lhs.data == rhs.data; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator==(const modular_fixed_base<_mod> &lhs, T rhs){ return lhs == modular_fixed_base<_mod>(rhs); } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator==(T lhs, const modular_fixed_base<_mod> &rhs){ return modular_fixed_base<_mod>(lhs) == rhs; } template<uint _mod> bool operator!=(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return !(lhs == rhs); } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator!=(const modular_fixed_base<_mod> &lhs, T rhs){ return !(lhs == rhs); } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator!=(T lhs, const modular_fixed_base<_mod> &rhs){ return !(lhs == rhs); } template<uint _mod> bool operator<(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return lhs.data < rhs.data; } template<uint _mod> bool operator>(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return lhs.data > rhs.data; } template<uint _mod> bool operator<=(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return lhs.data <= rhs.data; } template<uint _mod> bool operator>=(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return lhs.data >= rhs.data; } template<uint _mod> modular_fixed_base<_mod> operator+(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return modular_fixed_base<_mod>(lhs) += rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator+(const modular_fixed_base<_mod> &lhs, T rhs){ return modular_fixed_base<_mod>(lhs) += rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator+(T lhs, const modular_fixed_base<_mod> &rhs){ return modular_fixed_base<_mod>(lhs) += rhs; } template<uint _mod> modular_fixed_base<_mod> operator-(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return modular_fixed_base<_mod>(lhs) -= rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator-(const modular_fixed_base<_mod> &lhs, T rhs){ return modular_fixed_base<_mod>(lhs) -= rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator-(T lhs, const modular_fixed_base<_mod> &rhs){ return modular_fixed_base<_mod>(lhs) -= rhs; } template<uint _mod> modular_fixed_base<_mod> operator*(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs){ return modular_fixed_base<_mod>(lhs) *= rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator*(const modular_fixed_base<_mod> &lhs, T rhs){ return modular_fixed_base<_mod>(lhs) *= rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator*(T lhs, const modular_fixed_base<_mod> &rhs){ return modular_fixed_base<_mod>(lhs) *= rhs; } template<uint _mod> modular_fixed_base<_mod> operator/(const modular_fixed_base<_mod> &lhs, const modular_fixed_base<_mod> &rhs) { return modular_fixed_base<_mod>(lhs) /= rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator/(const modular_fixed_base<_mod> &lhs, T rhs) { return modular_fixed_base<_mod>(lhs) /= rhs; } template<uint _mod, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_fixed_base<_mod> operator/(T lhs, const modular_fixed_base<_mod> &rhs) { return modular_fixed_base<_mod>(lhs) /= rhs; } template<uint _mod> istream &operator>>(istream &in, modular_fixed_base<_mod> &number){ long long x; in >> x; number.data = modular_fixed_base<_mod>::normalize(x); return in; } #define _PRINT_AS_FRACTION template<uint _mod> ostream &operator<<(ostream &out, const modular_fixed_base<_mod> &number){ #if defined(LOCAL) && defined(_PRINT_AS_FRACTION) out << number(); cerr << "("; for(auto d = 1; ; ++ d){ if((number * d).data <= 1000000){ cerr << (number * d).data << "/" << d; break; } else if((-number * d).data <= 1000000){ cerr << "-" << (-number * d).data << "/" << d; break; } } cerr << ")"; return out; #else return out << number(); #endif } #undef _PRINT_AS_FRACTION // const uint mod = 1e9 + 7; // 1000000007 const uint mod = (119 << 23) + 1; // 998244353 // const uint mod = 1e9 + 9; // 1000000009 using modular = modular_fixed_base<mod>; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int nr, nc, n, k; cin >> nr >> nc >> n >> k; if(nr > nc){ swap(nr, nc); } auto inv = modular(1) / (nr - (k - 1)) / (nc - (k - 1)); modular res = 0; { set<array<int, 2>> done; auto update = [&](int x, int y)->void{ if(done.contains({x, y})){ return; } done.insert({x, y}); auto delta = 1 - (1 - inv * (clamp(x + 1, 0, nr - k + 1) - clamp(x - k + 1, 0, nr - k + 1)) * (clamp(y + 1, 0, nc - k + 1) - clamp(y - k + 1, 0, nc - k + 1))).power(n); res += delta; }; for(auto x = 0; x < k; ++ x){ for(auto y = 0; y < k; ++ y){ update(x, y); } } for(auto x = 0; x < k; ++ x){ for(auto y = nc - k; y < nc; ++ y){ update(x, y); } } for(auto x = nr - k; x < nr; ++ x){ for(auto y = 0; y < k; ++ y){ update(x, y); } } for(auto x = nr - k; x < nr; ++ x){ for(auto y = nc - k; y < nc; ++ y){ update(x, y); } } } if(2 * k <= nr){ for(auto x = 0; x < k; ++ x){ res += (1 - (1 - inv * (x + 1) * k).power(n)) * 2 * (nr + nc - 4 * k); } res += (1 - (1 - inv * k * k).power(n)) * (nr - 2 * k) * (nc - 2 * k); } else if(2 * k <= nc){ for(auto x = 0; x < nr; ++ x){ res += (1 - (1 - inv * (clamp(x + 1, 0, nr - k + 1) - clamp(x - k + 1, 0, nr - k + 1)) * k).power(n)) * (nc - 2 * k); } } cout << res << "\n"; return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////