Description
Sample Input
2 10
Sample Output
90
The Solution
迟来的博客。。。
Algorithm : DP+高精度
我们可以设:f[i][j]表示到第 i 位,这一位为 j 的方案数。
很显然可以推出转移方程:
f[i][j]=f[i][j]+f[i−1][l](j!=0andl!=0)
最后答案为
∑i=1l(f[n][i])
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,b,a) for(int i=b;i>=a;i--)
#define N 2000
#define ya 100000
using namespace std;
int n,m;
struct note
{
int x[N];
}f,g,t;
note jia(note a,note b)
{
note c;
memset(c.x,0,sizeof(c.x));
c.x[0]=max(a.x[0],b.x[0]);
fo(i,1,c.x[0])
{
c.x[i]+=a.x[i]+b.x[i];
c.x[i+1]+=c.x[i]/ya;
c.x[i]%=ya;
}
if (c.x[c.x[0]+1]) c.x[0]++;
return c;
}
note cheng(note a,int b)
{
note c;
memset(c.x,0,sizeof(c.x));
c.x[0]=a.x[0];
fo(i,1,c.x[0])
{
c.x[i]+=a.x[i]*b;
c.x[i+1]+=c.x[i]/ya;
c.x[i]%=ya;
}
if (c.x[c.x[0]+1]) c.x[0]++;
return c;
}
int main()
{
scanf("%d%d",&n,&m);
int q=m-1;
while (q)
{
f.x[++f.x[0]]=q%10;
q/=10;
}
fo(i,2,n)
{
t=f;
f=jia(f,g);
f=cheng(f,m-1);
g=t;
}
g=jia(g,f);
printf("%d",g.x[g.x[0]]);
fd(i,g.x[0]-1,1) printf("%05d",g.x[i]);
return 0;
}