Hashiryo's Library

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

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/loj/138.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://loj.ac/p/138
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64

#include <iostream>
#include "src/Math/ModInt.hpp"
#include "src/Math/AllPurposeEuclid.hpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 using Mint= ModInt<int(1e9 + 7)>;
 using FST= FloorSumTable<Mint, 10, 10>;
 FST::init();

 int T;
 cin >> T;
 while (T--) {
  int n, a, b, c, k1, k2;
  cin >> n >> a >> b >> c >> k1 >> k2;
  cout << FST::solve(n, a, b, c, k1, k2)[k1][k2] << '\n';
 }
 return 0;
}
#line 1 "test/loj/138.test.cpp"
// competitive-verifier: PROBLEM https://loj.ac/p/138
// 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/Math/AllPurposeEuclid.hpp"
#include <algorithm>
#include <array>
template <typename M> class AllPurposeEuclid {
 using Node= typename M::Node;
 using u64= unsigned long long;
 static inline Node pow(Node x, u64 e) {
  Node ret= M::ti();
  for (; e; e>>= 1, x= M::f(x, x))
   if (e & 1) ret= M::f(ret, x);
  return ret;
 }
 static Node rec(u64 n, u64 a, u64 b, u64 c, const Node &sU, const Node &sR) {
  if (!n) return M::ti();
  if (a >= c) return rec(n, a % c, b, c, sU, M::f(pow(sU, a / c), sR));
  u64 m= ((long double)a * n + b) / c;
  if (!m) return pow(sR, n);
  u64 cnt= n - u64(((long double)c * m - b - 1) / a);
  return M::f(M::f(pow(sR, (c - b - 1) / a), sU), M::f(rec(m - 1, c, (c - b - 1) % a, a, sR, sU), pow(sR, cnt)));
 }
public:
 static Node solve(u64 n, u64 a, u64 b, u64 c) { return M::f(pow(M::sU, b / c), rec(n, a, b % c, c, M::sU, M::sR)); }
};
template <typename int_t, int MXK1, int MXK2> struct FloorSumTable {
 static constexpr int MXK= std::max(MXK1, MXK2) + 1;
 static inline int_t C[MXK][MXK]= {}, pwX[MXK1 + 1]= {1}, pwY[MXK2 + 1]= {1};
 static inline int k1= MXK1, k2= MXK2;
 using u64= unsigned long long;
 struct Monoid {
  struct Node {
   int_t cntU= 0, cntR= 0, v[MXK1 + 1][MXK2 + 1]= {0};
  };
  static inline Node sU, sR;
  static Node ti() { return Node(); }
  static Node f(Node vl, const Node &vr) {
   for (int i= 0; i < k1; i++) pwX[i + 1]= pwX[i] * vl.cntR;
   for (int j= 0; j < k2; j++) pwY[j + 1]= pwY[j] * vl.cntU;
   vl.cntU+= vr.cntU, vl.cntR+= vr.cntR;
   for (int i= 0; i <= k1; i++)
    for (int j= 0; j <= k2; j++)
     for (int k= 0; k <= i; k++)
      for (int l= 0; l <= j; l++) vl.v[i][j]+= pwX[k] * pwY[l] * C[i][k] * C[j][l] * vr.v[i - k][j - l];
   return vl;
  }
 };
 static void init() {
  for (int i= 0; i < MXK; i++) C[i][0]= 1;
  for (int i= 1; i < MXK; i++)
   for (int j= 1; j <= i; j++) C[i][j]= C[i - 1][j] + C[i - 1][j - 1];
  Monoid::sU.cntU= Monoid::sR.cntR= 1;
  for (int i= 0; i <= k1; i++) Monoid::sR.v[i][0]= 1;
 }
 static auto solve(u64 n, u64 a, u64 b, u64 c, int k1_, int k2_) {
  k1= k1_, k2= k2_;
  auto tmp= AllPurposeEuclid<Monoid>::solve(n, a, b, c);
  std::array<std::array<int_t, MXK2 + 1>, MXK1 + 1> ret;
  for (int i= 0; i <= k1; i++)
   for (int j= 0; j <= k2; j++) ret[i][j]= tmp.v[i][j];
  int_t pw= 1, bs= double(b) / c;
  for (int j= 0; j <= k2; j++, pw*= bs) ret[0][j]+= pw;
  return ret;
 }
};
template <class R_t, R_t (*ro)(), R_t (*ri)()> struct RingFloorSum {
 using u64= unsigned long long;
 struct Monoid {
  struct Node {
   R_t u= ri(), r= ri(), v= ro();
  };
  static inline Node sU, sR;
  static Node ti() { return Node(); }
  static Node f(Node vl, const Node &vr) {
   vl.v+= vl.r * vr.v * vl.u, vl.u*= vr.u, vl.r*= vr.r;
   return vl;
  }
 };
 static R_t solve(u64 n, u64 a, u64 b, u64 c, const R_t &A, const R_t &B) {
  Monoid::sU= {B, ri(), ro()}, Monoid::sR= {ri(), A, A};
  return AllPurposeEuclid<Monoid>::solve(n, a, b, c).v;
 }
};
#line 8 "test/loj/138.test.cpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 using Mint= ModInt<int(1e9 + 7)>;
 using FST= FloorSumTable<Mint, 10, 10>;
 FST::init();

 int T;
 cin >> T;
 while (T--) {
  int n, a, b, c, k1, k2;
  cin >> n >> a >> b >> c >> k1 >> k2;
  cout << FST::solve(n, a, b, c, k1, k2)[k1][k2] << '\n';
 }
 return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-13 2 :heavy_check_mark: AC 16 ms 4 MB
g++-13 4 :heavy_check_mark: AC 15 ms 4 MB
g++-13 9 :heavy_check_mark: AC 127 ms 4 MB
clang++-18 2 :heavy_check_mark: AC 18 ms 4 MB
clang++-18 4 :heavy_check_mark: AC 17 ms 4 MB
clang++-18 9 :heavy_check_mark: AC 129 ms 4 MB
Back to top page