結果
| 問題 |
No.3044 よくあるカエルさん
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2025-03-02 18:24:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 2,000 ms |
| コード長 | 8,918 bytes |
| コンパイル時間 | 3,998 ms |
| コンパイル使用メモリ | 147,260 KB |
| 実行使用メモリ | 8,608 KB |
| 最終ジャッジ日時 | 2025-03-02 18:24:25 |
| 合計ジャッジ時間 | 5,211 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 20 |
ソースコード
#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 <atcoder/convolution>
#include <cassert>
#include <utility>
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{
class DynamicModSupplier{
using u64 = unsigned long long;
using Int = unsigned int;
private:
u64 imod;
Int mod;
// atcoder library
u64 reduce2(u64 z) const noexcept {
// atcoder library
#ifdef _MSC_VER
u64 x; _umul128(z, im, &x);
#else
using u128 = unsigned __int128;
u64 x = (u64)(((u128)(z)*imod) >> 64);
#endif
return z - x * mod;
}
public:
DynamicModSupplier(unsigned int MOD = 998244353) : mod(MOD) {
assert(2 <= MOD);
assert(MOD < (1u << 31));
imod = (u64)(-1) / mod + 1;
}
Int reduce(u64 z) const noexcept {
Int v = reduce2(z);
if(mod <= v) v += mod;
return v;
}
Int add(Int a, Int b) const { a += b; if(a >= mod){ a -= mod; } return a; }
Int sub(Int a, Int b) const { a -= b; if(a >= mod){ a += mod; } return a; }
Int mul(Int a, Int b) const { return reduce((u64)a * b); }
Int muladd(Int a, Int b, Int c) const { return reduce((u64)a * b + c); }
Int inv(Int a) const {
Int v = ExtGcd(a, mod).first;
return (v < mod) ? v : (v + mod);
}
Int pow(Int a, u64 i) const {
Int r = a, ans = 1;
while(i){
if(i & 1) ans = mul(ans, r);
i /= 2;
r = mul(r, r);
}
return ans;
}
Int getMod() const { return mod; }
};
} // namespace nachia
namespace nachia{
template<class FinishType>
struct GarnerMod{
using Int = unsigned int;
using IntLong = unsigned long long;
std::vector<Int> mods;
std::vector<DynamicModSupplier> dynmods;
std::vector<std::vector<Int>> table_coeff;
std::vector<Int> table_coeffinv;
void precalc(std::vector<Int> new_mods){
mods = std::move(new_mods);
dynmods.resize(mods.size());
for(size_t i=0; i<mods.size(); i++) dynmods[i] = DynamicModSupplier(mods[i]);
int nmods = mods.size();
table_coeff.assign(nmods+1, std::vector<Int>(nmods, 1));
for(int j=0; j<nmods; j++){
for(int k=0; k<nmods; k++) table_coeff[j+1][k] = table_coeff[j][k];
for(int k=j+1; k<nmods; k++) table_coeff[j+1][k] = dynmods[k].mul(table_coeff[j+1][k], mods[j] % mods[k]);
}
table_coeffinv.resize(nmods);
for(int i=0; i<nmods; i++) table_coeffinv[i] = dynmods[i].inv(table_coeff[i][i]);
}
FinishType calc(const std::vector<Int>& x) const {
int nmods = mods.size();
std::vector<Int> table_const(nmods);
FinishType res = 0;
FinishType res_coeff = 1;
for(int j=0; j<nmods; j++){
Int t = dynmods[j].mul(dynmods[j].sub(x[j], table_const[j]), table_coeffinv[j]);
for(int k=j+1; k<nmods; k++){
table_const[k] = dynmods[k].muladd(t, table_coeff[j][k], table_const[k]);
}
res += res_coeff * FinishType(t);
res_coeff *= mods[j];
}
return res;
}
std::vector<FinishType> calc(std::vector<std::vector<Int>> x) const {
int n = x[0].size(), m = x.size();
std::vector<FinishType> res(n);
std::vector<Int> buf(m);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++) buf[j] = x[j][i];
res[i] = calc(buf);
}
return res;
}
};
} // namespace nachia
namespace nachia{
template<class Modint, unsigned int nttmod> std::vector<unsigned int>
PartConvolution(std::vector<Modint> A, std::vector<Modint> B)
{
std::vector<atcoder::static_modint<nttmod>> AA(A.size());
for(std::size_t i=0; i<A.size(); i++) AA[i] = A[i].val();
std::vector<atcoder::static_modint<nttmod>> BB(B.size());
for(std::size_t i=0; i<B.size(); i++) BB[i] = B[i].val();
auto AB = atcoder::convolution(AA, BB);
std::vector<unsigned int> res(AB.size());
for(std::size_t i=0; i<AB.size(); i++) res[i] = AB[i].val();
return res;
}
template<class Modint>
std::vector<Modint> Convolution(std::vector<Modint> A, std::vector<Modint> B){
auto Q1 = PartConvolution<Modint, 998244353>(A, B);
auto Q2 = PartConvolution<Modint, 897581057>(A, B);
auto Q3 = PartConvolution<Modint, 880803841>(A, B);
GarnerMod<Modint> garner;
garner.precalc({ 998244353, 897581057, 880803841 });
return garner.calc({ Q1, Q2, Q3 });
}
} // namespace nachia
namespace nachia{
template<class Modint>
Modint KthTermOfRationalGF(
std::vector<Modint> denom,
std::vector<Modint> numer,
unsigned long long K
){
assert(denom.size() != 0);
assert(denom.size() == numer.size());
assert(denom[0].val() != 0);
int n = (int)denom.size();
while(K != 0){
auto Qn = denom;
Qn.push_back(Modint(0));
for(int i=1; i<n; i+=2) Qn[i] = -Qn[i];
int f = K % 2;
denom = Convolution(denom, Qn);
for(int i=0; i<n; i++) denom[i] = denom[i*2];
denom.resize(n);
numer = Convolution(numer, Qn);
for(int i=0; i<n; i++) numer[i] = numer[i*2+f];
numer.resize(n);
K /= 2;
}
return numer[0] / denom[0];
}
// divisor of fractional representation
// and first terms
template<class Modint>
Modint KthTermOfLinearRecurrence(
std::vector<Modint> denom,
std::vector<Modint> firstTerms,
unsigned long long K
){
assert(denom.size() <= firstTerms.size());
firstTerms.resize(denom.size());
auto numer = Convolution(firstTerms, denom);
numer.resize(denom.size());
return KthTermOfRationalGF(std::move(denom), std::move(numer), K);
}
} // 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>;
void testcase(){
i64 N, T; cin >> N >> T;
i64 K, L; cin >> K >> L;
vector<Modint> A(T+1);
A[0] = 1;
A[1] = -Modint(K-1) / 6;
A[2] = -Modint(L-K) / 6;
A[T] = -Modint(6-L+1) / 6;
vector<Modint> D(T+1); D[0] = 1;
auto res = nachia::KthTermOfRationalGF(A, D, N-1);
cout << res.val() << "\n";
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
testcase();
return 0;
}
Nachia