結果

問題 No.891 隣接3項間の漸化式
ユーザー tactac
提出日時 2019-09-21 18:10:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,435 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 173,860 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-19 06:40:40
合計ジャッジ時間 5,676 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 (a == 0 || b == 0) {
            if (a == 0 && b == 0) {
                cout << 0 << endl;
            } else if (a == 0) {
                if (n % 2 == 0) {
                    cout << 0 << endl;
                } else {
                    cout << modpow(b, n / 2) << endl;
                }
            } else if (b == 0) {
                cout << modpow(a, n - 1) << endl;
            }
            return 0;
        }
    }
    
    vec dp(2);
    dp[1] = 1;
    dp[0] = 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);
    
    ll ans = 0;
    rep(i, 2) (ans += dp[i]) %= mod;
    cout << ans << endl;
    
    // rep(i, 2) { rep(j, 2) cout << mt[i][j] << " "; cout << endl; }
}
0