Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) (x).begin(),(x).end()
- using namespace std;
- using ll = long long;
- using vvll = vector<vector<ll>>;
- const ll MOD = 1e9 + 7;
- void Mult(vvll& A, vvll& B, vvll& C, int n) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- A[i][j] = 0;
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- for (int k = 0; k < n; k++) {
- A[i][k] += B[i][j] * C[j][k];
- if (A[i][k] >= MOD) {
- A[i][k] %= MOD;
- }
- }
- }
- }
- }
- void Set(vvll& a, vvll& b, int n) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- a[i][j] = b[i][j];
- }
- }
- }
- vector<vector<ll>> A, ANS, Buffer1, Buffer2;
- int main() {
- ll n;
- int c;
- cin >> n >> c;
- c++;
- A.assign(c, vector<ll>(c, 0));
- ANS.assign(c, vector<ll>(c, 0));
- Buffer1.assign(c, vector<ll>(c, 0));
- Buffer2.assign(c, vector<ll>(c, 0));
- for (int i = 0; i < c; i++) {
- ANS[i][i] = 1;
- }
- for (int i = 1; i < c; i++) {
- A[i][i - 1] = 1;
- }
- for (int i = 0; i < c; i++) {
- A[0][i] = 1;
- }
- while (n) {
- if (n & 1) {
- Set(Buffer1, ANS, c);
- Mult(ANS, Buffer1, A, c);
- }
- n /= 2;
- Set(Buffer1, A, c);
- Set(Buffer2, A, c);
- Mult(A, Buffer1, Buffer2, c);
- }
- ll ans = 0;
- for (int i = 0; i < c; i++) {
- ans += ANS[i][0];
- ans %= MOD;
- }
- cout << ans << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement