generated from Tiphereth-A/TINplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ea881b
commit 46c8a45
Showing
51 changed files
with
1,026 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#ifndef TIFALIBS_COMB_FACTL_HELPER | ||
#define TIFALIBS_COMB_FACTL_HELPER | ||
|
||
#include "../poly/ctsh_fps.hpp" | ||
#include "fact_helper.hpp" | ||
|
||
namespace tifa_libs::math { | ||
|
||
template <class poly_t> | ||
struct factl_helper { | ||
using val_t = TPN poly_t::val_t; | ||
using base_t = fact_helper<TPN poly_t::val_t>; | ||
static CEXP u32 threshold = 2'000'001; | ||
|
||
factl_helper() = delete; | ||
|
||
static CEXP val_t get_fact(u64 n) NE { | ||
if (n >= base_t::mod()) [[unlikely]] | ||
return 0; | ||
if (base_t::fact.empty()) [[unlikely]] | ||
base_t::ensure(threshold); | ||
if (n < threshold) return base_t::fact[n]; | ||
return fact_(n); | ||
} | ||
static CEXP val_t get_ifact(u64 n) NE { | ||
if (n >= base_t::mod()) [[unlikely]] | ||
return 0; | ||
if (base_t::fact.empty()) [[unlikely]] | ||
base_t::ensure(threshold); | ||
if (n < threshold) return base_t::ifact[n]; | ||
return fact_(n).inv(); | ||
} | ||
|
||
private: | ||
static CEXP u32 LBSZ = 9; | ||
static CEXP u32 SZ = 1 << LBSZ; | ||
static inline u64 B = base_t::mod() >> LBSZ; | ||
// f[i] = (i*B + 1) * ... * (i*B + B) | ||
static inline poly_t f{1}; | ||
static val_t fact_(u64 n) NE { | ||
static bool inited = false; | ||
if (!inited) { | ||
inited = true; | ||
f.reserve(SZ); | ||
flt_ (u32, i, 0, LBSZ) { | ||
poly_t g = ctsh_fps(f, val_t(1 << i), base_t::ifact, 3_u32 << i); | ||
const auto get = [&](u32 j) { return j < (1_u32 << i) ? f[j] : g[j - (1 << i)]; }; | ||
f.resize(2_u32 << i); | ||
flt_ (u32, j, 0, 2_u32 << i) f[j] = get(2 * j) * get(2 * j + 1) * ((2 * j + 1) << i); | ||
} | ||
if (B > SZ) { | ||
vec<val_t> g = ctsh_fps(f, val_t(SZ), base_t::ifact, u32(B - SZ)); | ||
std::ranges::move(g, std::back_inserter(f)); | ||
} else f.resize(B); | ||
flt_ (u32, i, 0, (u32)B) f[i] *= val_t(i + 1) * SZ; | ||
f.insert(f.begin(), 1); | ||
flt_ (u32, i, 0, (u32)B) f[i + 1] *= f[i]; | ||
} | ||
val_t res; | ||
u64 q = n / SZ; | ||
u32 r = n % SZ; | ||
if (2 * r <= SZ) { | ||
res = f[q]; | ||
flt_ (u32, i, 0, r) res *= n - i; | ||
} else if (q != B) { | ||
res = f[q + 1]; | ||
val_t den = 1; | ||
flt_ (u32, i, 1, SZ - r + 1) den *= n + i; | ||
res /= den; | ||
} else { | ||
res = base_t::mod() - 1; | ||
val_t den = 1; | ||
for (u64 i = base_t::mod() - 1; i > n; --i) den *= i; | ||
res /= den; | ||
} | ||
return res; | ||
} | ||
}; | ||
|
||
} // namespace tifa_libs::math | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: factl_helper | ||
documentation_of: //src/code/comb/factl_helper.hpp | ||
--- |
Empty file.
29 changes: 29 additions & 0 deletions
29
src/meta_test/library-checker-enumerative_combinatorics/factorial.factl-helper.cppmeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#define AUTO_GENERATED | ||
#define PROBLEM "https://judge.yosupo.jp/problem/factorial" | ||
|
||
#include "../../code/comb/factl_helper.hpp" | ||
|
||
CEXP u32 MOD = 998244353; | ||
|
||
#define GENTCs_p3ntts30s | ||
#define GENTCs_p3ntts63s | ||
#define GENTCs_p3ntts30d_0 | ||
#define GENTCs_p3ntts63d_0 | ||
#define GENTCs_pmtts | ||
#define GENTCs_pmttd0 | ||
#define GENTCs_pntt | ||
|
||
int main() { | ||
#define GENTCs_p3ntts30d_1 | ||
#define GENTCs_p3ntts63d_1 | ||
#define GENTCs_pmttd1 | ||
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); | ||
u32 t; | ||
std::cin >> t; | ||
while (t--) { | ||
u32 n; | ||
std::cin >> n; | ||
std::cout << tifa_libs::math::factl_helper<poly>::get_fact(n) << '\n'; | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.