結果
| 問題 |
No.1105 Many Triplets
|
| コンテスト | |
| ユーザー |
KoD
|
| 提出日時 | 2020-07-03 22:09:25 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 7,214 bytes |
| コンパイル時間 | 3,350 ms |
| コンパイル使用メモリ | 119,676 KB |
| 最終ジャッジ日時 | 2025-01-11 14:42:37 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) { lhs = rhs; return true; }
return false;
}
template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) { lhs = rhs; return true; }
return false;
}
struct range {
using itr = int64_t;
struct iterator {
itr i;
constexpr iterator(itr i_) noexcept : i(i_) { }
constexpr void operator ++ () noexcept { ++i; }
constexpr itr operator * () const noexcept { return i; }
constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
};
const iterator l, r;
constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
constexpr iterator begin() const noexcept { return l; }
constexpr iterator end() const noexcept { return r; }
};
struct revrange {
using itr = int64_t;
struct iterator {
itr i;
constexpr iterator(itr i_) noexcept : i(i_) { }
constexpr void operator ++ () noexcept { --i; }
constexpr itr operator * () const noexcept { return i; }
constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
};
const iterator l, r;
constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
constexpr iterator begin() const noexcept { return r; }
constexpr iterator end() const noexcept { return l; }
};
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;
template <class T>
class matrix: public std::vector<std::vector<typename T::value_type>> {
public:
using value_type = typename T::value_type;
using addition = typename T::addition;
using multiplication = typename T::multiplication;
private:
addition add;
multiplication mult;
public:
int height, width;
matrix(): add(addition()), mult(multiplication()) { }
matrix(int height_, int width_, const value_type &initial_ = addition().identity):
add(addition()), mult(multiplication()),
height(height_), width(width_),
std::vector<std::vector<value_type>>(height_, std::vector<value_type>(width_, initial_))
{ }
matrix(const std::vector<std::vector<value_type>> &data_):
add(addition()), mult(multiplication()),
height(data_.size()), width(data_.front().size()),
std::vector<std::vector<value_type>>(data_)
{ }
matrix operator + (const matrix &rhs) const { return matrix(*this) += rhs; }
matrix& operator += (const matrix &rhs) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
(*this)[i][j] = add((*this)[i][j], rhs[i][j]);
}
}
return *this;
}
matrix& operator *= (const matrix &rhs) { *this = (*this) * rhs; return *this; }
matrix operator * (const matrix &rhs) const {
matrix res(height, rhs.width);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < rhs.width; ++j) {
for (int k = 0; k < width; ++k) {
res[i][j] = add(res[i][j], mult((*this)[i][k], rhs[k][j]));
}
}
}
return res;
}
matrix operator * (const value_type &rhs) const { return matrix(*this) *= rhs; }
matrix& operator *= (const value_type &rhs) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
(*this)[i][j] = mult((*this)[i][j], rhs);
}
}
return *this;
}
matrix power(unsigned long long pow) const {
matrix res(height, width), use(*this);
for (int i = 0; i < height; ++i) {
res[i][i] = mult.identity;
}
while (pow > 0) {
if (pow & 1) {
res *= use;
}
use *= use;
pow >>= 1;
}
return res;
}
};
template <class T>
struct number {
using value_type = T;
struct addition {
value_type identity = value_type(0);
value_type operator () (const value_type &x, const value_type &y) const {
return x + y;
}
};
struct multiplication {
value_type identity = value_type(1);
value_type operator () (const value_type &x, const value_type &y) const {
return x * y;
}
};
};
template <uint32_t Modulus>
class modular {
public:
using value_type = uint32_t;
using max_type = uint64_t;
static constexpr value_type mod = Modulus;
static constexpr value_type get_mod() { return mod; }
static_assert(mod >= 2, "invalid mod :: smaller than 2");
static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31");
template <class T>
static constexpr value_type normalize(T value_) {
if (value_ < 0) {
value_ = -value_;
value_ %= mod;
if (value_ == 0) return 0;
return mod - value_;
}
return value_ % mod;
}
private:
value_type value;
public:
constexpr modular(): value(0) { }
template <class T>
explicit constexpr modular(T value_): value(normalize(value_)) { }
template <class T>
explicit constexpr operator T() { return static_cast<T>(value); }
constexpr value_type get() const { return value; }
constexpr modular operator - () const { return modular(mod - value); }
constexpr modular operator ~ () const { return inverse(); }
constexpr value_type &extract() { return value; }
constexpr modular inverse() const { return power(mod - 2); }
constexpr modular power(max_type exp) const {
modular res(1), mult(*this);
while (exp > 0) {
if (exp & 1) res *= mult;
mult *= mult;
exp >>= 1;
}
return res;
}
constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; }
constexpr modular& operator += (const modular &rhs) {
if ((value += rhs.value) >= mod) value -= mod;
return *this;
}
constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; }
constexpr modular& operator -= (const modular &rhs) {
if ((value += mod - rhs.value) >= mod) value -= mod;
return *this;
}
constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; }
constexpr modular& operator *= (const modular &rhs) {
value = (max_type) value * rhs.value % mod;
return *this;
}
constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; }
constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); }
constexpr bool zero() const { return value == 0; }
constexpr bool operator == (const modular &rhs) const { return value == rhs.value; }
constexpr bool operator != (const modular &rhs) const { return value != rhs.value; }
friend std::ostream& operator << (std::ostream &stream, const modular &rhs) {
return stream << rhs.value;
}
};
using m32 = modular<1000000007>;
int main() {
i64 N;
std::cin >> N;
i32 a, b, c;
std::cin >> a >> b >> c;
using mat = matrix<number<m32>>;
mat mult({
{ m32(1), m32(0), m32(-1) },
{ m32(-1), m32(1), m32(0) },
{ m32(0), m32(-1), m32(1) },
});
mat start({
{ m32(a), m32(b), m32(c) }
});
start *= mult.power(N - 1);
std::cout << start[0][0] << ' ' << start[0][1] << ' ' << start[0][2] << '\n';
return 0;
}
KoD