結果
問題 |
No.2996 Floor Sum
|
ユーザー |
👑 ![]() |
提出日時 | 2025-04-23 01:07:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 431 ms / 5,000 ms |
コード長 | 5,858 bytes |
コンパイル時間 | 1,157 ms |
コンパイル使用メモリ | 86,216 KB |
実行使用メモリ | 7,844 KB |
最終ジャッジ日時 | 2025-04-23 01:08:01 |
合計ジャッジ時間 | 3,107 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 12 |
ソースコード
#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include <iostream> #include <string> #include <vector> #include <algorithm> using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i<i64(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 <utility> #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 { return x; } my_type& operator+=(const my_type& r) { auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; } my_type operator+(const my_type& r) const { my_type res = *this; return res += r; } my_type& operator-=(const my_type& r) { auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; } my_type operator-(const my_type& r) const { 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){ x = (u64)x * r.x % MOD; return *this; } my_type operator*(const my_type& r) const { my_type res = *this; return res *= r; } bool operator==(const my_type& r) const { return x == r.x; } my_type pow(unsigned long long i) const { 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 { return x; } int hval() const { return int(x > MOD/2 ? x - MOD : x); } static constexpr unsigned int mod() { return MOD; } static my_type raw(unsigned int val) { 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>; struct Unit { i64 x; i64 y; i64 P; i64 Q; vector<Modint> A; Modint& at(i64 p, i64 q){ return A[p * Q + q]; } Modint at(i64 p, i64 q) const { return A[p * Q + q]; } }; vector<Modint> ifact, fact; Unit shiftX(const Unit& r, i64 x){ i64 P = r.P, Q = r.Q; Unit res = { r.x, r.y, P, Q, vector<Modint>(P*Q) }; vector<Modint> XP(P,1); rep(i,P-1) XP[i+1] = XP[i] * (x-i) / (i+1); rep(p,P) rep(q,Q) rep(pp,p+1) res.at(p,q) += r.at(pp,q) * XP[p-pp]; return res; } Unit shiftY(const Unit& r, i64 x){ i64 P = r.P, Q = r.Q; Unit res = { r.x, r.y, P, Q, vector<Modint>(P*Q) }; vector<Modint> XP(Q,1); rep(i,Q-1) XP[i+1] = XP[i] * (x-i) / (i+1); rep(p,P) rep(q,Q) rep(qq,q+1) res.at(p,q) += r.at(p,qq) * XP[q-qq]; return res; } Unit merge(const Unit& l, const Unit& r){ i64 P = min(l.P, r.P), Q = min(l.Q, r.Q); Unit res = { l.x + r.x, l.y + r.y, P, Q }; res.A.resize(P * Q); rep(p,P) rep(q,Q) res.at(p,q) = r.at(p,q); res = shiftX(res, l.x); res = shiftY(res, l.y); rep(p,P) rep(q,Q) res.at(p,q) += l.at(p,q); return res; } Unit pow(const Unit& a, i64 i){ if(i == 0) return Unit{ 0, 0, a.P, a.Q, vector<Modint>(a.P*a.Q) }; if(i == 1) return a; auto r = pow(merge(a,a), i/2); return (i%2 == 1) ? merge(a,r) : r; } void testcase(){ i64 P, Q, N, M, A, B; cin >> P >> Q >> N >> M >> A >> B; i64 offset_B = B / M; B %= M; if(B < 0){ offset_B -= 1; B += M; } i64 neg = A < 0 ? -1 : 1; if(neg == -1){ B = M - 1 - B; A = -A; } N += 1; Unit X = Unit{ 1, 0, P+1, Q+1, vector<Modint>((P+1)*(Q+1)) }; Unit Y = Unit{ 0, neg, P+1, Q+1, vector<Modint>((P+1)*(Q+1)) }; X.at(0,0) = 1; Unit L = Unit{ 0, 0, P+1, Q+1, vector<Modint>((P+1)*(Q+1)) }; Unit R = Unit{ 0, 0, P+1, Q+1, vector<Modint>((P+1)*(Q+1)) }; i64 C = (A * N + B) / M; while(true){ i64 qa = A / M; A %= M; i64 qb = B / M; B %= M; X = merge(X, pow(Y, qa)); L = merge(L, pow(Y, qb)); C -= qa * N + qb; if(C == 0){ L = merge(merge(L, pow(X, N)), R); break; } i64 D = (M * C - B - 1) / A + 1; R = merge(merge(Y, pow(X, N - D)), R); B = M - B - 1 + A; swap(M, A); N = C - 1; C = D; swap(X, Y); } L = shiftY(L, offset_B); vector<Modint> F(P+1); F[0] = 1; rep(i,P) for(i64 j=i; j>=0; j--){ F[j+1] += F[j]; F[j] *= j; } vector<Modint> G(Q+1); G[0] = 1; rep(i,Q) for(i64 j=i; j>=0; j--){ G[j+1] += G[j]; G[j] *= j; } Modint ans = 0; rep(i,P+1) rep(j,Q+1) ans += L.at(i, j) * fact[i] * fact[j] * F[i] * G[j]; cout << ans.val() << "\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); i64 Z = 100; ifact.assign(Z+1, 1); for(i64 i=1; i<=Z; i++) ifact[i] = ifact[i-1] / i; fact.assign(Z+1, 1); for(i64 i=1; i<=Z; i++) fact[i] = fact[i-1] * i; i64 T; cin >> T; rep(t,T) testcase(); return 0; }