本帖最后由 guosl 于 2022-11-22 08:47 编辑
应用状态压缩的递推
答案:126461847755#include <iostream>
using namespace std;
const int m = (1 << 10);
long long f[41][10][m];//f[k][i][j]表示k位数且最高位是i同时j中那些非零的位上数字已经出现过的总步差数个数
int main(void)
{
for (int i = 0; i <= 9; ++i)//赋初值
f[1][i][1 << i] = 1;
for (int k = 2; k <= 40; ++k)//按位向大数递推
{
for (int i = 0; i <= 9; ++i)//枚举第i位的数字
{
int p = 1 << i;
for (int j = 1; j < m; ++j)//枚举已经用过的数字
{
if (i >= 1)
f[k][i][j | p] += f[k - 1][i - 1][j];
if (i <= 8)
f[k][i][j | p] += f[k - 1][i + 1][j];
}
}
}
long long nS = 0;
for (int k = 10; k <= 40; ++k)
{
for (int i = 1; i <= 9; ++i)
nS += f[k][i][m - 1];
}
cout << nS << endl;
return 0;
}
|