Source Code
#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds; // pb_ds
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>


using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define EPS (1e-9)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define debug(x) cerr << #x << ": " << (x) << endl;

void _IO_() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#ifdef ELARABY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

//             N  E  S  W  NW  NE SE SW
//int dr[] = {-1, 0, 1, 0, -1, -1, 1, 1};
//int dc[] = {0, 1, 0, -1, -1, 1, 1, -1};


template<typename T>
void input(vector<T> &v) {
    for (T &i: v) {
        cin >> i;
    }
}

template<typename T>
void output(const vector<T> &v, const int inc = 0) {
    for (int i = 0; i < int(v.size()); ++i) {
        if (i) cout << ' ';
        cout << v[i] + inc;
    }
    cout << '\n';
}

template<typename T>
void output(const vector<pair<T, T>> &v, const int inc1 = 0, const int inc2 = 0) {
    for (int i = 0; i < int(v.size()); ++i) {
        cout << v[i].first + inc1 << ' ' << v[i].second + inc2 << '\n';
    }
}
//////////////////------PROBLEM SOLUTIONS START FROM HERE------///////////////////

vector<int> dp(1e6 + 1, -1);
const ll mod = 1e9 + 7;

int solve(const int idx) {
    if (idx < 0) return 0;
    if (idx == 0) return 1;

    int &ans = dp[idx];
    if (ans != -1) return ans;

    int p1 = solve(idx - 1);
    int p2 = solve(idx - 2);

    return ans = (p1 + p2) % mod;
}

int main() {
    _IO_();
//    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        cout << solve(n) << '\n';
    }

    return 0;
}
Copy
Study Schedule Omar_Elaraby
GNU G++17
149 ms
35.5 MB
Accepted