【链接】
bzoj1089
【解题报告】
定义 fi 表示目前 i 层的方案数。
所以答案就是 fd−fd−1 。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=30,maxv=5005;
int n,m;
struct Bignum
{
int s[maxv];
Bignum operator + (const Bignum &a) const{
Bignum c;
memset(c.s,0,sizeof(c));
c.s[0]=max(s[0],a.s[0]);
for (int i=1; i<=c.s[0]; i++) c.s[i]+=s[i]+a.s[i],c.s[i+1]+=c.s[i]/10,c.s[i]%=10;
while (c.s[c.s[0]+1]) c.s[0]++;
return c;
}
Bignum operator - (const Bignum &a) const{
Bignum c;
memset(c.s,0,sizeof(c));
c.s[0]=s[0];
for (int i=1; i<=c.s[0]; i++)
{
c.s[i]+=s[i]-a.s[i];
if (c.s[i]<0) c.s[i]+=10,c.s[i+1]--;
}
while (c.s[0]>1&&!c.s[c.s[0]]) c.s[0]--;
return c;
}
Bignum operator * (const Bignum &a) const{
Bignum c;
memset(c.s,0,sizeof(c));
c.s[0]=s[0]+a.s[0];
for (int i=1; i<=s[0]; i++)
for (int j=1; j<=a.s[0]; j++) c.s[i+j-1]+=s[i]*a.s[j];
for (int i=1; i<=c.s[0]; i++) c.s[i+1]+=c.s[i]/10,c.s[i]%=10;
while (c.s[c.s[0]+1]) c.s[0]++;
while (c.s[0]>1&&!c.s[c.s[0]]) c.s[0]--;
return c;
}
Bignum operator ^ (int b) const{
Bignum c,a;
for (int i=0; i<=s[0]; i++) a.s[i]=s[i];
memset(c.s,0,sizeof(c.s));
c.s[0]=c.s[1]=1;
for (; b; b>>=1,a=a*a)
if (b&1) c=c*a;
return c;
}
void Write()
{
for (int i=s[0]; i; i--) putchar(s[i]+48);
}
}f[maxn],a1;
int main()
{
freopen("1089.in","r",stdin);
freopen("1089.out","w",stdout);
scanf("%d%d",&n,&m);
if(m==0) {printf("1"); return 0;}
a1.s[0]=f[0].s[0]=a1.s[1]=f[0].s[1]=1;
for (int i=1; i<=m; i++)
f[i]=(f[i-1]^n)+a1;
(f[m]-f[m-1]).Write();
return 0;
}