結果

問題 No.891 隣接3項間の漸化式
ユーザー roundplusroundplus
提出日時 2019-09-22 02:19:19
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,870 bytes
コンパイル時間 1,030 ms
コンパイル使用メモリ 105,448 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 12:56:04
合計ジャッジ時間 2,941 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 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,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 1 ms
4,384 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,384 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,384 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 AC 1 ms
4,384 KB
testcase_41 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <limits>
#include <numeric>
#include <queue>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define INF (1ll<<60)
#define INFint (1<<30)
#define BOUND 27182818284
#define MAT 2

typedef long long ll;
typedef long long int lli;
typedef pair<ll, ll> P;

ll MOD=1000000007;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for(int i=int(a);i<int(b);++i)

template<class T>
bool umax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool umin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

// gcd
template<typename T>
T gcd(T a, T b) {
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int findGCD(int arr[], int n) {
    int result = arr[0];
    for (int i = 1; i < n; i++)
        result = gcd(arr[i], result);
    return result;
}

template<typename T>
T lcm(T m, T n) {
    // 引数に0がある場合は0を返す
    if ((0 == m) || (0 == n))
        return 0;
    return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    fill((T *) array, (T *) (array + N), val);
}


int dx[5] = {1, 0, -1, 0};
int dy[5] = {0, 1, 0, -1};

// v.front() = -BOUND;
// v.back() = BOUND;

//struct edge{
//    int cost, to;
//
//    edge(int in_cost, int in_to){
//        cost=in_cost;
//        to=in_to;
//    }
//    bool operator<(const edge &a) const
//    {
//        return cost > a.cost;
//    }
//};
class Mat{

    static Mat mulmat(Mat &a, Mat &b,int n=MAT){
        ll mo2=4*MOD*MOD;
        Mat r;
        rep(x,n) rep(y,n) r.v[x][y]=0;
        rep(x,n) rep(z,n) rep(y,n) {
                    r.v[x][y] += a.v[x][z]*b.v[z][y];
                    if(r.v[x][y]>mo2) r.v[x][y] -= mo2;
                }
        rep(x,n) rep(y,n) r.v[x][y]%=MOD;
        return r;
    }

    static ll modpow(ll a, ll n = MOD-2) {
        ll r=1;
        while(n){
            r=r*((n%2)?a:1)%MOD;
            a=a*a%MOD;
            n>>=1;
        }
        return r;
    }

public:
    Mat(){
        for(auto & i : v)
            for(long long & j : i)
                j=0;
    }

    long long v[MAT][MAT]{};

    static Mat powmat(ll p, Mat a,int n=MAT) {
        Mat r;
        rep(x,n) rep(y,n) r.v[x][y]=0;
        rep(i,n) r.v[i][i]=1;
        while(p) {
            if(p%2) r=mulmat(r,a,n);
            a=mulmat(a,a,n);
            p>>=1;
        }
        return r;
    }
};
int main() {
    int A,B,N; cin >> A >> B >> N;
    Mat V;
    V.v[1][0]=1;
    V.v[0][0]=A;
    V.v[0][1]=B;
    Mat W;
    W=V.powmat(N,V);
    cout << W.v[1][0] << endl;
    return 0;
}
0