dp,同1012,只是数据从180变为了1800,采用滚动数组过去,自己测了下极限数据感觉有点慢,1.3s水过,代码也巨丑,直接从1012改过来的。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int k,n;
char dp[4][20][1800];
char sum[1800];
void plu(int x0,int y0,int x1,int y1)
{
for(int i=0;i<1800;i++)
{
dp[x0&1][y0][i]+=dp[x1&1][y1][i];
if(dp[x0&1][y0][i]>=10)
{
dp[x0&1][y0][i]%=10;
dp[x0&1][y0][i+1]++;
}
}
}
int main()
{
scanf("%d%d",&n,&k);
memset(dp,0,sizeof(dp));
for(int i=1;i<k;i++)
dp[n&1][i][0]=1;
for(int j=n-1;j>0;j--)
{
for(int i=0;i<k;i++)
for(int l=0;l<1800;l++)
dp[j&1][i][l]=0;
for(int i=0;i<k;i++)
{
for(int l=1;l<k;l++)
{
plu(j,i,j+1,l);
}
if(i!=0)plu(j,i,j+1,0);
}
}
memset(sum,0,sizeof(sum));
for(int i=0;i<k;i++)
{
for(int j=0;j<1800;j++)
{
sum[j]+=dp[1][i][j];
if(sum[j]>=10)
{
sum[j]%=10;
sum[j+1]++;
}
}
}
int top=1790;
while(sum[top]==0&&top>=0)top--;
while(top+1)
printf("%d",sum[top--]);
printf("\n");
return 0;
}