結果
| 問題 |
No.1105 Many Triplets
|
| コンテスト | |
| ユーザー |
merom686
|
| 提出日時 | 2020-07-04 02:10:29 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 3,485 bytes |
| コンパイル時間 | 1,104 ms |
| コンパイル使用メモリ | 96,044 KB |
| 最終ジャッジ日時 | 2025-01-11 15:19:37 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;
struct ModInt {
ModInt() : i(0) {}
ModInt(ll k) : i(k % Mod) {
if (i < 0) i += Mod;
}
ModInt operator+(ModInt m) const {
ModInt r;
r.i = i + m.i;
if (r.i >= Mod) r.i -= Mod;
return r;
}
ModInt operator-(ModInt m) const {
ModInt r;
r.i = i - m.i;
if (r.i < 0) r.i += Mod;
return r;
}
ModInt operator*(ModInt m) const {
ModInt r;
r.i = (ll)i * m.i % Mod;
return r;
}
ModInt &operator+=(ModInt m) {
i += m.i;
if (i >= Mod) i -= Mod;
return *this;
}
ModInt &operator-=(ModInt m) {
i -= m.i;
if (i < 0) i += Mod;
return *this;
}
ModInt &operator*=(ModInt m) {
i = (ll)i * m.i % Mod;
return *this;
}
ModInt operator-() const {
ModInt r;
r.i = i == 0 ? 0 : Mod - i;
return r;
}
bool operator!() const {
return !i;
}
ModInt pow(ll k) const {
ModInt r = 1, t = *this;
for (; k != 0; k /= 2) {
if (k & 1) r *= t;
t *= t;
}
return r;
}
////Modが素数のときのみ
//ModInt inv() const {
// return pow(Mod - 2);
//}
//ModInt operator/(ModInt m) const {
// return *this * m.inv();
//}
//ModInt &operator/=(ModInt m) {
// return *this *= m.inv();
//}
constexpr static inline int Mod = 1000000007;
int i;
};
ostream &operator<<(ostream &os, const ModInt &m) {
os << m.i;
return os;
}
istream &operator>>(istream &is, ModInt &m) {
is >> m.i;
return is;
}
template <class T>
struct Matrix {
Matrix(int n, int m) : a(n * m), n(n), m(m) {}
static Matrix E(int n) {
Matrix r(n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
r[i][j] = i == j;
}
}
return r;
}
Matrix pow(ll k) const {
Matrix r = E(n), t = *this;
for (; k != 0; k /= 2) {
if (k & 1) r = r * t;
t = t * t;
}
return r;
}
Matrix operator*(const Matrix &x) const {
if (m != x.n) throw;
Matrix r(n, x.m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < x.m; j++) {
T t = 0;
for (int k = 0; k < m; k++) {
t += (*this)[i][k] * x[k][j];
}
r[i][j] = t;
}
}
return r;
}
const T *operator[](int i) const {
return &a[i * m];
}
T *operator[](int i) {
return &a[i * m];
}
vector<T> a;
int n, m;
};
using Mat = Matrix<ModInt>;
ostream &operator<<(ostream &os, const Mat &x) {
for (int i = 0; i < x.n; i++) {
for (int j = 0; j < x.m; j++) {
cout << x[i][j] << " \n"[j == x.m - 1];
}
}
return os;
}
int main() {
ll n;
cin >> n;
Mat v(3, 1), x(3, 3);
int p[3] = { 1, -1, 0 };
for (int i = 0; i < 3; i++) {
cin >> v[i][0];
for (int j = 0; j < 3; j++) {
x[i][(j + i) % 3] = p[j];
}
}
v = x.pow(n - 1) * v;
for (int i = 0; i < 3; i++) {
cout << v[i][0] << " \n"[i == 2];
}
return 0;
}
merom686