結果

問題 No.978 Fibonacci Convolution Easy
ユーザー betrue12betrue12
提出日時 2020-01-31 21:36:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 836 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 204,132 KB
実行使用メモリ 19,028 KB
最終ジャッジ日時 2023-10-19 01:11:32
合計ジャッジ時間 3,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 13 ms
9,532 KB
testcase_02 AC 8 ms
6,500 KB
testcase_03 AC 27 ms
18,236 KB
testcase_04 AC 9 ms
7,224 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 11 ms
8,544 KB
testcase_07 AC 18 ms
13,092 KB
testcase_08 AC 14 ms
10,060 KB
testcase_09 AC 21 ms
14,412 KB
testcase_10 AC 29 ms
19,028 KB
testcase_11 AC 10 ms
7,752 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 12 ms
8,544 KB
testcase_14 AC 5 ms
4,916 KB
testcase_15 AC 13 ms
9,268 KB
testcase_16 AC 29 ms
19,028 KB
testcase_17 AC 29 ms
19,028 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
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
    a = (a+b) % MOD;
}
void mul(int64_t& a, int64_t b){
    a = a*b % MOD;
}

int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y){
    int64_t d = a;
    if(b != 0){
        d = extgcd(b, a%b, y, x);
        y -= (a/b) * x;
    }else{
        x = 1; y = 0;
    }
    return d;
}

int64_t inv_mod(int64_t a){
    int64_t x, y;
    extgcd(a, MOD, x, y);
    return (MOD + x%MOD) % MOD;
}

int main(){
    int N, P;
    cin >> N >> P;
    vector<int64_t> A(N);
    A[1] = 1;
    for(int i=2; i<N; i++) A[i] = (P*A[i-1] + A[i-2]) % MOD;

    int64_t S = 0, ans = 0;
    for(int64_t a : A){
        add(S, a);
        add(ans, a*a);
    }
    add(ans, S*S);
    mul(ans, inv_mod(2));
    cout << ans << endl;
    return 0;
}
0