P2106: Sam Number — Solution
This problem is more or less a template problem for matrix recurrences. If you're not very familiar with matrix multiplication or with how matrix-accelerated recurrences work, you can head over to this Wiki page: Matrices - OI Wiki. We can quickly come up with a DP equation, where \(i\) is the number of digits and \(j\) is the digit filled in the first position:
\[dp[i][j] = \sum^{j+2}_{k=j-2}dp[i-1][k]\]
Then, from this we can derive a recurrence matrix:
\[\left[ \begin{array}{ccc} 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1\\ \end{array} \right]\]
The initial matrix looks like this:
\[\left[ \begin{array}{ccc} 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\\ \end{array} \right]\]
The code is as follows:
// P2106.cpp
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define ll long long
using namespace std;
const ll mod = 1000000007;
struct matrix
{
ll mat[10][10];
matrix() { memset(mat, 0, sizeof(mat)); }
matrix operator*(const matrix &ma) const
{
matrix empt;
for (ll i = 0; i < 10; i++)
for (ll j = 0; j < 10; j++)
for (ll k = 0; k < 10; k++)
empt.mat[i][j] = (empt.mat[i][j] + mat[i][k] * ma.mat[k][j] % mod) % mod;
return empt;
}
matrix operator^(const ll &num) const
{
ll tim = num;
matrix pre = *(this);
matrix tmp;
for (ll i = 0; i < 10; i++)
tmp.mat[i][i] = 1;
while (tim)
{
if (tim & 1)
tmp = tmp * pre;
pre = pre * pre;
tim >>= 1;
}
return tmp;
}
} premat, Dmat;
int main()
{
ll k;
scanf("%lld", &k);
if (k == 1)
{
printf("%lld", 10);
return 0;
}
for (ll i = 1; i < 10; i++)
premat.mat[0][i] = 1;
for (ll i = 0; i < 10; i++)
for (ll j = i - 2; j <= i + 2; j++)
{
if (j < 0)
continue;
if (j > 9)
break;
Dmat.mat[j][i] = 1;
}
Dmat = Dmat ^ (k - 1);
premat = premat * Dmat;
ll ans = 0;
for (ll i = 0; i < 10; i++)
ans += premat.mat[0][i], ans %= mod;
printf("%lld", ans);
return 0;
}