結果

問題 No.1275 綺麗な式
ユーザー longrunlongrun
提出日時 2020-11-04 20:50:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 5,060 bytes
コンパイル時間 2,334 ms
コンパイル使用メモリ 211,216 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 15:56:21
合計ジャッジ時間 6,314 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,384 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 1 ms
4,376 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll MOD = 1000000007;

struct mint
{
    ll x;
    mint(ll x = 0) : x(x % MOD) {}
    mint& operator+=(const mint rh) { if((x += rh.x) >= MOD) x -= MOD; return *this; }
    mint& operator-=(const mint rh) { if((x += MOD-rh.x) >= MOD) x -= MOD; return *this; }
    mint& operator*=(const mint rh) { (x *= rh.x) %= MOD; return *this; }
    mint operator+(const mint rh) const { return mint(*this) += rh; }
    mint operator-(const mint rh) const { return mint(*this) -= rh; }
    mint operator*(const mint rh) const { return mint(*this) *= rh; }
    mint pow(ll k) const
    {
        if(!k) return 1;
        mint a = x;
        mint res = 1;
        while(k)
        {
            if(k & 1) res *= a;
            k >>= 1;
            a *= a;
        }
        return res;
    }
    mint inv() const { return pow(MOD-2); }
    mint& operator/=(const mint rh) { return *this *= rh.inv(); }
    mint operator/(const mint rh) { return mint(*this) /= rh; }
};

template <typename T>
struct matrix
{
    int r, c;
    vector<vector<T>> mat;
    //単位行列
    static matrix e(int n) { matrix res(n, n); for(int i=0; i<n; i++) res[i][i] = (T)1; return res; }

    //コンストラクタ
    matrix() = default;
    matrix(int r, int c, T val = 0) : r(r), c(c), mat(r, vector<T>(c,val)) {}
    matrix(vector<vector<T>> mat) : r((int)mat.size()), c((int)mat[0].size()), mat(mat) {}

    matrix operator-() const { matrix B = mat; for(int i=0; i<r; i++) for(int j=0; j<c; j++) B.mat[i][j] = -B.mat[i][j]; return B; }
    //添え字演算子
    vector<T> operator[](const int i) const { return mat[i]; }
    vector<T>& operator[](const int i) { return mat[i]; }

    //比較演算子
    bool operator==(matrix B) { if(r != B.r || c != B.c) return false; for(int i=0; i<r; i++) for(int j=0; j<c; j++) if(mat[i][j] != B.mat[i][j]) return false; return true; }
    bool operator!=(matrix B) { return !((*this) == B); }
    //スカラ演算
    matrix& operator+=(const T k) { for(int i=0; i<r; i++) for(int j=0; j<c; j++) mat[i][j] += k; return *this; }
    matrix& operator-=(const T k) { for(int i=0; i<r; i++) for(int j=0; j<c; j++) mat[i][j] -= k; return *this; }
    matrix& operator*=(const T k) { for(int i=0; i<r; i++) for(int j=0; j<c; j++) mat[i][j] *= k; return *this; }
    matrix operator+(const T k) const { return matrix(*this) += k; }
    matrix operator-(const T k) const { return matrix(*this) -= k; }
    matrix operator*(const T k) const { return matrix(*this) *= k;}
    //行列と行列の演算
    matrix& operator=(const matrix B) { mat = B.mat, r = B.r, c = B.c; return *this; }
    matrix& operator+=(const matrix B) { assert(r == B.r && c == B.c); for(int i=0; i<r; i++) for(int j=0; j<c; j++) mat[i][j] += B.mat[i][j]; return *this; }
    matrix& operator-=(const matrix B) { *this += (-B); return *this; }
    matrix& operator*=(const matrix B)
    {
        assert(c == B.r);
        vector<vector<T>> m(r, vector<T>(B.c, 0));
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<B.c; j++)
            {
                T sum = 0;
                for(int k=0; k<c; k++) sum += (*this)[i][k] * B[k][j];
                m[i][j] = sum;
            }
        }
        c = B.c;
        mat = m;
        return *this;
    }
    matrix operator+(const matrix B) const { return matrix(*this) += B; }
    matrix operator-(const matrix B) const { return matrix(*this) -= B; }
    matrix operator*(const matrix B) const { return matrix(*this) *= B; }
    matrix pow(long long k) const
    {
        assert(k >= 0);
        assert(c == r);
        matrix x = mat;
        matrix res = e(r);
        while(k)
        {
            if(k & 1) res = (res * x);
            k >>= 1;
            x *= x;
        }
        return res;
    }
    matrix inv() const {}
    matrix& operator/=(const matrix B) { (*this) *= B.inv(); return *this; }
    matrix& operator/(const matrix B) const { return matrix(*this) /= B; }
    //変形する操作
    matrix& transpose() { vector<vector<T>> res(c, vector<T>(r)); for(int i=0; i<r; i++) for(int j=0; j<c; j++) res[j][i] = mat[i][j]; mat = res; return *this; }
    matrix& rotate(int n = 1) {}
    //変形しない操作
    matrix transposed() const { return matrix(*this).transpose();}
    matrix rotated() const { return matrix(*this).rotate(); }

    T diterminant() const {}
    pair<int, int> size() const { return make_pair(r, c); }
    void show(int wid = 2) const
    {
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<c; j++)
            {
                cout << mat[i][j] << setw(wid);
            }
            cout << "\n";
        }
        cout << "\n";
    }
};


int main()
{
    ll a, b, n;
    cin >> a >> b >> n;
    mint a1(2), a2(2*a);
    matrix<mint> t(2, 2);
    t[0][0] = 2*a;
    t[0][1] = mint(mint(b) - mint(a*a));
    t[1][0] = mint(1);
    t[1][1] = mint(0);
    t = t.pow(n);
    mint ans = t[1][0] * a2 + t[1][1] * a1;
    cout << ans.x << endl;

    return 0;
}
0