This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY
#include "../hessenberg_system.hpp"
#include "../../modint.hpp"
#include "../../random/xorshift.hpp"
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
using mint = ModInt<1000000007>;
void test_lower_hessenberg_random() {
for (int t = 0; t < 10000; ++t) {
int N = t / 100;
vector<vector<mint>> A(N, vector<mint>(N));
vector<mint> x(N);
const double p = rand_double();
for (int i = 0; i < N; ++i) {
x[i] = rand_int() % mint::mod();
for (int j = 0; j < i; ++j) A[i][j] = rand_int() % mint::mod();
A[i][i] = 1 + rand_int() % (mint::mod() - 1);
if (i + 1 < N and rand_double() < p) A[i][i + 1] = rand_int() % mint::mod();
}
vector<mint> b(N);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) b[i] += A[i][j] * x[j];
}
assert(x == solve_lower_hessenberg<mint>(A, b));
}
}
void test_upper_hessenberg_random() {
for (int t = 0; t < 10000; ++t) {
int N = t / 100;
vector<vector<mint>> A(N, vector<mint>(N));
vector<mint> x(N);
const double p = rand_double();
for (int i = 0; i < N; ++i) {
x[i] = rand_int() % mint::mod();
for (int j = i + 1; j < N; ++j) A[i][j] = rand_int() % mint::mod();
A[i][i] = 1 + rand_int() % (mint::mod() - 1);
if (i and rand_double() < p) A[i][i - 1] = rand_int() % mint::mod();
}
vector<mint> b(N);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) b[i] += A[i][j] * x[j];
}
assert(x == solve_upper_hessenberg<mint>(A, b));
}
}
void test_lower_hessenberg_hand() {
vector<vector<mint>> A{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}};
vector<mint> b{2, 3, 4}, x{4, 2, 3};
assert(solve_lower_hessenberg<mint>(A, b) == x);
}
int main() {
test_lower_hessenberg_random();
test_lower_hessenberg_hand();
test_upper_hessenberg_random();
cout << "Hello World\n";
}
#line 1 "linear_algebra_matrix/test/hessenberg_system.stress.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY
#line 2 "number/dual_number.hpp"
#include <type_traits>
namespace dual_number_ {
struct has_id_method_impl {
template <class T_> static auto check(T_ *) -> decltype(T_::id(), std::true_type());
template <class T_> static auto check(...) -> std::false_type;
};
template <class T_> struct has_id : decltype(has_id_method_impl::check<T_>(nullptr)) {};
} // namespace dual_number_
// Dual number (二重数)
// Verified: https://atcoder.jp/contests/abc235/tasks/abc235_f
template <class T> struct DualNumber {
T a, b; // a + bx
template <typename T2, typename std::enable_if<dual_number_::has_id<T2>::value>::type * = nullptr>
static T2 _T_id() {
return T2::id();
}
template <typename T2, typename std::enable_if<!dual_number_::has_id<T2>::value>::type * = nullptr>
static T2 _T_id() {
return T2(1);
}
DualNumber(T x = T(), T y = T()) : a(x), b(y) {}
static DualNumber id() { return DualNumber(_T_id<T>(), T()); }
explicit operator bool() const { return a != T() or b != T(); }
DualNumber operator+(const DualNumber &x) const { return DualNumber(a + x.a, b + x.b); }
DualNumber operator-(const DualNumber &x) const { return DualNumber(a - x.a, b - x.b); }
DualNumber operator*(const DualNumber &x) const {
return DualNumber(a * x.a, b * x.a + a * x.b);
}
DualNumber operator/(const DualNumber &x) const {
T cinv = _T_id<T>() / x.a;
return DualNumber(a * cinv, (b * x.a - a * x.b) * cinv * cinv);
}
DualNumber operator-() const { return DualNumber(-a, -b); }
DualNumber &operator+=(const DualNumber &x) { return *this = *this + x; }
DualNumber &operator-=(const DualNumber &x) { return *this = *this - x; }
DualNumber &operator*=(const DualNumber &x) { return *this = *this * x; }
DualNumber &operator/=(const DualNumber &x) { return *this = *this / x; }
bool operator==(const DualNumber &x) const { return a == x.a and b == x.b; }
bool operator!=(const DualNumber &x) const { return !(*this == x); }
bool operator<(const DualNumber &x) const { return (a != x.a ? a < x.a : b < x.b); }
template <class OStream> friend OStream &operator<<(OStream &os, const DualNumber &x) {
return os << '{' << x.a << ',' << x.b << '}';
}
T eval(const T &x) const { return a + b * x; }
T root() const { return (-a) / b; } // Solve a + bx = 0 (b \neq 0 is assumed)
};
#line 3 "linear_algebra_matrix/hessenberg_system.hpp"
#include <algorithm>
#include <cassert>
#include <vector>
// Solve Ax = b, where A is n x n (square), lower Hessenberg, and non-singular.
// Complexity: O(n^2)
// Verified: https://atcoder.jp/contests/abc249/tasks/abc249_h
template <class T>
std::vector<T>
solve_lower_hessenberg(const std::vector<std::vector<T>> &A, const std::vector<T> &b) {
const int N = A.size();
if (!N) return {};
assert(int(A[0].size()) == N and int(b.size()) == N);
using dual = DualNumber<T>;
std::vector<dual> sol(N);
for (int h = 0; h < N;) {
sol[h] = dual(0, 1);
for (int i = h;; ++i) {
dual y = b[i];
for (int j = 0; j <= i; ++j) y -= sol[j] * A[i][j];
T a = i + 1 < N ? A[i][i + 1] : T();
if (a == T()) {
T x0 = y.root();
while (h <= i) sol[h] = sol[h].eval(x0), ++h;
break;
} else {
sol[i + 1] = y / a;
}
}
}
std::vector<T> ret(N);
for (int i = 0; i < N; ++i) ret[i] = sol[i].a;
return ret;
}
// Solve Ax = b, where A is n x n (square), upper Hessenberg, and non-singular
// Complexity: O(n^2)
template <class T>
std::vector<T> solve_upper_hessenberg(std::vector<std::vector<T>> A, std::vector<T> b) {
std::reverse(A.begin(), A.end());
for (auto &v : A) std::reverse(v.begin(), v.end());
std::reverse(b.begin(), b.end());
auto ret = solve_lower_hessenberg(A, b);
std::reverse(ret.begin(), ret.end());
return ret;
}
#line 3 "modint.hpp"
#include <iostream>
#include <set>
#line 6 "modint.hpp"
template <int md> struct ModInt {
using lint = long long;
constexpr static int mod() { return md; }
static int get_primitive_root() {
static int primitive_root = 0;
if (!primitive_root) {
primitive_root = [&]() {
std::set<int> fac;
int v = md - 1;
for (lint i = 2; i * i <= v; i++)
while (v % i == 0) fac.insert(i), v /= i;
if (v > 1) fac.insert(v);
for (int g = 1; g < md; g++) {
bool ok = true;
for (auto i : fac)
if (ModInt(g).pow((md - 1) / i) == 1) {
ok = false;
break;
}
if (ok) return g;
}
return -1;
}();
}
return primitive_root;
}
int val_;
int val() const noexcept { return val_; }
constexpr ModInt() : val_(0) {}
constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; }
constexpr ModInt(lint v) { _setval(v % md + md); }
constexpr explicit operator bool() const { return val_ != 0; }
constexpr ModInt operator+(const ModInt &x) const {
return ModInt()._setval((lint)val_ + x.val_);
}
constexpr ModInt operator-(const ModInt &x) const {
return ModInt()._setval((lint)val_ - x.val_ + md);
}
constexpr ModInt operator*(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.val_ % md);
}
constexpr ModInt operator/(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.inv().val() % md);
}
constexpr ModInt operator-() const { return ModInt()._setval(md - val_); }
constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; }
friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; }
friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; }
friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; }
constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; }
constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; }
constexpr bool operator<(const ModInt &x) const {
return val_ < x.val_;
} // To use std::map<ModInt, T>
friend std::istream &operator>>(std::istream &is, ModInt &x) {
lint t;
return is >> t, x = ModInt(t), is;
}
constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) {
return os << x.val_;
}
constexpr ModInt pow(lint n) const {
ModInt ans = 1, tmp = *this;
while (n) {
if (n & 1) ans *= tmp;
tmp *= tmp, n >>= 1;
}
return ans;
}
static constexpr int cache_limit = std::min(md, 1 << 21);
static std::vector<ModInt> facs, facinvs, invs;
constexpr static void _precalculation(int N) {
const int l0 = facs.size();
if (N > md) N = md;
if (N <= l0) return;
facs.resize(N), facinvs.resize(N), invs.resize(N);
for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i;
facinvs[N - 1] = facs.back().pow(md - 2);
for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1);
for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1];
}
constexpr ModInt inv() const {
if (this->val_ < cache_limit) {
if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0};
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return invs[this->val_];
} else {
return this->pow(md - 2);
}
}
constexpr ModInt fac() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facs[this->val_];
}
constexpr ModInt facinv() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facinvs[this->val_];
}
constexpr ModInt doublefac() const {
lint k = (this->val_ + 1) / 2;
return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac())
: ModInt(k).fac() * ModInt(2).pow(k);
}
constexpr ModInt nCr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv() * ModInt(r).facinv();
}
constexpr ModInt nPr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv();
}
static ModInt binom(int n, int r) {
static long long bruteforce_times = 0;
if (r < 0 or n < r) return ModInt(0);
if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r);
r = std::min(r, n - r);
ModInt ret = ModInt(r).facinv();
for (int i = 0; i < r; ++i) ret *= n - i;
bruteforce_times += r;
return ret;
}
// Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!)
// Complexity: O(sum(ks))
template <class Vec> static ModInt multinomial(const Vec &ks) {
ModInt ret{1};
int sum = 0;
for (int k : ks) {
assert(k >= 0);
ret *= ModInt(k).facinv(), sum += k;
}
return ret * ModInt(sum).fac();
}
// Catalan number, C_n = binom(2n, n) / (n + 1)
// C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ...
// https://oeis.org/A000108
// Complexity: O(n)
static ModInt catalan(int n) {
if (n < 0) return ModInt(0);
return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv();
}
ModInt sqrt() const {
if (val_ == 0) return 0;
if (md == 2) return val_;
if (pow((md - 1) / 2) != 1) return 0;
ModInt b = 1;
while (b.pow((md - 1) / 2) == 1) b += 1;
int e = 0, m = md - 1;
while (m % 2 == 0) m >>= 1, e++;
ModInt x = pow((m - 1) / 2), y = (*this) * x * x;
x *= (*this);
ModInt z = b.pow(m);
while (y != 1) {
int j = 0;
ModInt t = y;
while (t != 1) j++, t *= t;
z = z.pow(1LL << (e - j - 1));
x *= z, z *= z, y *= z;
e = j;
}
return ModInt(std::min(x.val_, md - x.val_));
}
};
template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0};
using ModInt998244353 = ModInt<998244353>;
// using mint = ModInt<998244353>;
// using mint = ModInt<1000000007>;
#line 2 "random/xorshift.hpp"
#include <cstdint>
// CUT begin
uint32_t rand_int() // XorShift random integer generator
{
static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;
uint32_t t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
double rand_double() { return (double)rand_int() / UINT32_MAX; }
#line 8 "linear_algebra_matrix/test/hessenberg_system.stress.test.cpp"
using namespace std;
using mint = ModInt<1000000007>;
void test_lower_hessenberg_random() {
for (int t = 0; t < 10000; ++t) {
int N = t / 100;
vector<vector<mint>> A(N, vector<mint>(N));
vector<mint> x(N);
const double p = rand_double();
for (int i = 0; i < N; ++i) {
x[i] = rand_int() % mint::mod();
for (int j = 0; j < i; ++j) A[i][j] = rand_int() % mint::mod();
A[i][i] = 1 + rand_int() % (mint::mod() - 1);
if (i + 1 < N and rand_double() < p) A[i][i + 1] = rand_int() % mint::mod();
}
vector<mint> b(N);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) b[i] += A[i][j] * x[j];
}
assert(x == solve_lower_hessenberg<mint>(A, b));
}
}
void test_upper_hessenberg_random() {
for (int t = 0; t < 10000; ++t) {
int N = t / 100;
vector<vector<mint>> A(N, vector<mint>(N));
vector<mint> x(N);
const double p = rand_double();
for (int i = 0; i < N; ++i) {
x[i] = rand_int() % mint::mod();
for (int j = i + 1; j < N; ++j) A[i][j] = rand_int() % mint::mod();
A[i][i] = 1 + rand_int() % (mint::mod() - 1);
if (i and rand_double() < p) A[i][i - 1] = rand_int() % mint::mod();
}
vector<mint> b(N);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) b[i] += A[i][j] * x[j];
}
assert(x == solve_upper_hessenberg<mint>(A, b));
}
}
void test_lower_hessenberg_hand() {
vector<vector<mint>> A{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}};
vector<mint> b{2, 3, 4}, x{4, 2, 3};
assert(solve_lower_hessenberg<mint>(A, b) == x);
}
int main() {
test_lower_hessenberg_random();
test_lower_hessenberg_hand();
test_upper_hessenberg_random();
cout << "Hello World\n";
}