#define SKIP_RUNTIME_CHECKS // Verify: https://yukicoder.me/problems/no/2583 // Library: Math/Polynomial_Differential_Operator.cpp // SPDX-License-Identifier: CC0-1.0 // ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/polynomial_differential_operator.md // 参考(微分を含む遷移): https://info.atcoder.jp/entry/algorithm_lectures/eigenfunction // // API一覧 // polynomial_differential_operator_power(f,p,a,t,n): g↦p(x)g′(x)+a(x)g(x)をfへt回適用し、先頭n係数を返す。 // 法998244353。t>0かつn>0ならp(0)≠0。N=t+n<=2^18。n=0なら空、t=0ならfの先頭n係数。 // O(N log²(N+1))時間、O(N log(N+1))領域。f,p,aのx^N以上の項は不要。 // SIRKIT_BUNDLED_DEPENDENCIES // SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。 #include #include #include #include #include #include // // [同梱の形式的冪級数の合成] // fps_compose(outer,inner,precision): outer(inner(x))をx^precision未満で切る。innerの定数項は0。 // fps_compositional_inverse(value,precision): value(g(x))=x mod x^precisionを満たすg。 // 逆合成ではvalueの定数項0・一次係数非零。精度0なら空。精度Nは2^18以下、法998244353。 // 合成と逆合成は各O(N log²(N+1))時間、O(N log(N+1))領域。 // fps_power_projection(value,weight,degree,count): k=0..count-1の[x^degree]weight(x)*value(x)^k。 // degree<2^18、count<=2^18。D=degree+1、C=countとしてO(D log²(D+1)+C log(C+1))時間、O(D+C)領域。 // // [同梱FPS/NTT] // FormalPowerSeries: 法998244353、Mint不要の形式的冪級数/多項式。係数はuint32_tの独立所有vector。 // ()/size_t n/vector<整数>/initializer_list: 空/n個の0/法へ正規化した係数で生成。コピーは独立。 // modulus()/max_precision(): 法/精度操作の上限をO(1)で返す。 // size()/empty()/coefficients(): 係数数/空か/係数vectorのconst参照。operator[](i)/set(i,value): 既存添字の読取/正規化した代入。 // get(i): 範囲外は0。resize(n): 末尾を0で拡張/切詰め。prefix(n): n係数へ0拡張/切詰めしたコピー。 // trim(): 末尾の0を除く。空vectorは零多項式。長さを含めて等値比較するため、精度の異なる0列は区別する。 // +,-,*,/,%, +=,-=,*=,/=,%=: 係数和/差/積/多項式商/剰余。和・差は長い方の長さ、積は空を含めば空。 // scale(value): 全係数を整数倍。derivative()/integral(): 微分/積分(積分定数0)。積分は係数数<998244353が必要。 // inverse(n)/log(n)/exp(n): x^n未満で逆数/形式log/形式exp。inverseは定数項!=0、logは=1、expは=0。 // pow(k,n): 非負uint64_t乗をx^nで切る。0^0=1、先頭0と大きいkを扱う。sqrt(n): 平方根かnullopt。 // sqrtは先頭非0次数が偶数かつ定数部分が平方剰余なら存在。根の先頭非0係数は2候補の小さい方、未確定末尾は0。 // inverse/log/exp/pow/sqrtの引数n省略は現在のsize()。n=0は常に空、精度nは2^22以下。 // divmod(divisor): 末尾0を除いた商/剰余のpair。0除算は検査省略時もdomain_error。 // 線形操作はO(n)。精度操作はM(n)=O(n log n)としてO(M(n))、冪乗は加えてO(log k)。作業領域O(n)。 // 積と商剰余のNTT長は2^23以下。短い積/除数では直接計算。長さ・数学的0判定は検査省略時も維持。 // clear_cache(): このスレッドのNTT根/整数逆数cacheを解放。cacheは同一スレッドで共有し、係数は共有しない。 // // [同梱NTT] // NTT: Mint不要の素数法NTT。3<=Mod<2^31、Rootは最大2冪位数の根を生成する値。 // NTT(): 動的キャッシュなしで生成。コピーはキャッシュを独立に所有し、時間・領域は保持数に比例。 // modulus()/max_length(): 法/対応する最大変換長をO(1)で返す。 // prepare(length): 必要な根を保持。lengthは0または2冪、上限は2^v2(Mod-1)。新規分O(length)時間・保持O(length)。 // transform(a,inverse=false): 正順の係数と正順の周波数を相互変換しvoidを返す。aはuint32_tのvector。 // transformは空または対応する2冪長、各値0..Mod-1が前提。O(n log n)時間、根以外の追加領域O(1)。 // convolution(a,b): 整数vectorを法Modへ正規化し畳み込みを返す。負数/uint64_t最大値も可。空を含めば{}。 // convolution_normalized(a,b): uint32_tの正規化済みvectorを値で受け取り再利用。std::moveで入力領域を譲渡可。 // square(a): 同じ列の畳み込み。大規模では順変換を1回にする。返却値はvector。 // 畳み込みは短い方32要素以下ならO(nm)、それ以外はL=bit_ceil(n+m-1)としてO(L log L)、追加O(L)。 // NTT経路のLとtransform長は上限以下。長さ・容量の上限検査はSKIP_RUNTIME_CHECKSでも維持。 // ConvolutionMod(a,b): 一時NTTを使う簡易版。同じ長さを繰り返すならNTTオブジェクトを再利用する。 // roots_capacity()/clear_cache(): 動的な根のキャッシュの要素数を返す/解放。通常操作は入力不変、transformは例外までの変更が残り得る。 // Mod<2^30では標準C++のMontgomery演算を使用。GCC/Clangの対応x86 CPUではAVX2で8係数ずつ処理する。 // SIRKIT_DISABLE_SIMDで追加の専用命令とCPU判定を除外できる。代替経路も同じAPI・結果を保つ。 #include #include #include #include #include #include #include #include #include #include #include #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) #include #endif // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { #ifndef SIRKIT_NTT_DETAIL_DEFINED #define SIRKIT_NTT_DETAIL_DEFINED namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ { consteval bool ntt_prime(std::uint32_t n) noexcept { if (n < 2 || n % 2 == 0) return n == 2; for (std::uint32_t d = 3; d <= n / d; d += 2) if (n % d == 0) return false; return true; } consteval std::uint32_t ntt_power(std::uint32_t a, std::uint32_t exponent, std::uint32_t mod) noexcept { std::uint32_t result = 1; for (; exponent != 0; exponent >>= 1) { if (exponent & 1) result = static_cast(std::uint64_t{result} * a % mod); a = static_cast(std::uint64_t{a} * a % mod); } return result; } inline std::size_t convolution_result_size(std::size_t a, std::size_t b, const char* overflow_message) { if (a == 0 || b == 0) return 0; if (a - 1 > std::numeric_limits::max() - b) throw std::length_error(overflow_message); return a - 1 + b; } } #endif #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) #ifndef SIRKIT_NTT_SIMD_DETAIL_DEFINED #define SIRKIT_NTT_SIMD_DETAIL_DEFINED namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ { inline bool ntt_avx2_available() noexcept { static const bool available=__builtin_cpu_supports("avx2"); return available; } } #endif #endif #ifndef SIRKIT_NTT_DEFINED #define SIRKIT_NTT_DEFINED template class NTT { static_assert(Mod >= 3 && Mod < (std::uint32_t{1} << 31) && /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::ntt_prime(Mod), "NTT modulus must be an odd prime below 2^31"); static_assert(Root > 0 && Root < Mod, "NTT root outside modulus"); static constexpr int rank_ = std::countr_zero(Mod - 1); static constexpr std::uint32_t highest_root_ = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::ntt_power(Root, (Mod - 1) >> rank_, Mod); static_assert(/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::ntt_power(highest_root_, std::uint32_t{1} << (rank_ - 1), Mod) == Mod - 1, "NTT root has insufficient power-of-two order"); std::vector roots_; static constexpr std::uint32_t multiply(std::uint32_t a, std::uint32_t b) noexcept { return static_cast(std::uint64_t{a} * b % Mod); } static constexpr std::uint32_t add(std::uint32_t a, std::uint32_t b) noexcept { const auto sum = a + b; return sum >= Mod ? sum - Mod : sum; } static constexpr std::uint32_t subtract(std::uint32_t a, std::uint32_t b) noexcept { return a >= b ? a - b : a + Mod - b; } static constexpr bool montgomery_ = Mod < (std::uint32_t{1} << 30); static constexpr std::uint32_t negative_inverse_ = [] { std::uint32_t x=Mod;for(int i=0;i<5;++i)x*=2u-Mod*x;return 0u-x; }(); static constexpr std::uint32_t montgomery_one_ = static_cast((std::uint64_t{1} << 32)%Mod); static constexpr std::uint32_t r_squared_ = static_cast(std::uint64_t{montgomery_one_}*montgomery_one_%Mod); static constexpr std::array stage_roots_ = [] { std::array table{}; table[rank_] = highest_root_; for (int k = rank_; k >= 2; --k) table[k - 1] = static_cast(std::uint64_t{table[k]} * table[k] % Mod); if constexpr (montgomery_) { for (auto& value : table) { const auto product = std::uint64_t{value} * r_squared_; const auto q = static_cast(std::uint64_t{static_cast(product)} * negative_inverse_); value = static_cast((product + std::uint64_t{q} * Mod) >> 32); } } return table; }(); static constexpr std::uint32_t working_multiply(std::uint32_t a,std::uint32_t b) noexcept { if constexpr(montgomery_) { // 内部値は2Mod未満。t+qMod<2^63で、還元後も2Mod未満。 const auto product=std::uint64_t{a}*b; const auto q=static_cast(product)*negative_inverse_; const auto value=static_cast((product+std::uint64_t{q}*Mod)>>32); return value; } else return multiply(a,b); } static constexpr std::uint32_t working_normalize(std::uint32_t value) noexcept { if constexpr(montgomery_)return value>=Mod?value-Mod:value; else return value; } static constexpr std::uint32_t working_add(std::uint32_t a,std::uint32_t b) noexcept { if constexpr(montgomery_){const auto value=a+b;return value>=2*Mod?value-2*Mod:value;} else return add(a,b); } static constexpr std::uint32_t working_subtract(std::uint32_t a,std::uint32_t b) noexcept { if constexpr(montgomery_)return a>=b?a-b:a+2*Mod-b; else return subtract(a,b); } static constexpr std::uint32_t encode(std::uint32_t value) noexcept { if constexpr(montgomery_)return working_multiply(value,r_squared_); else return value; } template static void bit_reverse_convert(std::vector& a) { const auto convert=[](std::uint32_t value) { #ifndef SKIP_RUNTIME_CHECKS if constexpr(Check)if(value>=Mod)throw std::invalid_argument("NTT: coefficient not normalized"); #endif if constexpr(Encode)return encode(value); else return working_normalize(working_multiply(value,1)); }; a[0]=convert(a[0]); for(std::size_t i=1,reverse=0;i>1; for(;reverse&bit;bit>>=1)reverse^=bit; reverse^=bit; if(i static std::uint32_t normalize(T value) noexcept { static_assert(sizeof(T) <= sizeof(std::uint64_t)); if constexpr (std::is_signed_v) { const auto remainder = static_cast(value) % static_cast(Mod); return static_cast(remainder < 0 ? remainder + Mod : remainder); } else return static_cast(static_cast(value) % Mod); } static std::size_t result_size(std::size_t a, std::size_t b) { return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::convolution_result_size(a, b, "NTT: convolution size overflow"); } static std::size_t transform_size(std::size_t output) { if (output > max_length()) throw std::length_error("NTT: transform too long"); return std::bit_ceil(output); } static void bit_reverse(std::vector& a) noexcept { const std::size_t n = a.size(); for (std::size_t i = 1, reverse = 0; i < n; ++i) { std::size_t bit = n >> 1; for (; reverse & bit; bit >>= 1) reverse ^= bit; reverse ^= bit; if (i < reverse) std::swap(a[i], a[reverse]); } } #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) static bool use_avx2(std::size_t n) noexcept { if constexpr(montgomery_)return n>=16 && /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::ntt_avx2_available(); else return false; } [[gnu::always_inline,gnu::target("avx2")]] static inline __m256i vector_reduce(__m256i value) noexcept { const auto mod=_mm256_set1_epi32(static_cast(Mod)); return _mm256_min_epu32(value,_mm256_sub_epi32(value,mod)); } [[gnu::always_inline,gnu::target("avx2")]] static inline __m256i vector_add(__m256i a,__m256i b) noexcept { const auto sum=_mm256_add_epi32(a,b);return _mm256_min_epu32(sum,_mm256_sub_epi32(sum,_mm256_set1_epi32(static_cast(2*Mod)))); } [[gnu::always_inline,gnu::target("avx2")]] static inline __m256i vector_subtract(__m256i a,__m256i b) noexcept { const auto difference=_mm256_sub_epi32(a,b);return _mm256_min_epu32(difference,_mm256_add_epi32(difference,_mm256_set1_epi32(static_cast(2*Mod)))); } [[gnu::always_inline,gnu::target("avx2")]] static inline __m256i vector_multiply(__m256i a,__m256i b) noexcept { const auto mod=_mm256_set1_epi32(static_cast(Mod)); const auto inverse=_mm256_set1_epi32(static_cast(negative_inverse_)); const auto even=_mm256_mul_epu32(a,b); const auto odd=_mm256_mul_epu32(_mm256_srli_epi64(a,32),_mm256_srli_epi64(b,32)); const auto q_even=_mm256_mul_epu32(even,inverse),q_odd=_mm256_mul_epu32(odd,inverse); const auto reduced_even=_mm256_srli_epi64(_mm256_add_epi64(even,_mm256_mul_epu32(q_even,mod)),32); const auto reduced_odd=_mm256_add_epi64(odd,_mm256_mul_epu32(q_odd,mod)); return _mm256_blend_epi32(reduced_even,reduced_odd,0xaa); } [[gnu::always_inline,gnu::target("avx2")]] // 4点段の偶数レーンの根は1。奇数レーンだけ虚数単位の根を掛ける。 static inline __m256i vector_multiply_quarter_root(__m256i value, __m256i imaginary) noexcept { const auto odd = _mm256_mul_epu32(_mm256_srli_epi64(value, 32), imaginary); const auto q = _mm256_mul_epu32(odd, _mm256_set1_epi32(static_cast(negative_inverse_))); const auto reduced = _mm256_add_epi64(odd, _mm256_mul_epu32(q, _mm256_set1_epi32(static_cast(Mod)))); return _mm256_blend_epi32(value, reduced, 0xaa); } [[gnu::always_inline,gnu::target("avx2")]] static inline __m256i vector_multiply_prepared(__m256i a, __m256i b, __m256i b_times_negative_inverse) noexcept { const auto mod = _mm256_set1_epi32(static_cast(Mod)); const auto even = _mm256_mul_epu32(a, b); const auto odd = _mm256_mul_epu32(_mm256_srli_epi64(a, 32), _mm256_srli_epi64(b, 32)); const auto q_even = _mm256_mul_epu32(a, b_times_negative_inverse); const auto q_odd = _mm256_mul_epu32(_mm256_srli_epi64(a, 32), _mm256_srli_epi64(b_times_negative_inverse, 32)); const auto reduced_even = _mm256_srli_epi64(_mm256_add_epi64(even, _mm256_mul_epu32(q_even, mod)), 32); const auto reduced_odd = _mm256_add_epi64(odd, _mm256_mul_epu32(q_odd, mod)); return _mm256_blend_epi32(reduced_even, reduced_odd, 0xaa); } #ifndef SKIP_RUNTIME_CHECKS [[gnu::always_inline,gnu::target("avx2")]] static inline void vector_check(__m256i value) { const auto maximum=_mm256_set1_epi32(static_cast(Mod-1)); if(_mm256_movemask_epi8(_mm256_cmpeq_epi32(value,_mm256_min_epu32(value,maximum)))!=-1) throw std::invalid_argument("NTT: coefficient not normalized"); } #endif [[gnu::target("avx2")]] static void pointwise_avx2(std::vector& a,const std::vector& b) noexcept { for(std::size_t i=0;i(a.data()+i)); const auto y=_mm256_loadu_si256(reinterpret_cast(b.data()+i)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+i),vector_multiply(x,y)); } } [[gnu::target("avx2")]] void prepare_avx2(std::size_t previous, std::size_t length) { for (std::size_t half = previous; half < length; half <<= 1) { const auto step = stage_roots_[std::countr_zero(half) + 1]; const auto scale = _mm256_set1_epi32(static_cast(step)); std::size_t j = 0; for (; j + 8 <= half / 2; j += 8) { const auto value = _mm256_loadu_si256(reinterpret_cast(roots_.data() + half / 2 + j)); const auto product = vector_multiply(value, scale); // 偶数べきと奇数べきを交互に並べる。 const auto low = _mm256_unpacklo_epi32(value, product); const auto high = _mm256_unpackhi_epi32(value, product); _mm256_storeu_si256(reinterpret_cast<__m256i*>(roots_.data() + half + 2 * j), _mm256_permute2x128_si256(low, high, 0x20)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(roots_.data() + half + 2 * j + 8), _mm256_permute2x128_si256(low, high, 0x31)); } for (; j < half / 2; ++j) { const auto value = roots_[half / 2 + j]; roots_[half + 2 * j] = value; roots_[half + 2 * j + 1] = working_multiply(value, step); } } } template [[gnu::target("avx2")]] static std::vector naive_avx2(std::vector& a,std::vector& b,std::size_t output) { if(a.size()>b.size())a.swap(b); std::vector result(output); for(std::size_t i=0;i=Mod)throw std::invalid_argument("NTT: coefficient not normalized"); #endif const auto encoded_coefficient = encode(a[i]); const auto coefficient=_mm256_set1_epi32(static_cast(encoded_coefficient)); const auto coefficient_times_inverse = static_cast(std::uint64_t{encoded_coefficient} * negative_inverse_); const auto qcoefficient = _mm256_set1_epi32(static_cast(coefficient_times_inverse)); std::size_t j=0; for(;j+8<=b.size();j+=8) { const auto input=_mm256_loadu_si256(reinterpret_cast(b.data()+j)); #ifndef SKIP_RUNTIME_CHECKS if constexpr(Check)if(i==0)vector_check(input); #endif const auto old=_mm256_loadu_si256(reinterpret_cast(result.data()+i+j)); const auto product=vector_reduce(vector_multiply_prepared(input,coefficient,qcoefficient)); const auto sum=vector_reduce(_mm256_add_epi32(old,product)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(result.data()+i+j),sum); } for(;j=Mod)throw std::invalid_argument("NTT: coefficient not normalized"); #endif result[i+j]=add(result[i+j],multiply(a[i],b[j])); } } return result; } template [[gnu::target("avx2")]] void forward_avx2(std::vector& a, [[maybe_unused]] std::size_t remaining_length=0) const { const auto n=a.size(); std::size_t length=SkipFirst ? remaining_length : n; for(;length>=32;length>>=2) { const auto quarter=length/4; for(std::size_t block=0;block(a.data()+block+j)); auto x1=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+quarter)); auto x2=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+2*quarter)); auto x3=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+3*quarter)); if constexpr (!SkipFirst) if(length==n) { #ifndef SKIP_RUNTIME_CHECKS if constexpr(Check){vector_check(x0);vector_check(x1);vector_check(x2);vector_check(x3);} #endif const auto r2=_mm256_set1_epi32(static_cast(r_squared_)); x0=vector_multiply(x0,r2);x1=vector_multiply(x1,r2);x2=vector_multiply(x2,r2);x3=vector_multiply(x3,r2); } const auto first=_mm256_loadu_si256(reinterpret_cast(roots_.data()+2*quarter+j)); const auto second=_mm256_loadu_si256(reinterpret_cast(roots_.data()+3*quarter+j)); const auto third=_mm256_loadu_si256(reinterpret_cast(roots_.data()+quarter+j)); const auto v0=vector_add(x0,x2),v1=vector_add(x1,x3); const auto v2=vector_multiply(vector_subtract(x0,x2),first),v3=vector_multiply(vector_subtract(x1,x3),second); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j),vector_add(v0,v1)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+quarter),vector_multiply(vector_subtract(v0,v1),third)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+2*quarter),vector_add(v2,v3)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+3*quarter),vector_multiply(vector_subtract(v2,v3),third)); } } if(length==16) { const auto half=length>>1; for(std::size_t block=0;block(a.data()+block+j)); auto v=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+half)); if constexpr (!SkipFirst) if(length==n) { #ifndef SKIP_RUNTIME_CHECKS if constexpr(Check){vector_check(u);vector_check(v);} #endif const auto r2=_mm256_set1_epi32(static_cast(r_squared_)); u=vector_multiply(u,r2);v=vector_multiply(v,r2); } const auto roots=_mm256_loadu_si256(reinterpret_cast(roots_.data()+half+j)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j),vector_add(u,v)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+half),vector_multiply(vector_subtract(u,v),roots)); } for(;j=Mod||v>=Mod))throw std::invalid_argument("NTT: coefficient not normalized"); #endif if constexpr (!SkipFirst) if(length==n){u=encode(u);v=encode(v);} a[block+j]=working_add(u,v);a[block+j+half]=working_multiply(working_subtract(u,v),roots_[half+j]); } } } const auto u4=_mm256_setr_epi32(0,1,0,1,4,5,4,5),v4=_mm256_setr_epi32(2,3,2,3,6,7,6,7); const auto r4=_mm256_set1_epi32(static_cast(roots_[3])); const auto r8=_mm256_setr_epi32(static_cast(roots_[4]),static_cast(roots_[5]),static_cast(roots_[6]),static_cast(roots_[7]),static_cast(roots_[4]),static_cast(roots_[5]),static_cast(roots_[6]),static_cast(roots_[7])); for(std::size_t block=0;block(a.data()+block)); auto u=_mm256_permute2x128_si256(x,x,0),v=_mm256_permute2x128_si256(x,x,0x11); x=_mm256_blend_epi32(vector_add(u,v),vector_multiply(vector_subtract(u,v),r8),0xf0); u=_mm256_permutevar8x32_epi32(x,u4);v=_mm256_permutevar8x32_epi32(x,v4); x=_mm256_blend_epi32(vector_add(u,v),vector_multiply_quarter_root(vector_subtract(u,v),r4),0xcc); u=_mm256_shuffle_epi32(x,_MM_SHUFFLE(2,2,0,0));v=_mm256_shuffle_epi32(x,_MM_SHUFFLE(3,3,1,1)); x=_mm256_blend_epi32(vector_add(u,v),vector_subtract(u,v),0xaa); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block),x); } } template [[gnu::target("avx2")]] void forward_zero_padded_avx2(std::vector& a) const { const auto n=a.size(), quarter=n/4; const auto r2=_mm256_set1_epi32(static_cast(r_squared_)); // 後半はresizeが補ったゼロ。初段のゼロの符号化と積を省く。 for(std::size_t j=0;j(a.data()+j)); auto x1=_mm256_loadu_si256(reinterpret_cast(a.data()+quarter+j)); #ifndef SKIP_RUNTIME_CHECKS if constexpr(Check){vector_check(x0);vector_check(x1);} #endif x0=vector_multiply(x0,r2);x1=vector_multiply(x1,r2); const auto first=_mm256_loadu_si256(reinterpret_cast(roots_.data()+2*quarter+j)); const auto second=_mm256_loadu_si256(reinterpret_cast(roots_.data()+3*quarter+j)); const auto third=_mm256_loadu_si256(reinterpret_cast(roots_.data()+quarter+j)); const auto v2=vector_multiply(x0,first),v3=vector_multiply(x1,second); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+j),vector_add(x0,x1)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+quarter+j),vector_multiply(vector_subtract(x0,x1),third)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+2*quarter+j),vector_add(v2,v3)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+3*quarter+j),vector_multiply(vector_subtract(v2,v3),third)); } forward_avx2(a,n/4); } template [[gnu::target("avx2")]] void forward_zero_quarters_avx2(std::vector& a, std::size_t length, std::size_t active) const { const auto quarter=length/4; // 各区間の先頭active要素以外は、以前の段でもゼロのまま。 for(std::size_t block=0;block(a.data()+block+j)); if constexpr(Encode) { #ifndef SKIP_RUNTIME_CHECKS if constexpr(Check)vector_check(x0); #endif x0=vector_multiply(x0,_mm256_set1_epi32(static_cast(r_squared_))); } const auto first=_mm256_loadu_si256(reinterpret_cast(roots_.data()+2*quarter+j)); const auto third=_mm256_loadu_si256(reinterpret_cast(roots_.data()+quarter+j)); const auto v2=vector_multiply(x0,first); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j),x0); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+quarter+j),vector_multiply(x0,third)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+2*quarter+j),v2); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+3*quarter+j),vector_multiply(v2,third)); } } template [[gnu::target("avx2")]] void backward_avx2(std::vector& a) const { const auto n=a.size(); const auto u4=_mm256_setr_epi32(0,1,0,1,4,5,4,5),v4=_mm256_setr_epi32(2,3,2,3,6,7,6,7); const auto r4=_mm256_set1_epi32(static_cast(roots_[3])); const auto r8=_mm256_setr_epi32(static_cast(roots_[4]),static_cast(roots_[5]),static_cast(roots_[6]),static_cast(roots_[7]),static_cast(roots_[4]),static_cast(roots_[5]),static_cast(roots_[6]),static_cast(roots_[7])); for(std::size_t block=0;block(a.data()+block)); auto u=_mm256_shuffle_epi32(x,_MM_SHUFFLE(2,2,0,0)),v=_mm256_shuffle_epi32(x,_MM_SHUFFLE(3,3,1,1)); x=_mm256_blend_epi32(vector_add(u,v),vector_subtract(u,v),0xaa); u=_mm256_permutevar8x32_epi32(x,u4);v=vector_multiply_quarter_root(_mm256_permutevar8x32_epi32(x,v4),r4); x=_mm256_blend_epi32(vector_add(u,v),vector_subtract(u,v),0xcc); u=_mm256_permute2x128_si256(x,x,0);v=vector_multiply(_mm256_permute2x128_si256(x,x,0x11),r8); x=_mm256_blend_epi32(vector_add(u,v),vector_subtract(u,v),0xf0); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block),x); } std::size_t completed=8; for(std::size_t length=32;length<=n;) { const auto quarter=length/4; for(std::size_t block=0;block(a.data()+block+j)); const auto raw1=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+quarter)); const auto x2=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+2*quarter)); const auto raw3=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+3*quarter)); const auto first=_mm256_loadu_si256(reinterpret_cast(roots_.data()+quarter+j)); const auto second=_mm256_loadu_si256(reinterpret_cast(roots_.data()+2*quarter+j)); const auto third=_mm256_loadu_si256(reinterpret_cast(roots_.data()+3*quarter+j)); const auto x1=vector_multiply(raw1,first),x3=vector_multiply(raw3,first); const auto v0=vector_add(x0,x1),v1=vector_subtract(x0,x1); const auto v2=vector_multiply(vector_add(x2,x3),second),v3=vector_multiply(vector_subtract(x2,x3),third); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j),vector_add(v0,v2)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+quarter),vector_add(v1,v3)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+2*quarter),vector_subtract(v0,v2)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+3*quarter),vector_subtract(v1,v3)); } completed=length; if(length>n/4)break; length*=4; } if(completed>1; for(std::size_t block=0;block(a.data()+block+j)); const auto raw=_mm256_loadu_si256(reinterpret_cast(a.data()+block+j+half)); const auto roots=_mm256_loadu_si256(reinterpret_cast(roots_.data()+half+j)); const auto v=vector_multiply(raw,roots); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j),vector_add(u,v)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+block+j+half),vector_subtract(u,v)); } for(;j=Mod||raw>=Mod))throw std::invalid_argument("NTT: coefficient not normalized"); #endif const auto v=working_multiply(raw,roots_[half+j]); a[block+j]=working_add(u,v);a[block+j+half]=working_subtract(u,v); } } } // nはMod-1の約数(変換長は対応する2冪)。 // d=(Mod-1)/nなら、n*(Mod-d)=(n-1)*Mod+1。 // よってMod-dがnの逆元になる。 const auto inverse_n=static_cast(Mod - (Mod - 1) / static_cast(n)); const auto scale=_mm256_set1_epi32(static_cast(inverse_n)); const auto reverse=_mm256_setr_epi32(7,6,5,4,3,2,1,0); a[0]=working_normalize(working_multiply(a[0],inverse_n)); std::size_t i=1; for(;i+8<=n/2;i+=8) { const auto lo=_mm256_loadu_si256(reinterpret_cast(a.data()+i)); const auto hi=_mm256_loadu_si256(reinterpret_cast(a.data()+n-i-7)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+i),_mm256_permutevar8x32_epi32(vector_reduce(vector_multiply(hi,scale)),reverse)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(a.data()+n-i-7),_mm256_permutevar8x32_epi32(vector_reduce(vector_multiply(lo,scale)),reverse)); } for(;i void forward(std::vector& a) const { const std::size_t n = a.size(); #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) if constexpr(montgomery_)if(use_avx2(n)){forward_avx2(a);return;} #endif for (std::size_t length = n; length >= 2; length >>= 1) { const std::size_t half = length >> 1; for (std::size_t block = 0; block < n; block += length) for (std::size_t j = 0; j < half; ++j) { auto u = a[block + j], v = a[block + j + half]; #ifndef SKIP_RUNTIME_CHECKS if constexpr (Check) if (length == n && (u >= Mod || v >= Mod)) throw std::invalid_argument("NTT: coefficient not normalized"); #endif if constexpr(montgomery_)if(length==n){u=encode(u);v=encode(v);} a[block + j] = working_add(u, v); a[block + j + half] = working_multiply(working_subtract(u, v), roots_[half + j]); } } } template void forward_padded(std::vector& a, [[maybe_unused]] std::size_t input_size) const { #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) if constexpr(montgomery_) if(a.size()>=32 && input_size<=a.size()/2 && use_avx2(a.size())) { if(input_size<=a.size()/4) { // 長さは上限検査済み。8要素へ丸めても各区間の1/4以内に収まる。 const auto active=(input_size+7)&~std::size_t{7}; auto length=a.size(); forward_zero_quarters_avx2(a,length,active); length/=4; while(length>=32 && active<=length/4) { forward_zero_quarters_avx2(a,length,active); length/=4; } forward_avx2(a,length);return; } forward_zero_padded_avx2(a);return; } #endif forward(a); } template void backward(std::vector& a) const { const std::size_t n = a.size(); #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) if constexpr(montgomery_)if(use_avx2(n)){backward_avx2(a);return;} #endif for (std::size_t length = 2; length <= n; length <<= 1) { const std::size_t half = length >> 1; for (std::size_t block = 0; block < n; block += length) for (std::size_t j = 0; j < half; ++j) { const auto u = a[block + j], raw = a[block + j + half]; #ifndef SKIP_RUNTIME_CHECKS if constexpr (Check) if (length == 2 && (u >= Mod || raw >= Mod)) throw std::invalid_argument("NTT: coefficient not normalized"); #endif const auto v = working_multiply(raw, roots_[half + j]); a[block + j] = working_add(u, v); a[block + j + half] = working_subtract(u, v); } } // nはMod-1の約数(変換長は対応する2冪)。 // d=(Mod-1)/nなら、n*(Mod-d)=(n-1)*Mod+1。 // よってMod-dがnの逆元になる。 const auto inverse_n = static_cast(Mod - (Mod - 1) / static_cast(n)); a[0] = working_normalize(working_multiply(a[0], inverse_n)); for (std::size_t i = 1; i < n - i; ++i) { const auto lo = working_normalize(working_multiply(a[i], inverse_n)), hi = working_normalize(working_multiply(a[n - i], inverse_n)); a[i] = hi; a[n - i] = lo; } if (n > 1) a[n / 2] = working_normalize(working_multiply(a[n / 2], inverse_n)); } template std::vector compute(std::vector a, std::vector b) { const std::size_t output = result_size(a.size(), b.size()); if (output == 0) return {}; if (std::min(a.size(), b.size()) <= 32) { #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) if(use_avx2(output))return naive_avx2(a,b,output); #endif std::vector result(output); for (std::size_t i = 0; i < a.size(); ++i) { #ifndef SKIP_RUNTIME_CHECKS if constexpr (Check) if (a[i] >= Mod) throw std::invalid_argument("NTT: coefficient not normalized"); #endif for (std::size_t j = 0; j < b.size(); ++j) { #ifndef SKIP_RUNTIME_CHECKS if constexpr (Check) if (i == 0 && b[j] >= Mod) throw std::invalid_argument("NTT: coefficient not normalized"); #endif result[i + j] = add(result[i + j], multiply(a[i], b[j])); } } return result; } const auto length = transform_size(output); prepare(length); #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) const auto input_a=a.size(), input_b=b.size(); #endif a.resize(length); b.resize(length); #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) forward_padded(a,input_a); forward_padded(b,input_b); #else forward(a); forward(b); #endif #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) if(use_avx2(length))pointwise_avx2(a,b); else #endif for (std::size_t i = 0; i < length; ++i) a[i] = working_multiply(a[i], b[i]); backward(a); a.resize(output); return a; } public: static constexpr std::uint32_t modulus() noexcept { return Mod; } static constexpr std::size_t max_length() noexcept { return std::size_t{1} << rank_; } [[nodiscard]] std::size_t roots_capacity() const noexcept { return roots_.size(); } void clear_cache() noexcept { std::vector{}.swap(roots_); } void prepare(std::size_t length) { if (length == 0) return; if (!std::has_single_bit(length) || length > max_length()) throw std::length_error("NTT: length must be a supported power of two"); if (length <= 1 || length <= roots_.size()) return; const std::size_t previous = std::max(2, roots_.size()); roots_.resize(length); roots_[1] = encode(1); #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) if constexpr (montgomery_) if (length >= 256 && use_avx2(length)) { prepare_avx2(previous, length); return; } #endif for (std::size_t half = previous; half < length; half <<= 1) { const auto step = stage_roots_[std::countr_zero(half) + 1]; for (std::size_t j = 0; j < half/2; ++j) { const auto value=roots_[half/2+j]; roots_[half+2*j]=value; roots_[half+2*j+1]=working_multiply(value,step); } } } void transform(std::vector& a, bool inverse = false) { if (a.empty()) return; prepare(a.size()); if (a.size() == 1) { #ifndef SKIP_RUNTIME_CHECKS if (a[0] >= Mod) throw std::invalid_argument("NTT: coefficient not normalized"); #endif return; } if constexpr(montgomery_) { if(inverse){bit_reverse_convert(a);backward(a);} else{forward(a);bit_reverse_convert(a);} }else{ if (inverse) { bit_reverse(a); backward(a); } else { forward(a); bit_reverse(a); } } } template std::vector convolution(const std::vector& a, const std::vector& b) { if (a.empty() || b.empty()) return {}; const auto output = result_size(a.size(), b.size()); if (std::min(a.size(), b.size()) > 32) (void)transform_size(output); std::vector x(a.size()), y(b.size()); for (std::size_t i = 0; i < a.size(); ++i) x[i] = normalize(a[i]); for (std::size_t i = 0; i < b.size(); ++i) y[i] = normalize(b[i]); return compute(std::move(x), std::move(y)); } std::vector convolution_normalized(std::vector a, std::vector b) { return compute(std::move(a), std::move(b)); } template std::vector square(const std::vector& a) { if (a.size() <= 32) return convolution(a, a); const auto output = result_size(a.size(), a.size()), length = transform_size(output); prepare(length); std::vector x(length); for (std::size_t i = 0; i < a.size(); ++i) x[i] = normalize(a[i]); #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) forward_padded(x,a.size()); #else forward(x); #endif #if !defined(SIRKIT_DISABLE_SIMD) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)) if(use_avx2(length))pointwise_avx2(x,x); else #endif for (auto& value : x) value = working_multiply(value, value); backward(x); x.resize(output); return x; } }; #endif #ifndef SIRKIT_CONVOLUTION_MOD_DEFINED #define SIRKIT_CONVOLUTION_MOD_DEFINED template std::vector ConvolutionMod(const std::vector& a, const std::vector& b) { return NTT{}.convolution(a, b); } #endif } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { #ifndef SIRKIT_FORMAL_POWER_SERIES_DETAIL_DEFINED #define SIRKIT_FORMAL_POWER_SERIES_DETAIL_DEFINED namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ { inline constexpr std::uint32_t fps_modulus = 998244353; inline std::uint32_t fps_add(std::uint32_t a, std::uint32_t b) noexcept { const auto value = a + b; return value >= fps_modulus ? value - fps_modulus : value; } inline std::uint32_t fps_subtract(std::uint32_t a, std::uint32_t b) noexcept { return a >= b ? a - b : a + fps_modulus - b; } inline std::uint32_t fps_multiply(std::uint32_t a, std::uint32_t b) noexcept { return static_cast(std::uint64_t{a} * b % fps_modulus); } inline std::uint32_t fps_power(std::uint32_t value, std::uint64_t exponent) noexcept { std::uint32_t result = 1; for (; exponent != 0; exponent >>= 1) { if (exponent & 1) result = fps_multiply(result, value); value = fps_multiply(value, value); } return result; } inline NTT<>& fps_engine() { static thread_local NTT<> engine; return engine; } inline std::vector& fps_integer_inverses() { static thread_local std::vector values; return values; } inline void fps_prepare_inverses(std::size_t n) { if (n >= fps_modulus) throw std::length_error("FormalPowerSeries: noninvertible integration index"); auto& table = fps_integer_inverses(); if (table.size() < 2) { table.resize(2); table[1] = 1; } if (n < table.size()) return; const auto first = table.size(); table.resize(n + 1); for (std::size_t i = first; i <= n; ++i) table[i] = fps_modulus - static_cast( std::uint64_t{fps_modulus / static_cast(i)} * table[fps_modulus % i] % fps_modulus); } inline void fps_require_precision(std::size_t n) { if (n > NTT<>::max_length() / 2) throw std::length_error("FormalPowerSeries: precision exceeds NTT budget"); } inline std::optional fps_scalar_sqrt(std::uint32_t value) noexcept { if (value == 0) return 0; if (fps_power(value, (fps_modulus - 1) / 2) != 1) return std::nullopt; constexpr std::uint32_t odd = 119; auto root = fps_power(value, (odd + 1) / 2), remainder = fps_power(value, odd); auto correction = fps_power(3, odd); unsigned remaining = 23; while (remainder != 1) { unsigned first = 0; auto probe = remainder; // Euler判定後のremainderの位数は2^(remaining-1)を割り切る。 while (probe != 1) { probe = fps_multiply(probe, probe); ++first; } const auto change = fps_power(correction, std::uint32_t{1} << (remaining - first - 1)); root = fps_multiply(root, change); correction = fps_multiply(change, change); remainder = fps_multiply(remainder, correction); remaining = first; } return std::min(root, fps_modulus - root); } } #endif #ifndef SIRKIT_FORMAL_POWER_SERIES_DEFINED #define SIRKIT_FORMAL_POWER_SERIES_DEFINED class FormalPowerSeries { std::vector coefficients_; struct Normalized {}; FormalPowerSeries(std::vector values, Normalized) noexcept : coefficients_(std::move(values)) {} template static std::uint32_t normalize(T value) noexcept { static_assert(sizeof(T) <= sizeof(std::uint64_t)); if constexpr (std::is_signed_v) { const auto remainder = static_cast(value) % static_cast(modulus()); return static_cast(remainder < 0 ? remainder + modulus() : remainder); } else return static_cast(static_cast(value) % modulus()); } std::vector short_prefix(std::size_t n) const { return {coefficients_.begin(), coefficients_.begin() + std::min(n, size())}; } static FormalPowerSeries normalized(std::vector values) noexcept { return FormalPowerSeries(std::move(values), Normalized{}); } public: static constexpr std::uint32_t modulus() noexcept { return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_modulus; } static constexpr std::size_t max_precision() noexcept { return NTT<>::max_length() / 2; } FormalPowerSeries() = default; explicit FormalPowerSeries(std::size_t n) : coefficients_(n) {} explicit FormalPowerSeries(std::vector values) : coefficients_(std::move(values)) { for (auto& value : coefficients_) value %= modulus(); } template explicit FormalPowerSeries(const std::vector& values) : coefficients_(values.size()) { for (std::size_t i = 0; i < values.size(); ++i) coefficients_[i] = normalize(values[i]); } FormalPowerSeries(std::initializer_list values) : coefficients_(values.size()) { std::size_t at = 0; for (auto value : values) coefficients_[at++] = normalize(value); } [[nodiscard]] std::size_t size() const noexcept { return coefficients_.size(); } [[nodiscard]] bool empty() const noexcept { return coefficients_.empty(); } [[nodiscard]] const std::vector& coefficients() const noexcept { return coefficients_; } [[nodiscard]] std::uint32_t operator[](std::size_t i) const { #ifndef SKIP_RUNTIME_CHECKS if (i >= size()) throw std::out_of_range("FormalPowerSeries: coefficient"); #endif return coefficients_[i]; } [[nodiscard]] std::uint32_t get(std::size_t i) const noexcept { return i < size() ? coefficients_[i] : 0; } template void set(std::size_t i, T value) { #ifndef SKIP_RUNTIME_CHECKS if (i >= size()) throw std::out_of_range("FormalPowerSeries: coefficient"); #endif coefficients_[i] = normalize(value); } void resize(std::size_t n) { coefficients_.resize(n); } void trim() noexcept { while (!empty() && coefficients_.back() == 0) coefficients_.pop_back(); } [[nodiscard]] FormalPowerSeries prefix(std::size_t n) const { std::vector result(n); std::copy_n(coefficients_.begin(), std::min(n, size()), result.begin()); return normalized(std::move(result)); } FormalPowerSeries& operator+=(const FormalPowerSeries& other) { if (other.size() > size()) coefficients_.resize(other.size()); for (std::size_t i = 0; i < other.size(); ++i) coefficients_[i] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_add(coefficients_[i], other.coefficients_[i]); return *this; } FormalPowerSeries& operator-=(const FormalPowerSeries& other) { if (other.size() > size()) coefficients_.resize(other.size()); for (std::size_t i = 0; i < other.size(); ++i) coefficients_[i] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_subtract(coefficients_[i], other.coefficients_[i]); return *this; } FormalPowerSeries& operator*=(const FormalPowerSeries& other) { auto result = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine().convolution_normalized(coefficients_, other.coefficients_); coefficients_.swap(result); return *this; } FormalPowerSeries& operator/=(const FormalPowerSeries& other) { *this = divmod(other).first; return *this; } FormalPowerSeries& operator%=(const FormalPowerSeries& other) { *this = divmod(other).second; return *this; } friend FormalPowerSeries operator+(FormalPowerSeries a, const FormalPowerSeries& b) { a += b; return a; } friend FormalPowerSeries operator-(FormalPowerSeries a, const FormalPowerSeries& b) { a -= b; return a; } friend FormalPowerSeries operator*(const FormalPowerSeries& a, const FormalPowerSeries& b) { return normalized(/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine().convolution_normalized(a.coefficients_, b.coefficients_)); } friend FormalPowerSeries operator/(const FormalPowerSeries& a, const FormalPowerSeries& b) { return a.divmod(b).first; } friend FormalPowerSeries operator%(const FormalPowerSeries& a, const FormalPowerSeries& b) { return a.divmod(b).second; } friend bool operator==(const FormalPowerSeries&, const FormalPowerSeries&) = default; template [[nodiscard]] FormalPowerSeries scale(T scalar) const { auto result = coefficients_; const auto value = normalize(scalar); for (auto& coefficient : result) coefficient = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(coefficient, value); return normalized(std::move(result)); } [[nodiscard]] FormalPowerSeries derivative() const { if (empty()) return {}; std::vector result(size() - 1); for (std::size_t i = 1; i < size(); ++i) result[i - 1] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(coefficients_[i], static_cast(i % modulus())); return normalized(std::move(result)); } [[nodiscard]] FormalPowerSeries integral() const { if (empty()) return FormalPowerSeries{0}; /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_prepare_inverses(size()); const auto& inverses = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_integer_inverses(); std::vector result(size() + 1); for (std::size_t i = 0; i < size(); ++i) result[i + 1] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(coefficients_[i], inverses[i + 1]); return normalized(std::move(result)); } [[nodiscard]] FormalPowerSeries inverse(std::size_t n) const { /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_require_precision(n); if (n == 0) return {}; if (get(0) == 0) throw std::domain_error("FormalPowerSeries: inverse of zero constant"); std::vector result{/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_power(get(0), modulus() - 2)}; auto& engine = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine(); for (std::size_t known = 1; known < n;) { const auto target = std::min(n, known * 2); auto error = engine.convolution_normalized(short_prefix(target), result); error.resize(target); for (auto& coefficient : error) coefficient = coefficient == 0 ? 0 : modulus() - coefficient; error[0] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_add(error[0], 2); auto next = engine.convolution_normalized(std::move(result), std::move(error)); next.resize(target); result.swap(next); known = target; } return normalized(std::move(result)); } [[nodiscard]] FormalPowerSeries inverse() const { return inverse(size()); } [[nodiscard]] FormalPowerSeries log(std::size_t n) const { /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_require_precision(n); if (n == 0) return {}; #ifndef SKIP_RUNTIME_CHECKS if (get(0) != 1) throw std::domain_error("FormalPowerSeries: logarithm constant must be one"); #endif auto differentiated = normalized(short_prefix(n)).derivative(); auto result = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine().convolution_normalized( std::move(differentiated.coefficients_), inverse(n).coefficients_); result.resize(n - 1); return normalized(std::move(result)).integral(); } [[nodiscard]] FormalPowerSeries log() const { return log(size()); } [[nodiscard]] FormalPowerSeries exp(std::size_t n) const { /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_require_precision(n); if (n == 0) return {}; #ifndef SKIP_RUNTIME_CHECKS if (get(0) != 0) throw std::domain_error("FormalPowerSeries: exponential constant must be zero"); #endif FormalPowerSeries result{1}; for (std::size_t known = 1; known < n;) { const auto target = std::min(n, known * 2); auto correction = result.log(target).coefficients_; for (std::size_t i = 0; i < target; ++i) correction[i] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_subtract(get(i), correction[i]); correction[0] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_add(correction[0], 1); auto next = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine().convolution_normalized(std::move(result.coefficients_), std::move(correction)); next.resize(target); result.coefficients_.swap(next); known = target; } return result; } [[nodiscard]] FormalPowerSeries exp() const { return exp(size()); } [[nodiscard]] FormalPowerSeries pow(std::uint64_t exponent, std::size_t n) const { /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_require_precision(n); if (n == 0) return {}; if (exponent == 0) { auto result = FormalPowerSeries(n); result.coefficients_[0] = 1; return result; } std::size_t leading = 0; while (leading < std::min(n, size()) && coefficients_[leading] == 0) ++leading; if (leading == std::min(n, size()) || (leading != 0 && exponent > (n - 1) / leading)) return FormalPowerSeries(n); const auto shift = leading == 0 ? 0 : leading * static_cast(exponent); const auto remaining = n - shift; const auto first = coefficients_[leading], inverse_first = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_power(first, modulus() - 2); std::vector unit(std::min(remaining, size() - leading)); for (std::size_t i = 0; i < unit.size(); ++i) unit[i] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(coefficients_[leading + i], inverse_first); auto logarithm = normalized(std::move(unit)).log(remaining); const auto reduced_exponent = static_cast(exponent % modulus()); for (auto& coefficient : logarithm.coefficients_) coefficient = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(coefficient, reduced_exponent); auto powered = logarithm.exp(remaining); const auto multiplier = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_power(first, exponent); std::vector result(n); for (std::size_t i = 0; i < remaining; ++i) result[shift + i] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(powered.coefficients_[i], multiplier); return normalized(std::move(result)); } [[nodiscard]] FormalPowerSeries pow(std::uint64_t exponent) const { return pow(exponent, size()); } [[nodiscard]] std::optional sqrt(std::size_t n) const { /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_require_precision(n); if (n == 0) return FormalPowerSeries{}; std::size_t leading = 0; while (leading < std::min(n, size()) && coefficients_[leading] == 0) ++leading; if (leading == std::min(n, size())) return FormalPowerSeries(n); if (leading & 1) return std::nullopt; const auto root = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_scalar_sqrt(coefficients_[leading]); if (!root) return std::nullopt; const auto needed = n - leading; FormalPowerSeries result{static_cast(*root)}; for (std::size_t known = 1; known < needed;) { const auto target = std::min(needed, known * 2); std::vector part(std::min(target, size() - leading)); std::copy_n(coefficients_.begin() + leading, part.size(), part.begin()); auto ratio = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine().convolution_normalized(std::move(part), result.inverse(target).coefficients_); ratio.resize(target); for (std::size_t i = 0; i < target; ++i) ratio[i] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_add(ratio[i], result.get(i)), (modulus() + 1) / 2); result.coefficients_.swap(ratio); known = target; } std::vector shifted(n); std::copy(result.coefficients_.begin(), result.coefficients_.end(), shifted.begin() + leading / 2); return normalized(std::move(shifted)); } [[nodiscard]] std::optional sqrt() const { return sqrt(size()); } [[nodiscard]] std::pair divmod(const FormalPowerSeries& divisor) const { auto a = *this, b = divisor; a.trim(); b.trim(); if (b.empty()) throw std::domain_error("FormalPowerSeries: zero polynomial divisor"); if (a.size() < b.size()) return {FormalPowerSeries{}, std::move(a)}; const auto quotient_size = a.size() - b.size() + 1; FormalPowerSeries quotient; if (std::min(quotient_size, b.size()) <= 32) { quotient.resize(quotient_size); const auto inverse_leading = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_power(b.coefficients_.back(), modulus() - 2); for (std::size_t at = quotient_size; at != 0; --at) { const auto index = at - 1; const auto coefficient = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(a.coefficients_[index + b.size() - 1], inverse_leading); quotient.coefficients_[index] = coefficient; for (std::size_t j = 0; j < b.size(); ++j) a.coefficients_[index + j] = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_subtract(a.coefficients_[index + j], /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_multiply(coefficient, b.coefficients_[j])); } a.resize(b.size() - 1); a.trim(); quotient.trim(); return {std::move(quotient), std::move(a)}; } /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_require_precision(quotient_size); std::vector reversed_a(quotient_size), reversed_b(std::min(quotient_size, b.size())); std::copy_n(a.coefficients_.rbegin(), quotient_size, reversed_a.begin()); std::copy_n(b.coefficients_.rbegin(), reversed_b.size(), reversed_b.begin()); auto reversed_quotient = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine().convolution_normalized( std::move(reversed_a), normalized(std::move(reversed_b)).inverse(quotient_size).coefficients_); reversed_quotient.resize(quotient_size); std::reverse(reversed_quotient.begin(), reversed_quotient.end()); quotient = normalized(std::move(reversed_quotient)); auto remainder = std::move(a) - b * quotient; remainder.resize(b.size() - 1); remainder.trim(); quotient.trim(); return {std::move(quotient), std::move(remainder)}; } static void clear_cache() noexcept { /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_engine().clear_cache(); std::vector{}.swap(/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_integer_inverses()); } }; #endif // SPDX-License-Identifier: CC0-1.0 } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { #ifndef SIRKIT_FPS_COMPOSITION_DEFINED #define SIRKIT_FPS_COMPOSITION_DEFINED namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ { inline constexpr auto kCompositionCapacity = std::size_t{1} << 18; inline void CheckCompositionPrecision(std::size_t precision) { if (precision > kCompositionCapacity) { throw std::length_error("FPS composition: precision exceeds NTT budget"); } } struct CompositionRectangle { std::size_t x_width; std::size_t y_width; std::vector values; CompositionRectangle(std::size_t x_size, std::size_t y_size) : x_width(x_size), y_width(y_size), values(x_size * y_size) { } std::uint32_t &at(std::size_t x, std::size_t y) { return values[y * x_width + x]; } std::uint32_t at(std::size_t x, std::size_t y) const { return values[y * x_width + x]; } }; inline CompositionRectangle CompositionGraeffe(const CompositionRectangle &denominator, std::size_t y_limit) { const auto half_width = (denominator.x_width + 1) / 2; const auto odd_width = denominator.x_width / 2; const auto stride = 2 * half_width; const auto y_last = denominator.y_width - 1; std::vector even(y_last * stride + half_width), odd(y_last * stride + odd_width); for (std::size_t y = 0; y <= y_last; ++y) { const auto offset = y * stride; for (std::size_t x = 0; x < half_width; ++x) { even[offset + x] = denominator.at(2 * x, y); } for (std::size_t x = 0; x < odd_width; ++x) { odd[offset + x] = denominator.at(2 * x + 1, y); } } // Q(x,y)=E(x²,y)+xO(x²,y)なら、Q(x,y)Q(-x,y)=E(x²,y)^2-x²O(x²,y)^2。 const auto even_squared = fps_engine().square(even); const auto odd_squared = fps_engine().square(odd); CompositionRectangle result(half_width, std::min(y_limit, 2 * y_last + 1)); for (std::size_t y = 0; y < result.y_width; ++y) { const auto offset = y * stride; result.at(0, y) = even_squared[offset]; for (std::size_t x = 1; x < half_width; ++x) { result.at(x, y) = fps_subtract(even_squared[offset + x], odd_squared[offset + x - 1]); } } return result; } inline std::vector CompositionProduct(const CompositionRectangle &numerator, const CompositionRectangle &denominator, std::size_t stride, std::size_t stretch) { std::vector left((numerator.y_width - 1) * stride + stretch * (numerator.x_width - 1) + 1); std::vector right((denominator.y_width - 1) * stride + denominator.x_width); for (std::size_t y = 0; y < numerator.y_width; ++y) { for (std::size_t x = 0; x < numerator.x_width; ++x) { left[y * stride + stretch * x] = numerator.at(x, y); } } for (std::size_t y = 0; y < denominator.y_width; ++y) { for (std::size_t x = 0; x < denominator.x_width; ++x) { const auto coefficient = denominator.at(x, y); right[y * stride + x] = (x % 2 == 0) ? coefficient : fps_subtract(0, coefficient); } } return fps_engine().convolution_normalized(std::move(left), std::move(right)); } inline CompositionRectangle CompositionDenominator(const FormalPowerSeries &series, std::size_t precision) { CompositionRectangle denominator(precision, 2); denominator.at(0, 0) = 1; for (std::size_t x = 0; x < precision; ++x) { denominator.at(x, 1) = fps_subtract(0, series.get(x)); } return denominator; } class FpsComposer { const FormalPowerSeries &outer_; const std::size_t outer_size_; [[nodiscard]] CompositionRectangle restore(const CompositionRectangle &denominator, const CompositionRectangle &reduced, std::size_t y_skip, std::size_t y_count) const { const auto stride = 2 * denominator.x_width - 1; const auto product = CompositionProduct(reduced, denominator, stride, 2); CompositionRectangle result(denominator.x_width, y_count); for (std::size_t y = 0; y < y_count; ++y) { std::copy_n(product.data() + (y_skip + y) * stride, result.x_width, result.values.data() + y * result.x_width); } return result; } [[nodiscard]] CompositionRectangle extract(const CompositionRectangle &denominator, std::size_t start) const { const auto y_count = outer_size_ - start; if (denominator.x_width == 1) { CompositionRectangle answer(1, y_count); // g(0)=0なら各段階でQ(0,y)=1。 for (std::size_t y = 0; y < y_count; ++y) { answer.at(0, y) = outer_.get(outer_size_ - 1 - start - y); } return answer; } const auto maximum_degree = denominator.y_width - 1; // y^start以上に寄与し得る係数だけを残す。 const auto reduced_start = start - std::min(start, maximum_degree); const auto window = extract(CompositionGraeffe(denominator, outer_size_), reduced_start); return restore(denominator, window, start - reduced_start, y_count); } public: FpsComposer(const FormalPowerSeries &outer, std::size_t outer_size) : outer_(outer), outer_size_(outer_size) { } [[nodiscard]] FormalPowerSeries run(const FormalPowerSeries &inner, std::size_t precision) const { return FormalPowerSeries(extract(CompositionDenominator(inner, precision), outer_size_ - 1).values); // [y^(m-1)] (y^(m-1)f(1/y))/(1-yg(x))。 } }; inline FormalPowerSeries ProjectFpsPowers(const FormalPowerSeries &value, const FormalPowerSeries &weight, std::size_t degree, std::size_t count) { if (count == 0) { return {}; } if (degree >= kCompositionCapacity || count > kCompositionCapacity) { throw std::length_error("fps_power_projection: degree or count exceeds NTT budget"); } if (count == 1) { return FormalPowerSeries{weight.get(degree)}; } auto denominator = CompositionDenominator(value, degree + 1); // [x^degree]weight(x)/(1-y*value(x))を取り出す。 CompositionRectangle numerator(degree + 1, 1); for (std::size_t x = 0; x <= degree; ++x) { numerator.at(x, 0) = weight.get(x); } for (; degree > 0; degree /= 2) { const auto stride = 2 * denominator.x_width - 1; const auto width = std::min(count, numerator.y_width + denominator.y_width - 1); const auto product = CompositionProduct(numerator, denominator, stride, 1); CompositionRectangle selected(degree / 2 + 1, width); for (std::size_t y = 0; y < width; ++y) { for (std::size_t x = 0; x < selected.x_width; ++x) { selected.at(x, y) = product[y * stride + 2 * x + degree % 2]; } } numerator = std::move(selected); denominator = CompositionGraeffe(denominator, count); } const FormalPowerSeries upper(std::move(numerator.values)); FormalPowerSeries lower(std::move(denominator.values)); lower.trim(); if (lower.size() == 1) { return upper.prefix(count); } return (upper * lower.inverse(count)).prefix(count); } } inline FormalPowerSeries fps_compose(const FormalPowerSeries &outer, const FormalPowerSeries &inner, std::size_t precision) { if (precision == 0) { return {}; } /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::CheckCompositionPrecision(precision); if (inner.get(0) != 0) { throw std::invalid_argument("fps_compose: inner constant must be zero"); } const auto inner_size = std::min(inner.size(), precision); std::size_t leading = 1; while (leading < inner_size && inner[leading] == 0) { ++leading; } const auto outer_size = std::min(outer.size(), 1 + (precision - 1) / leading); if (leading >= inner_size || outer_size <= 1) { FormalPowerSeries answer(precision); answer.set(0, outer.get(0)); return answer; } return /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::FpsComposer(outer, outer_size).run(inner, precision); } inline FormalPowerSeries fps_compositional_inverse(const FormalPowerSeries &value, std::size_t precision) { if (precision == 0) { return {}; } /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::CheckCompositionPrecision(precision); if (value.get(0) != 0 || value.get(1) == 0) { throw std::invalid_argument("fps_compositional_inverse: expected zero constant and nonzero linear term"); } if (precision == 1) { return FormalPowerSeries(precision); } constexpr auto kMod = FormalPowerSeries::modulus(); FormalPowerSeries result{0, /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fps_power(value.get(1), kMod - 2)}; for (std::size_t width = 2; width < precision;) { const auto target = std::min(2 * width, precision); const auto tail = target - width; const auto composed = fps_compose(value, result, target); std::vector error(tail); std::copy_n(composed.coefficients().data() + width, tail, error.begin()); const auto reciprocal = composed.derivative().inverse(tail); // f'(g)=(f(g))'/g'から上側のNewton補正を計算。 const auto correction = (FormalPowerSeries(std::move(error)) * result.derivative().prefix(tail)).prefix(tail) * reciprocal; result.resize(target); for (std::size_t i = 0; i < tail; ++i) { result.set(width + i, -static_cast(correction[i])); } width = target; } return result; } inline FormalPowerSeries fps_power_projection(const FormalPowerSeries &value, const FormalPowerSeries &weight, std::size_t degree, std::size_t count) { return /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::ProjectFpsPowers(value, weight, degree, count); } #endif #ifndef SIRKIT_POLYNOMIAL_DIFFERENTIAL_OPERATOR_DEFINED #define SIRKIT_POLYNOMIAL_DIFFERENTIAL_OPERATOR_DEFINED namespace detail { inline FormalPowerSeries DifferentialOperatorQuadraticFlow(const FormalPowerSeries &derivative, std::size_t precision) { const auto constant = derivative.get(0); const auto linear = derivative.get(1); const auto negative_quadratic = fps_subtract(0, derivative.get(2)); fps_prepare_inverses(precision - 1); const auto &inverses = fps_integer_inverses(); std::vector u(precision), v(precision); v[0] = 1; // u′=linear*u+constant*v, v′=-quadratic*u とすると u/v が X′=P(X) を満たす。 for (std::size_t i = 1; i < precision; ++i) { u[i] = fps_multiply(fps_add(fps_multiply(linear, u[i - 1]), fps_multiply(constant, v[i - 1])), inverses[i]); v[i] = fps_multiply(fps_multiply(negative_quadratic, u[i - 1]), inverses[i]); } const auto reciprocal = FormalPowerSeries(std::move(v)).inverse(precision); return (FormalPowerSeries(std::move(u)) * reciprocal).prefix(precision); } inline FormalPowerSeries DifferentialOperatorDifferentiate(const FormalPowerSeries &series, std::size_t steps, std::size_t precision) { fps_prepare_inverses(precision + steps - 1); const auto &inverses = fps_integer_inverses(); std::uint32_t falling = 1; for (std::size_t i = 1; i <= steps; ++i) { falling = fps_multiply(falling, static_cast(i)); } std::vector result(precision); for (std::size_t i = 0; i < precision; ++i) { result[i] = fps_multiply(series.get(i + steps), falling); falling = fps_multiply(fps_multiply(falling, static_cast(i + steps + 1)), inverses[i + 1]); } return FormalPowerSeries(std::move(result)); } } // namespace detail [[nodiscard]] inline FormalPowerSeries polynomial_differential_operator_power(const FormalPowerSeries &initial, const FormalPowerSeries &derivative, const FormalPowerSeries &multiplier, std::size_t steps, std::size_t precision) { if (precision > detail::kCompositionCapacity || steps > detail::kCompositionCapacity - precision) { throw std::length_error("differential operator: steps plus precision exceed composition budget"); } if (precision == 0) { return {}; } if (steps == 0) { return initial.prefix(precision); } if (derivative.get(0) == 0) { throw std::invalid_argument("differential operator: zero derivative multiplier at x=0"); } const auto known = precision + steps; auto derivative_part = derivative.prefix(std::min(derivative.size(), known - 1)); derivative_part.trim(); const auto reciprocal = derivative_part.inverse(known - 1); const auto coordinate = reciprocal.integral(); const auto reversed = derivative_part.size() <= 3 ? detail::DifferentialOperatorQuadraticFlow(derivative_part, known) : fps_compositional_inverse(coordinate, known); auto coefficient = multiplier.prefix(known - 1); coefficient.trim(); const auto scale = (coefficient * reciprocal).prefix(known - 1).integral().exp(known); auto initial_part = initial.prefix(known); initial_part.trim(); const auto flat = fps_compose((initial_part * scale).prefix(known), reversed, known); // y′(x)=1/P(x) と U′(x)/U(x)=A(x)/P(x) により、遷移はyに関する微分になる。 const auto applied = detail::DifferentialOperatorDifferentiate(flat, steps, precision); return (fps_compose(applied, coordinate, precision) * scale.inverse(precision)).prefix(precision); } #endif } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 // SIRKIT_NAMESPACE_END_V1 namespace sirkit {} using namespace sirkit; // SPDX-License-Identifier: CC0-1.0 // Verify I/O: Verify/current/common/fast_io.hpp // SPDX-License-Identifier: CC0-1.0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef SIRKIT_VERIFY_FAST_IO_DEFINED #define SIRKIT_VERIFY_FAST_IO_DEFINED // VerifyInput(file=stdin): 非所有のFILEから読む。別の入力方式と混用しない。 // >> 整数: 64bit以下の十進整数を読む。検査省略時は値域内の正しい入力を前提とする。 // >> char/string: 空白を飛ばし、1文字/空白までの文字列を読む。 // >> 浮動小数点: 十進文字列を変換する。通常のCロケールで使う。 // bool / !: 直前までの読み取りが成功しているか/失敗したかを返す。 class VerifyInput { static constexpr std::size_t buffer_size = 1 << 17; std::FILE* file_; std::array buffer_; std::size_t position_ = 0, size_ = 0; bool good_ = true; int peek() { if (position_ == size_) { size_ = std::fread(buffer_.data(), 1, buffer_size, file_); buffer_[size_] = 0; // 補充後の末尾は常に数字でない番兵。 position_ = 0; if (size_ == 0) return EOF; } return buffer_[position_]; } int skip_space() { int c; while ((c = peek()) != EOF && c <= ' ') ++position_; if (c == EOF) good_ = false; return c; } public: explicit VerifyInput(std::FILE* file = stdin) : file_(file) {} VerifyInput(const VerifyInput&) = delete; VerifyInput& operator=(const VerifyInput&) = delete; explicit operator bool() const noexcept { return good_; } bool operator!() const noexcept { return !good_; } template requires (std::is_integral_v && sizeof(T) <= sizeof(std::uint64_t) && !std::is_same_v) VerifyInput& operator>>(T& result) { if (!good_) return *this; int c = skip_space(); if (c == EOF) return *this; bool negative = false; if (c == '-' || c == '+') { negative = c == '-'; ++position_; c = peek(); } if (c == EOF) { good_ = false; return *this; } #ifndef SKIP_RUNTIME_CHECKS if (c < '0' || c > '9') { good_ = false; return *this; } // 先頭の0を除けば、20桁以下の末尾直前の値はuint64_tに収まる。 while (c == '0') { ++position_; c = peek(); } std::uint64_t previous = 0; unsigned last_digit = 0; std::size_t digits = 0; #endif std::uint64_t value = 0; #ifdef SKIP_RUNTIME_CHECKS // バッファ末尾の番兵で内側の範囲判定を省き、数字が境界をまたぐときだけ補充する。 for (;;) { const unsigned char* first = buffer_.data() + position_; const unsigned char* cursor = first; while (static_cast(*cursor - '0') < 10) { value = value * 10 + static_cast(*cursor - '0'); ++cursor; } position_ += static_cast(cursor - first); if (position_ != size_) break; c = peek(); if (c == EOF) break; } #else while (c >= '0' && c <= '9') { previous = value; last_digit = static_cast(c - '0'); if (digits <= 20) ++digits; value = value * 10 + static_cast(c - '0'); ++position_; c = peek(); } #endif #ifndef SKIP_RUNTIME_CHECKS constexpr auto maximum = std::numeric_limits::max(); if ((c != EOF && c > ' ') || digits > 20 || (digits == 20 && (previous > maximum / 10 || (previous == maximum / 10 && last_digit > maximum % 10)))) { good_ = false; return *this; } std::uint64_t limit = static_cast(std::numeric_limits::max()); if constexpr (std::is_signed_v) limit += negative; else if (negative && value != 0) good_ = false; if (value > limit) good_ = false; if (!good_) return *this; #endif if constexpr (std::is_signed_v) { constexpr std::uint64_t minimum_magnitude = static_cast(std::numeric_limits::max()) + 1; // -(最小値)は評価せず、絶対値が最大値+1のときだけ最小値を使う。 result = negative ? (value == minimum_magnitude ? std::numeric_limits::min() : static_cast(-static_cast(value))) : static_cast(value); } else result = static_cast(value); return *this; } VerifyInput& operator>>(char& result) { if (!good_) return *this; const int c = skip_space(); if (c != EOF) { result = static_cast(c); ++position_; } return *this; } VerifyInput& operator>>(std::string& result) { if (!good_) return *this; int c = skip_space(); if (c == EOF) return *this; result.clear(); for (;;) { const std::size_t first = position_; while (position_ < size_ && buffer_[position_] > ' ') ++position_; result.append(reinterpret_cast(buffer_.data() + first), position_ - first); if (position_ != size_ || peek() == EOF) break; } return *this; } template requires std::is_floating_point_v VerifyInput& operator>>(T& result) { std::string token; *this >> token; if (!good_) return *this; #ifndef SKIP_RUNTIME_CHECKS char* end = nullptr; const long double value = std::strtold(token.c_str(), &end); if (end != token.c_str() + token.size()) good_ = false; else result = static_cast(value); #else result = static_cast(std::strtold(token.c_str(), nullptr)); #endif return *this; } }; // VerifyOutput(file=stdout): 非所有のFILEへ書く。破棄時にflushし、FILEは閉じない。 // << 整数/char/文字列/浮動小数点: 値を出力する。整数は64bit以下に対応する。 // fixed(digits): 浮動小数点を小数点以下digits桁で出力する(digits>=0)。 // flush(): バッファの内容をFILEへ渡す。bool / !で書き込み失敗を確認できる。 class VerifyOutput { static constexpr std::size_t buffer_size = 1 << 17; std::FILE* file_; std::array buffer_; std::size_t size_ = 0; int precision_ = 6; bool fixed_ = false, good_ = true; void write(std::string_view text) { while (!text.empty() && good_) { if (size_ == buffer_.size()) flush(); const auto count = std::min(text.size(), buffer_.size() - size_); std::memcpy(buffer_.data() + size_, text.data(), count); size_ += count; text.remove_prefix(count); } } public: explicit VerifyOutput(std::FILE* file = stdout) : file_(file) {} VerifyOutput(const VerifyOutput&) = delete; VerifyOutput& operator=(const VerifyOutput&) = delete; ~VerifyOutput() { flush(); } explicit operator bool() const noexcept { return good_; } bool operator!() const noexcept { return !good_; } VerifyOutput& fixed(int digits) { if (digits < 0) throw std::invalid_argument("negative output precision"); fixed_ = true; precision_ = digits; return *this; } void flush() noexcept { std::size_t written = 0; while (written < size_ && good_) { const auto count = std::fwrite(buffer_.data() + written, 1, size_ - written, file_); if (count == 0) good_ = false; written += count; } size_ = 0; } template requires (std::is_integral_v && sizeof(T) <= sizeof(std::uint64_t) && !std::is_same_v) VerifyOutput& operator<<(T value) { if constexpr (std::is_same_v) return *this << static_cast('0' + value); else { constexpr std::size_t capacity = std::numeric_limits::digits10 + 3; if (buffer_.size() - size_ < capacity) flush(); if (!good_) return *this; const auto end = std::to_chars(buffer_.data() + size_, buffer_.data() + size_ + capacity, value).ptr; size_ = static_cast(end - buffer_.data()); return *this; } } VerifyOutput& operator<<(char value) { if (size_ == buffer_.size()) flush(); if (good_) buffer_[size_++] = value; return *this; } VerifyOutput& operator<<(std::string_view value) { write(value); return *this; } VerifyOutput& operator<<(const std::string& value) { write(value); return *this; } VerifyOutput& operator<<(const char* value) { write(value); return *this; } template requires std::is_floating_point_v VerifyOutput& operator<<(T value) { char local[128]; const char* format = fixed_ ? "%.*Lf" : "%.*Lg"; const int count = std::snprintf(local, sizeof(local), format, precision_, static_cast(value)); if (count < 0) good_ = false; else if (static_cast(count) < sizeof(local)) write(std::string_view(local, static_cast(count))); else { std::vector text(static_cast(count) + 1); std::snprintf(text.data(), text.size(), format, precision_, static_cast(value)); write(std::string_view(text.data(), static_cast(count))); } return *this; } }; inline VerifyInput verify_in; inline VerifyOutput verify_out; #endif #include #include #include #include int main() { std::size_t steps{}, length{}; verify_in >> steps >> length; std::vector multiplier(length); for (auto &coefficient : multiplier) { verify_in >> coefficient; } const auto answer = sirkit::polynomial_differential_operator_power( {0, 1}, {-1, 0, 1}, sirkit::FormalPowerSeries(std::move(multiplier)), steps, steps + 2); for (std::size_t i = 0; i < answer.size(); ++i) { verify_out << answer[i] << (i + 1 == answer.size() ? '\n' : ' '); } }