結果

問題 No.891 隣接3項間の漸化式
ユーザー tactac
提出日時 2019-09-21 18:20:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,427 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 173,532 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 06:41:11
合計ジャッジ時間 3,064 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }


typedef vector<ll> vec;
typedef vector<vector<ll> > mat;

int m;

const ll mod = 1e9 + 7;

vec matmul(vec &dp, mat &mt) {
    vec ret(m, 0);
    for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j) {
        (ret[i] += mt[i][j] * dp[j]) %= mod;
    }
    return ret;
}

mat update(mat &mt) {
    mat ret(m, vec(m, 0));
    for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j)
        for (int k = 0; k < m; ++k) {
        (ret[i][j] += mt[i][k] * mt[k][j]) %= mod;
    }
    return ret;
}

void matpow(vec &dp, mat &mt, ll k) {
    m = (int)dp.size();
    while (k) {
        if (k & 1) dp = matmul(dp, mt);
        mt = update(mt);
        k /= 2;
    }
}


ll modpow(ll n, int k) {
    ll ans = 1;
    while (k) {
        if (k & 1) (ans *= n) %= mod;
        (n *= n) %= mod;
        k >>= 1;
    }
    return ans;
}

int main() {
    int a, b, n;
    cin >> a >> b >> n;
    
    if (n == 0) {
        cout << 0 << endl;
        return 0;
    }
    if (n == 1) {
        cout << 1 << endl;
        return 0;
    }
    
    if (a == 0 && b == 0) {
        cout << 0 << endl;
        return 0;
    } else if (a == 0) {
        if (n % 2 == 0) {
            cout << 0 << endl;
        } else {
            cout << modpow(b, n / 2) << endl;
        }
        return 0;
    } else if (b == 0) {
        cout << modpow(a, n - 1) << endl;
        return 0;
    }
    
    
    vec dp(2);
    dp[0] = 1;
    dp[1] = 0;
    
    mat mt(2, vec(2));
    mt[0][0] = a; mt[0][1] = b;
    mt[1][0] = 1; mt[1][1] = 0;
    
    matpow(dp, mt, n - 1);
    
    cout << dp[0] << endl;
    
    
    // rep(i, 2) { rep(j, 2) cout << mt[i][j] << " "; cout << endl; }
}
0