Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define debug(l) cerr<<#l<<' '<<l<<'\n';
- #include "bits/stdc++.h"
- using namespace std;
- #define all(a) a.begin(), a.end()
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef long double ld;
- const ll mod = 1e9 + 7;
- struct Matrix {
- ll n, m;
- vector<vector<ll>> a;
- Matrix(ll _n, ll _m) {
- n = _n, m = _m;
- a.resize(n, vector<ll>(m,0));
- }
- Matrix(ll _n, ll _m, vector<vector<ll>> &b) {
- n = _n, m = _m;
- a.resize(n, vector<ll>(m,0));
- a = b;
- }
- void assign_ed() {
- for (ll i = 0; i < min(n, m); i++)a[i][i] = 1;
- }
- Matrix operator*(const Matrix& other) const {
- vector<vector<ll>> c(other.n, vector<ll>(other.m));
- for (ll k = 0; k < m; k++) {
- for (ll i = 0; i < other.n; i++) {
- for (ll j = 0; j < other.m; j++) {
- c[i][j] += (a[i][k] * other.a[k][j])%mod;
- c[i][j] %= mod;
- }
- }
- }
- return Matrix(other.n, other.m, c);
- }
- };
- Matrix Fastpow(Matrix a, ll n) {
- Matrix res(a.n, a.m);
- res.assign_ed();
- while (n) {
- if (n & 1) {
- res = res * a;
- }
- a = a * a;
- n >>= 1;
- }
- return res;
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Matrix Vec_res(3, 1), Base(3, 3);
- for (int i = 0; i < 3; i++)Base.a[0][i] = 1;
- Base.a[1][0] = 1;
- Base.a[2][1] = 1;
- Vec_res.a[0][0] = 7, Vec_res.a[1][0] = 4, Vec_res.a[2][0] = 2;
- ll n;
- cin >> n;
- n--;
- if (n <= 2) {
- cout << Vec_res.a[2 - n][0];
- }
- else {
- n -= 2;
- Base = Fastpow(Base, n);
- Vec_res = Base * Vec_res;
- cout << Vec_res.a[0][0];
- }
- }
Add Comment
Please, Sign In to add comment