結果

問題 No.978 Fibonacci Convolution Easy
ユーザー Ngô Lê HoàngNgô Lê Hoàng
提出日時 2024-01-05 19:07:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,251 bytes
コンパイル時間 1,651 ms
コンパイル使用メモリ 167,664 KB
実行使用メモリ 19,144 KB
最終ジャッジ日時 2024-01-05 19:07:16
合計ジャッジ時間 4,662 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
19,144 KB
testcase_01 AC 14 ms
19,144 KB
testcase_02 AC 10 ms
19,144 KB
testcase_03 RE -
testcase_04 AC 11 ms
19,144 KB
testcase_05 AC 8 ms
19,144 KB
testcase_06 AC 12 ms
19,144 KB
testcase_07 RE -
testcase_08 AC 16 ms
19,144 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 13 ms
19,144 KB
testcase_12 AC 7 ms
19,144 KB
testcase_13 AC 12 ms
19,144 KB
testcase_14 AC 7 ms
19,144 KB
testcase_15 AC 12 ms
19,144 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 7 ms
19,144 KB
testcase_19 AC 7 ms
19,144 KB
testcase_20 AC 6 ms
19,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep_r(i,a,b) for(int i=a;i>=b;i--)
#define each(a,x) for (auto& x : a)
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define fi first    
#define se second
#define sz(x) int(x.size())
#define so(x) sort(all(x))
#define so_r(x) sort(all(x),greater<int>())
#define zero(x) memset(x,0,sizeof(x))
#define pb push_back
#define lb lower_bound
#define ub upper_bound
const char nl = '\n';
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int bit_cnt(int x){
    return __builtin_popcount(x);
}
ll bex(ll a, ll b, ll mod = 1e9 + 7){ll res = 1LL; while(b){ if (b&1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;}
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

// armistcxy
const int MOD = 1e9 + 7; // 998244353
const int N = 1e6 + 5;
const ll INF = 1e18;

struct mi {
    ll v; explicit operator ll() const { return v; }
    mi() { v = 0; }
    mi(ll _v) { 
        v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
        if (v < 0) v += MOD;
    }
    friend bool operator==(const mi& a, const mi& b) { 
        return a.v == b.v; }
    friend bool operator!=(const mi& a, const mi& b) { 
        return !(a == b); }
    friend bool operator<(const mi& a, const mi& b) { 
        return a.v < b.v; }
   
    mi& operator+=(const mi& m) { 
        if ((v += m.v) >= MOD) v -= MOD; 
        return *this; }
    mi& operator-=(const mi& m) { 
        if ((v -= m.v) < 0) v += MOD; 
        return *this; }
    mi& operator*=(const mi& m) { 
        v = v*m.v%MOD; return *this; }
    mi& operator/=(const mi& m) { return (*this) *= inv(m); }
    friend mi pow(mi a, ll p) {
        mi ans = 1; assert(p >= 0);
        for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend mi inv(const mi& a) { assert(a.v != 0); 
        return pow(a,MOD-2); }
        
    mi operator-() const { return mi(-v); }
    mi& operator++() { return *this += 1; }
    mi& operator--() { return *this -= 1; }
    mi operator++(int) { v++; if (v == MOD) v = 0; return mi(v); }
    mi operator--(int) { v--; if (v < 0) v = MOD-1; return mi(v); }
    friend mi operator+(mi a, const mi& b) { return a += b; }
    friend mi operator-(mi a, const mi& b) { return a -= b; }
    friend mi operator*(mi a, const mi& b) { return a *= b; }
    friend mi operator/(mi a, const mi& b) { return a /= b; }
    friend ostream& operator<<(ostream& os, const mi& m) {
        os << m.v; return os;
    }
    friend istream& operator>>(istream& is, mi& m) {
        ll x; is >> x;
        m.v = x;
        return is;
    }
};
mi a[N], pref[N];
void solve(){
    int n; cin >> n;
    int p; cin >> p;
    a[1] = 0, a[2] = 1;
    rep(i,3,n) a[i] = (p * a[i - 1] + a[i-2]);
    mi ans = 0, cur = 0;
    rep(i,1,n){
        cur += a[i];
        ans += a[i] * cur;
    }
    cout << ans << nl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1; //cin >> t;
    while (t--){
        solve();
    }
}
0