Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/yukicoder/502.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/502
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include "src/Math/ModInt.hpp"
#include "src/FFT/polynomial_matrix_prod.hpp"
using namespace std;
int main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
 static constexpr int MOD= 1e9 + 7;
 using Mint= ModInt<MOD>;
 long long n;
 cin >> n;
 if (n >= MOD) {
  cout << 0 << '\n';
 } else {
  cout << polynomial_matrix_prod<Mint>({{{1, 1}}}, n)[0][0] << '\n';
 }
 return 0;
}
#line 1 "test/yukicoder/502.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/502
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#line 2 "src/Math/mod_inv.hpp"
#include <utility>
#include <type_traits>
#include <cassert>
template <class Uint> constexpr inline Uint mod_inv(Uint a, Uint mod) {
 std::make_signed_t<Uint> x= 1, y= 0, z= 0;
 for (Uint q= 0, b= mod, c= 0; b;) z= x, x= y, y= z - y * (q= a / b), c= a, a= b, b= c - b * q;
 return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
#line 2 "src/Internal/Remainder.hpp"
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
struct MP_Na {  // mod < 2^32
 u32 mod;
 constexpr MP_Na(): mod(0) {}
 constexpr MP_Na(u32 m): mod(m) {}
 constexpr inline u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
 constexpr inline u32 set(u32 n) const { return n; }
 constexpr inline u32 get(u32 n) const { return n; }
 constexpr inline u32 norm(u32 n) const { return n; }
 constexpr inline u32 plus(u64 l, u32 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u32 diff(u64 l, u32 r) const { return l-= r, l >> 63 ? l + mod : l; }
};
template <class u_t, class du_t, u8 B> struct MP_Mo {  // mod < 2^32, mod < 2^62
 u_t mod;
 constexpr MP_Mo(): mod(0), iv(0), r2(0) {}
 constexpr MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
 constexpr inline u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
 constexpr inline u_t set(u_t n) const { return mul(n, r2); }
 constexpr inline u_t get(u_t n) const { return n= reduce(n), n >= mod ? n - mod : n; }
 constexpr inline u_t norm(u_t n) const { return n >= mod ? n - mod : n; }
 constexpr inline u_t plus(u_t l, u_t r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u_t diff(u_t l, u_t r) const { return l-= r, l >> (B - 1) ? l + (mod << 1) : l; }
private:
 u_t iv, r2;
 static constexpr u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
 constexpr inline u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
using MP_Mo32= MP_Mo<u32, u64, 32>;
using MP_Mo64= MP_Mo<u64, u128, 64>;
struct MP_Br {  // 2^20 < mod <= 2^41
 u64 mod;
 constexpr MP_Br(): mod(0), x(0) {}
 constexpr MP_Br(u64 m): mod(m), x((u128(1) << 84) / m) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
 static constexpr inline u64 set(u64 n) { return n; }
 constexpr inline u64 get(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 norm(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 plus(u64 l, u64 r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u64 diff(u64 l, u64 r) const { return l-= r, l >> 63 ? l + (mod << 1) : l; }
private:
 u64 x;
 constexpr inline u128 quo(const u128 &n) const { return (n * x) >> 84; }
 constexpr inline u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
template <class du_t, u8 B> struct MP_D2B1 {  // mod < 2^63, mod < 2^64
 u64 mod;
 constexpr MP_D2B1(): mod(0), s(0), d(0), v(0) {}
 constexpr MP_D2B1(u64 m): mod(m), s(__builtin_clzll(m)), d(m << s), v(u128(-1) / d) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
 constexpr inline u64 set(u64 n) const { return n; }
 constexpr inline u64 get(u64 n) const { return n; }
 constexpr inline u64 norm(u64 n) const { return n; }
 constexpr inline u64 plus(du_t l, u64 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u64 diff(du_t l, u64 r) const { return l-= r, l >> B ? l + mod : l; }
private:
 u8 s;
 u64 d, v;
 constexpr inline u64 rem(const u128 &u) const {
  u128 q= (u >> 64) * v + u;
  u64 r= u64(u) - (q >> 64) * d - d;
  if (r > u64(q)) r+= d;
  if (r >= d) r-= d;
  return r;
 }
};
using MP_D2B1_1= MP_D2B1<u64, 63>;
using MP_D2B1_2= MP_D2B1<u128, 127>;
template <class u_t, class MP> constexpr u_t pow(u_t x, u64 k, const MP &md) {
 for (u_t ret= md.set(1);; x= md.mul(x, x))
  if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
}
#line 3 "src/Internal/modint_traits.hpp"
namespace math_internal {
struct m_b {};
struct s_b: m_b {};
}
template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;
template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;
#line 6 "src/Math/ModInt.hpp"
namespace math_internal {
template <class MP, u64 MOD> struct SB: s_b {
protected:
 static constexpr MP md= MP(MOD);
};
template <class U, class B> struct MInt: public B {
 using Uint= U;
 static constexpr inline auto mod() { return B::md.mod; }
 constexpr MInt(): x(0) {}
 template <class T, typename= enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>>> constexpr MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
 constexpr MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
 constexpr MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
 constexpr MInt name const { \
  MInt ret; \
  return ret.x= op, ret; \
 }
 FUNC(operator+(const MInt & r), B::md.plus(x, r.x))
 FUNC(operator-(const MInt & r), B::md.diff(x, r.x))
 FUNC(operator*(const MInt & r), B::md.mul(x, r.x))
 FUNC(pow(u64 k), math_internal::pow(x, k, B::md))
#undef FUNC
 constexpr MInt operator/(const MInt &r) const { return *this * r.inv(); }
 constexpr MInt &operator+=(const MInt &r) { return *this= *this + r; }
 constexpr MInt &operator-=(const MInt &r) { return *this= *this - r; }
 constexpr MInt &operator*=(const MInt &r) { return *this= *this * r; }
 constexpr MInt &operator/=(const MInt &r) { return *this= *this / r; }
 constexpr bool operator==(const MInt &r) const { return B::md.norm(x) == B::md.norm(r.x); }
 constexpr bool operator!=(const MInt &r) const { return !(*this == r); }
 constexpr bool operator<(const MInt &r) const { return B::md.norm(x) < B::md.norm(r.x); }
 constexpr inline MInt inv() const { return mod_inv<U>(val(), B::md.mod); }
 constexpr inline Uint val() const { return B::md.get(x); }
 friend ostream &operator<<(ostream &os, const MInt &r) { return os << r.val(); }
 friend istream &operator>>(istream &is, MInt &r) {
  i64 v;
  return is >> v, r= MInt(v), is;
 }
private:
 Uint x;
};
template <u64 MOD> using MP_B= conditional_t < (MOD < (1 << 30)) & MOD, MP_Mo32, conditional_t < MOD < (1ull << 32), MP_Na, conditional_t<(MOD < (1ull << 62)) & MOD, MP_Mo64, conditional_t<MOD<(1ull << 41), MP_Br, conditional_t<MOD<(1ull << 63), MP_D2B1_1, MP_D2B1_2>>>>>;
template <u64 MOD> using ModInt= MInt < conditional_t<MOD<(1 << 30), u32, u64>, SB<MP_B<MOD>, MOD>>;
}
using math_internal::ModInt;
#line 2 "src/FFT/polynomial_matrix_prod.hpp"
#include <cmath>
#line 2 "src/FFT/sample_points_shift.hpp"
#include <vector>
#include <algorithm>
#include <numeric>
#line 6 "src/FFT/sample_points_shift.hpp"
#include <cstdint>
#line 2 "src/FFT/NTT.hpp"
#include <array>
#include <limits>
#line 3 "src/NumberTheory/is_prime.hpp"
namespace math_internal {
template <class Uint, class MP, u32... args> constexpr bool miller_rabin(Uint n) {
 const MP md(n);
 const Uint s= __builtin_ctzll(n - 1), d= n >> s, one= md.set(1), n1= md.norm(md.set(n - 1));
 for (u32 a: (u32[]){args...})
  if (Uint b= a % n; b)
   if (Uint p= md.norm(pow(md.set(b), d, md)); p != one)
    for (int i= s; p != n1; p= md.norm(md.mul(p, p)))
     if (!(--i)) return 0;
 return 1;
}
}
constexpr bool is_prime(unsigned long long n) {
 if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
 if (n < (1 << 30)) return math_internal::miller_rabin<unsigned, math_internal::MP_Mo32, 2, 7, 61>(n);
 if (n < (1ull << 62)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_Mo64, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
 if (n < (1ull << 63)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_1, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
 return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_2, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
}
#line 6 "src/FFT/NTT.hpp"
template <class mod_t, size_t LM> mod_t get_inv(int n) {
 static_assert(is_modint_v<mod_t>);
 static const auto m= mod_t::mod();
 static mod_t* dat= new mod_t[LM];
 static int l= 1;
 if (l == 1) dat[l++]= 1;
 for (; l <= n; ++l) dat[l]= dat[m % l] * (m - m / l);
 return dat[n];
}
namespace math_internal {
#define CE constexpr
#define ST static
#define TP template
#define BSF(_, n) __builtin_ctz##_(n)
TP<class mod_t> struct NTT {
#define _DFT(a, b, c, ...) \
 mod_t r, u, *x0, *x1; \
 for (int a= n, b= 1, s, i; a>>= 1; b<<= 1) \
  for (s= 0, r= I, x0= x;; r*= c[BSF(, s)], x0= x1 + p) { \
   for (x1= x0 + (i= p); i--;) __VA_ARGS__; \
   if (++s == e) break; \
  }
 ST inline void dft(int n, mod_t x[]) { _DFT(p, e, r2, x1[i]= x0[i] - (u= r * x1[i]), x0[i]+= u); }
 ST inline void idft(int n, mod_t x[]) {
  _DFT(e, p, ir2, u= x0[i] - x1[i], x0[i]+= x1[i], x1[i]= r * u)
  for (const mod_t iv= I / n; n--;) x[n]*= iv;
 }
#undef _DFT
 ST inline void even_dft(int n, mod_t x[]) {
  for (int i= 0, j= 0; i < n; i+= 2) x[j++]= iv2 * (x[i] + x[i + 1]);
 }
 ST inline void odd_dft(int n, mod_t x[], mod_t r= iv2) {
  for (int i= 0, j= 0;; r*= ir2[BSF(, ++j)])
   if (x[j]= r * (x[i] - x[i + 1]); (i+= 2) == n) break;
 }
 ST inline void dft_doubling(int n, mod_t x[], int i= 0) {
  mod_t k= I, t= rt[BSF(, n << 1)];
  for (copy_n(x, n, x + n), idft(n, x + n); i < n; ++i) x[n + i]*= k, k*= t;
  dft(n, x + n);
 }
protected:
 ST CE u64 md= mod_t::mod();
 static_assert(md & 1);
 static_assert(is_prime(md));
 ST CE u8 E= BSF(ll, md - 1);
 ST CE mod_t w= [](u8 e) {
  for (mod_t r= 2;; r+= 1)
   if (auto s= r.pow((md - 1) / 2); s != 1 && s * s == 1) return r.pow((md - 1) >> e);
  return mod_t();
 }(E);
 static_assert(w != mod_t());
 ST CE mod_t I= 1, iv2= (md + 1) / 2, iw= w.pow((1ULL << E) - 1);
 ST CE auto roots(mod_t w) {
  array<mod_t, E + 1> x= {};
  for (u8 e= E; e; w*= w) x[e--]= w;
  return x[0]= w, x;
 }
 TP<u32 N> ST CE auto ras(const array<mod_t, E + 1>& rt, const array<mod_t, E + 1>& irt, int i= N) {
  array<mod_t, E + 1 - N> x= {};
  for (mod_t ro= 1; i <= E; ro*= irt[i++]) x[i - N]= rt[i] * ro;
  return x;
 }
 ST CE auto rt= roots(w), irt= roots(iw);
 ST CE auto r2= ras<2>(rt, irt), ir2= ras<2>(irt, rt);
};
TP<class T, u8 t, class B> struct NI: public B {
 using B::B;
#define FUNC(op, name, HG, ...) \
 inline void name(__VA_ARGS__) { \
  HG(op, 1); \
  if CE (t > 1) HG(op, 2); \
  if CE (t > 2) HG(op, 3); \
  if CE (t > 3) HG(op, 4); \
  if CE (t > 4) HG(op, 5); \
 }
#define REP for (int i= b; i < e; ++i)
#define DFT(fft, _) B::ntt##_::fft(e - b, this->dt##_ + b)
#define ZEROS(op, _) fill_n(this->dt##_ + b, e - b, typename B::m##_())
#define SET(op, _) copy(x + b, x + e, this->dt##_ + b)
#define SET_S(op, _) this->dt##_[i]= x;
#define SUBST(op, _) copy(r.dt##_ + b, r.dt##_ + e, this->dt##_ + b)
#define ASGN(op, _) REP this->dt##_[i] op##= r.dt##_[i]
#define ASN(nm, op) TP<class C> FUNC(op, nm, ASGN, const NI<T, t, C>& r, int b, int e)
#define BOP(op, _) REP this->dt##_[i]= l.dt##_[i] op r.dt##_[i]
#define OP(nm, op) TP<class C, class D> FUNC(op, nm, BOP, const NI<T, t, C>& l, const NI<T, t, D>& r, int b, int e)
 OP(add, +) OP(dif, -) OP(mul, *) ASN(add, +) ASN(dif, -) ASN(mul, *) FUNC(dft, dft, DFT, int b, int e) FUNC(idft, idft, DFT, int b, int e) FUNC(__, zeros, ZEROS, int b, int e) FUNC(__, set, SET, const T x[], int b, int e) FUNC(__, set, SET_S, int i, T x) TP<class C> FUNC(__, subst, SUBST, const NI<T, t, C>& r, int b, int e) inline void get(T x[], int b, int e) const {
  if CE (t == 1) copy(this->dt1 + b, this->dt1 + e, x + b);
  else REP x[i]= get(i);
 }
#define TMP(_) B::iv##_##1 * (this->dt##_[i] - r1)
 inline T get(int i) const {
  if CE (t > 1) {
   u64 r1= this->dt1[i].val(), r2= (TMP(2)).val();
   T a= 0;
   if CE (t > 2) {
    u64 r3= (TMP(3) - B::iv32 * r2).val();
    if CE (t > 3) {
     u64 r4= (TMP(4) - B::iv42 * r2 - B::iv43 * r3).val();
     if CE (t > 4) a= T(B::m4::mod()) * (TMP(5) - B::iv52 * r2 - B::iv53 * r3 - B::iv54 * r4).val();
     a= (a + r4) * B::m3::mod();
    }
    a= (a + r3) * B::m2::mod();
   }
   return (a + r2) * B::m1::mod() + r1;
  } else return this->dt1[i];
 }
#undef TMP
#undef DFT
#undef ZEROS
#undef SET
#undef SET_S
#undef SUBST
#undef ASGN
#undef ASN
#undef BOP
#undef OP
#undef FUNC
#undef REP
};
#define ARR(_) \
 using m##_= ModInt<M##_>; \
 using ntt##_= NTT<m##_>; \
 m##_* dt##_= new m##_[LM];
#define IV2 ST CE m2 iv21= m2(1) / m1::mod();
#define IV3 ST CE m3 iv32= m3(1) / m2::mod(), iv31= iv32 / m1::mod();
#define IV4 ST CE m4 iv43= m4(1) / m3::mod(), iv42= iv43 / m2::mod(), iv41= iv42 / m1::mod();
#define IV5 ST CE m5 iv54= m5(1) / m4::mod(), iv53= iv54 / m3::mod(), iv52= iv53 / m2::mod(), iv51= iv52 / m1::mod();
TP<u8 t, u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM, bool v> struct NB {
 ARR(1)
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<2, M1, M2, M3, M4, M5, LM, 0> {
 ARR(1) ARR(2) IV2
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<3, M1, M2, M3, M4, M5, LM, 0> {
 ARR(1) ARR(2) ARR(3) IV2 IV3
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<4, M1, M2, M3, M4, M5, LM, 0> {
 ARR(1) ARR(2) ARR(3) ARR(4) IV2 IV3 IV4
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<5, M1, M2, M3, M4, M5, LM, 0> {
 ARR(1) ARR(2) ARR(3) ARR(4) ARR(5) IV2 IV3 IV4 IV5
};
#undef ARR
#define VC(_) \
 using m##_= ModInt<M##_>; \
 using ntt##_= NTT<m##_>; \
 vector<m##_> bf##_; \
 m##_* dt##_;
#define RS resize
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<1, M1, M2, M3, M4, M5, LM, 1> {
 NB(): dt1(bf1.data()) {}
 void RS(int n) { bf1.RS(n), dt1= bf1.data(); }
 u32 size() const { return bf1.size(); }
 VC(1)
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<2, M1, M2, M3, M4, M5, LM, 1> {
 NB(): dt1(bf1.data()), dt2(bf2.data()) {}
 void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(); }
 u32 size() const { return bf1.size(); }
 VC(1) VC(2) IV2
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<3, M1, M2, M3, M4, M5, LM, 1> {
 NB(): dt1(bf1.data()), dt2(bf2.data()), dt3(bf3.data()) {}
 void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(), bf3.RS(n), dt3= bf3.data(); }
 u32 size() const { return bf1.size(); }
 VC(1) VC(2) VC(3) IV2 IV3
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<4, M1, M2, M3, M4, M5, LM, 1> {
 NB(): dt1(bf1.data()), dt2(bf2.data()), dt3(bf3.data()), dt4(bf4.data()) {}
 void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(), bf3.RS(n), dt3= bf3.data(), bf4.RS(n), dt4= bf4.data(); }
 u32 size() const { return bf1.size(); }
 VC(1) VC(2) VC(3) VC(4) IV2 IV3 IV4
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<5, M1, M2, M3, M4, M5, LM, 1> {
 NB(): dt1(bf1.data()), dt2(bf2.data()), dt3(bf3.data()), dt4(bf4.data()), dt5(bf5.data()) {}
 void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(), bf3.RS(n), dt3= bf3.data(), bf4.RS(n), dt4= bf4.data(), bf5.RS(n), dt5= bf5.data(); }
 u32 size() const { return bf1.size(); }
 VC(1) VC(2) VC(3) VC(4) VC(5) IV2 IV3 IV4 IV5
};
#undef VC
#undef IV2
#undef IV3
#undef IV4
#undef IV5
TP<class T, u32 LM> CE bool is_nttfriend() {
 if CE (!is_staticmodint_v<T>) return 0;
 else return (T::mod() & is_prime(T::mod())) && LM <= (1ULL << BSF(ll, T::mod() - 1));
}
TP<class T, enable_if_t<is_arithmetic_v<T>, nullptr_t> = nullptr> CE u64 mv() { return numeric_limits<T>::max(); }
TP<class T, enable_if_t<is_staticmodint_v<T>, nullptr_t> = nullptr> CE u64 mv() { return T::mod(); }
TP<class T, u32 LM, u32 M1, u32 M2, u32 M3, u32 M4> CE u8 nt() {
 if CE (!is_nttfriend<T, LM>()) {
  CE u128 m= mv<T>(), mm= m * m;
  if CE (mm <= M1 / LM) return 1;
  else if CE (mm <= u64(M1) * M2 / LM) return 2;
  else if CE (mm <= u128(M1) * M2 * M3 / LM) return 3;
  else if CE (mm <= u128(M1) * M2 * M3 * M4 / LM) return 4;
  else return 5;
 } else return 1;
}
#undef BSF
#undef RS
CE u32 MOD1= 998244353, MOD2= 897581057, MOD3= 880803841, MOD4= 754974721, MOD5= 645922817;
TP<class T, u32 LM> CE u8 nttarr_type= nt<T, LM, MOD1, MOD2, MOD3, MOD4>();
TP<class T, u32 LM> CE u8 nttarr_cat= is_nttfriend<T, LM>() && (mv<T>() > (1 << 30)) ? 0 : nttarr_type<T, LM>;
TP<class T, u32 LM, bool v> using NTTArray= NI<T, nttarr_type<T, LM>, conditional_t<is_nttfriend<T, LM>(), NB<1, mv<T>(), 0, 0, 0, 0, LM, v>, NB<nttarr_type<T, LM>, MOD1, MOD2, MOD3, MOD4, MOD5, LM, v>>>;
#undef CE
#undef ST
#undef TP
}
using math_internal::is_nttfriend, math_internal::nttarr_type, math_internal::nttarr_cat, math_internal::NTT, math_internal::NTTArray;
template <class T, size_t LM, int id= 0> struct GlobalNTTArray {
 static inline NTTArray<T, LM, 0> bf;
};
template <class T, size_t LM, size_t LM2, int id= 0> struct GlobalNTTArray2D {
 static inline NTTArray<T, LM, 0>* bf= new NTTArray<T, LM, 0>[LM2];
};
template <class T, size_t LM, int id= 0> struct GlobalArray {
 static inline T* bf= new T[LM];
};
constexpr unsigned pw2(unsigned n) { return --n, n|= n >> 1, n|= n >> 2, n|= n >> 4, n|= n >> 8, n|= n >> 16, ++n; }
#line 8 "src/FFT/sample_points_shift.hpp"
template <class mod_t, size_t LM= 1 << 23> std::vector<mod_t> sample_points_shift(const std::vector<mod_t>& y, mod_t c, int m= 1) {
 assert(m <= (int)mod_t::mod()), assert(y.size() <= mod_t::mod());
 static constexpr int TH= (int[]){45, 32, 75, 130, 180, 260}[nttarr_cat<mod_t, LM>];
 if (m == 0) return {};
 uint64_t c_64= c.val(), nc1= (c + (m - 1)).val();
 uint32_t k= y.size(), d= k - 1, i= d, e;
 if (c_64 + m <= k) return std::vector<mod_t>(y.begin() + c_64, y.begin() + c_64 + m);
 mod_t *x= GlobalArray<mod_t, LM, 0>::bf, *p= GlobalArray<mod_t, LM, 1>::bf, *bf;
 for (x[d]= 1; i; i--) x[i - 1]= x[i] * i;
 mod_t t= mod_t(1) / (x[0] * x[0]);
 for (i= d / 2 + 1; i--;) x[i]= x[d - i]= x[i] * x[d - i] * t;
 for (i= k; i--;) x[i]*= y[i];
 for (i= 1; i < k; i+= 2) x[d - i]= -x[d - i];
 auto f= [&](mod_t a, uint32_t n, mod_t ret[]) {
  using GNA1= GlobalNTTArray<mod_t, LM, 1>;
  using GNA2= GlobalNTTArray<mod_t, LM, 2>;
  mod_t* q= GlobalArray<mod_t, LM, 2>::bf;
  for (e= d + n, i= 0, t= a - d; i < e; ++i, t+= 1) ret[i]= t;
  std::partial_sum(ret, ret + e, q, std::multiplies<>());
  for (t= mod_t(1) / q[e - 1]; --i;) q[i]= t * q[i - 1], t*= ret[i];
  if (q[0]= t; k >= TH && n >= TH) {
   const int len= pw2(e + (d > 0));
   GNA1::bf.set(x, 0, k), GNA1::bf.zeros(k, len), GNA1::bf.dft(0, len), GNA2::bf.set(q, 0, e), GNA2::bf.zeros(e, len), GNA2::bf.dft(0, len), GNA1::bf.mul(GNA2::bf, 0, len), GNA1::bf.idft(0, len), GNA1::bf.get(ret - d, d, e);
  } else
   for (std::fill_n(ret, n, mod_t()), i= k; i--;)
    for (int b= d - i, j= n; j--;) ret[j]+= x[i] * q[j + b];
  for (t= a, i= k; --i;) t*= a - i;
  for (; i < n; i++) ret[i]*= t, t*= (a + (i + 1)) * q[i];
  return ret + n;
 };
 if (c_64 < k) {
  if (bf= std::copy_n(y.begin() + c_64, k - c_64, p); nc1 < k) std::copy_n(y.begin(), nc1 + 1, f(k, mod_t::mod() - k, bf));
  else f(k, c_64 + m - k, bf);
 } else if (nc1 < c_64) {
  if (bf= f(c, (-c).val(), p); nc1 < k) std::copy_n(y.begin(), nc1 + 1, bf);
  else f(k, nc1 + 1 - k, std::copy_n(y.begin(), k, bf));
 } else f(c, m, p);
 return std::vector(p, p + m);
}
#line 4 "src/FFT/polynomial_matrix_prod.hpp"
// M(0)*M(1)*...*M(k)
template <class mod_t, std::size_t LM= 1 << 20> std::vector<std::vector<mod_t>> polynomial_matrix_prod(const std::vector<std::vector<std::vector<mod_t>>> &m, std::uint64_t k) {
 using Mat= std::vector<std::vector<mod_t>>;
 using Poly= std::vector<mod_t>;
 const int n= m.size();
 assert(n > 0), assert(n == (int)m[0].size());
 if (!k) {
  Mat ret(n, std::vector<mod_t>(n, mod_t(0)));
  for (int i= n; i--;) ret[i][i]= mod_t(1);
  return ret;
 }
 auto shift= [n](const std::vector<Mat> &G, mod_t x, int d) {
  std::vector<Mat> H(d, Mat(n, std::vector<mod_t>(n)));
  for (int i= n; i--;)
   for (int j= n; j--;) {
    Poly g(G.size());
    for (int l= G.size(); l--;) g[l]= G[l][i][j];
    g= sample_points_shift<mod_t, LM>(g, x, d);
    for (int l= d; l--;) H[l][i][j]= g[l];
   }
  return H;
 };
 auto mult= [n](const Mat &A, const Mat &B) {
  Mat ret(n, std::vector<mod_t>(n, mod_t(0)));
  for (int i= n; i--;)
   for (int k= n; k--;)
    for (int j= n; j--;) ret[i][j]+= A[i][k] * B[k][j];
  return ret;
 };
 auto eval= [](const Poly &f, mod_t x) {
  mod_t ret= mod_t(0);
  for (int l= f.size(); l--;) ret*= x, ret+= f[l];
  return ret;
 };
 int deg= 1, w;
 for (int i= n; i--;)
  for (int j= n; j--;) deg= std::max(deg, (int)m[i][j].size() - 1);
 std::vector<Mat> G(deg + 1, Mat(n, std::vector<mod_t>(n)));
 const int v= pw2((std::sqrt(std::max<std::uint64_t>(1, (k + deg - 1) / deg))));
 mod_t tmp, iv= mod_t(1) / v;
 for (int d= deg + 1, i; d--;)
  for (tmp= mod_t(v) * d, i= n; i--;)
   for (int j= n, l; j--;) G[d][i][j]= eval(m[i][j], tmp);
 for (tmp= v, w= 1; w < v; w<<= 1) {
  auto G0= shift(G, (tmp * deg * w + v) * iv, w * deg);
  auto G1= shift(G, (tmp * deg * w + v + w) * iv, w * deg);
  for (int i= w * deg; i--;) G0[i]= mult(G1[i], G0[i]);
  G1= shift(G, iv * w, w * deg + 1);
  for (int i= w * deg + 1; i--;) G[i]= mult(G1[i], G[i]);
  std::copy(G0.begin(), G0.end(), std::back_inserter(G));
 }
 Mat ret= G[0];
 const int e= std::min<int>(k / v, G.size());
 for (int i= 1; i < e; i++) ret= mult(G[i], ret);
 for (long long i= e * v; i < k; i++) {
  Mat mt(n, std::vector<mod_t>(n, mod_t(0)));
  for (int j= n; j--;)
   for (int l= n, p; l--;)
    for (tmp= eval(m[j][l], i), p= n; p--;) mt[j][p]+= tmp * ret[l][p];
  ret.swap(mt);
 }
 return ret;
}
#line 7 "test/yukicoder/502.test.cpp"
using namespace std;
int main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
 static constexpr int MOD= 1e9 + 7;
 using Mint= ModInt<MOD>;
 long long n;
 cin >> n;
 if (n >= MOD) {
  cout << 0 << '\n';
 } else {
  cout << polynomial_matrix_prod<Mint>({{{1, 1}}}, n)[0][0] << '\n';
 }
 return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-13 00_n0.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 00_n1.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n10.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 00_n100.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n11.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 00_n12.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n13.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n14.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n15.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 00_n16.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n17.txt :heavy_check_mark: AC 16 ms 40 MB
g++-13 00_n18.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n19.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 00_n2.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n20.txt :heavy_check_mark: AC 17 ms 40 MB
g++-13 00_n3.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 00_n4.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 00_n5.txt :heavy_check_mark: AC 17 ms 40 MB
g++-13 00_n6.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 00_n7.txt :heavy_check_mark: AC 16 ms 40 MB
g++-13 00_n8.txt :heavy_check_mark: AC 16 ms 40 MB
g++-13 00_n9.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 20_small1.txt :heavy_check_mark: AC 19 ms 41 MB
g++-13 20_small10.txt :heavy_check_mark: AC 19 ms 41 MB
g++-13 20_small2.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 20_small3.txt :heavy_check_mark: AC 18 ms 41 MB
g++-13 20_small4.txt :heavy_check_mark: AC 18 ms 41 MB
g++-13 20_small5.txt :heavy_check_mark: AC 19 ms 41 MB
g++-13 20_small6.txt :heavy_check_mark: AC 18 ms 41 MB
g++-13 20_small7.txt :heavy_check_mark: AC 19 ms 41 MB
g++-13 20_small8.txt :heavy_check_mark: AC 18 ms 41 MB
g++-13 20_small9.txt :heavy_check_mark: AC 19 ms 41 MB
g++-13 30_medium1.txt :heavy_check_mark: AC 109 ms 47 MB
g++-13 30_medium10.txt :heavy_check_mark: AC 110 ms 47 MB
g++-13 30_medium2.txt :heavy_check_mark: AC 110 ms 47 MB
g++-13 30_medium3.txt :heavy_check_mark: AC 108 ms 47 MB
g++-13 30_medium4.txt :heavy_check_mark: AC 63 ms 44 MB
g++-13 30_medium5.txt :heavy_check_mark: AC 111 ms 47 MB
g++-13 30_medium6.txt :heavy_check_mark: AC 109 ms 47 MB
g++-13 30_medium7.txt :heavy_check_mark: AC 109 ms 47 MB
g++-13 30_medium8.txt :heavy_check_mark: AC 109 ms 47 MB
g++-13 30_medium9.txt :heavy_check_mark: AC 62 ms 44 MB
g++-13 40_large1.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 40_large10.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 40_large2.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 40_large3.txt :heavy_check_mark: AC 17 ms 40 MB
g++-13 40_large4.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 40_large5.txt :heavy_check_mark: AC 16 ms 40 MB
g++-13 40_large6.txt :heavy_check_mark: AC 16 ms 41 MB
g++-13 40_large7.txt :heavy_check_mark: AC 17 ms 40 MB
g++-13 40_large8.txt :heavy_check_mark: AC 17 ms 41 MB
g++-13 40_large9.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n0.txt :heavy_check_mark: AC 16 ms 40 MB
clang++-18 00_n1.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n10.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n100.txt :heavy_check_mark: AC 16 ms 40 MB
clang++-18 00_n11.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n12.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 00_n13.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n14.txt :heavy_check_mark: AC 16 ms 40 MB
clang++-18 00_n15.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n16.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n17.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n18.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n19.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n2.txt :heavy_check_mark: AC 16 ms 40 MB
clang++-18 00_n20.txt :heavy_check_mark: AC 16 ms 40 MB
clang++-18 00_n3.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n4.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n5.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n6.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n7.txt :heavy_check_mark: AC 16 ms 40 MB
clang++-18 00_n8.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 00_n9.txt :heavy_check_mark: AC 16 ms 40 MB
clang++-18 20_small1.txt :heavy_check_mark: AC 19 ms 41 MB
clang++-18 20_small10.txt :heavy_check_mark: AC 19 ms 41 MB
clang++-18 20_small2.txt :heavy_check_mark: AC 18 ms 41 MB
clang++-18 20_small3.txt :heavy_check_mark: AC 19 ms 41 MB
clang++-18 20_small4.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 20_small5.txt :heavy_check_mark: AC 18 ms 41 MB
clang++-18 20_small6.txt :heavy_check_mark: AC 18 ms 41 MB
clang++-18 20_small7.txt :heavy_check_mark: AC 19 ms 41 MB
clang++-18 20_small8.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 20_small9.txt :heavy_check_mark: AC 19 ms 41 MB
clang++-18 30_medium1.txt :heavy_check_mark: AC 84 ms 47 MB
clang++-18 30_medium10.txt :heavy_check_mark: AC 85 ms 47 MB
clang++-18 30_medium2.txt :heavy_check_mark: AC 86 ms 47 MB
clang++-18 30_medium3.txt :heavy_check_mark: AC 82 ms 47 MB
clang++-18 30_medium4.txt :heavy_check_mark: AC 49 ms 44 MB
clang++-18 30_medium5.txt :heavy_check_mark: AC 84 ms 47 MB
clang++-18 30_medium6.txt :heavy_check_mark: AC 84 ms 47 MB
clang++-18 30_medium7.txt :heavy_check_mark: AC 84 ms 47 MB
clang++-18 30_medium8.txt :heavy_check_mark: AC 83 ms 47 MB
clang++-18 30_medium9.txt :heavy_check_mark: AC 49 ms 44 MB
clang++-18 40_large1.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 40_large10.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 40_large2.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 40_large3.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 40_large4.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 40_large5.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 40_large6.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 40_large7.txt :heavy_check_mark: AC 17 ms 41 MB
clang++-18 40_large8.txt :heavy_check_mark: AC 16 ms 41 MB
clang++-18 40_large9.txt :heavy_check_mark: AC 16 ms 40 MB
Back to top page