我的#11 TLE,请求帮助,谢谢
#include <cstdio>
typedef long long ll;
const int M = 110;
const ll mod = 1e9 + 7;
int m;
ll n;
struct Matrix
{
ll ma[M][M];
Matrix() : ma() { }
}
SET, BASE;
void setMatrix()
{
for (int i = 0; i < m; ++i)
{
SET.ma[i][i] = 1;
}
}
void baseMatrix()
{
BASE.ma[0][0] = BASE.ma[0][m - 1] = 1;
for (int i = 1; i < m; ++i)
{
BASE.ma[i][i - 1] = 1;
}
}
Matrix operator*(const Matrix& m1, const Matrix& m2)
{
Matrix ans;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < m; ++j)
{
for (int k = 0; k < m; ++k)
{
(ans.ma[i][j] += (m1.ma[i][k] * m2.ma[k][j]) % mod) %= mod;
}
}
}
return ans;
}
Matrix fastPow(Matrix matrix, ll n)
{
Matrix ans(SET);
while (n)
{
if (n & 1)
{
ans = ans * matrix;
}
matrix = matrix * matrix;
n >>= 1;
}
return ans;
}
int main()
{
scanf("%lld%d", &n, &m);
setMatrix();
baseMatrix();
Matrix matrix = fastPow(BASE, n - (ll)(m) + 1);
ll ans = 0;
for (int i = 0; i < m; ++i)
{
(ans += matrix.ma[0][i]) %= mod;
}
printf("%lld", ans);
}
我的状态转移方程:
其中 i 表示凑 i 个体积单位的方法数
dpi={1dpi−1+dpi−mi<mi≥m
我目前调不出来,向各位求助,谢谢