|
|
back to boardWhere is my mistakes? why wa#1 ??? #include <cstdlib> #include <iostream> using namespace std; int a[2000],b[2000]; int c[2000][2000]; int en; int sum(int k,int p) { int top[4000]; int i,j=0; int y=0,t;
if (k > p) { for (i=k;i>=k-p;i--) b[i]=b[i-(k-p)]; for (i=0;i<k-p;i++) b[i]=0; en=k; } else if (p > k) { for (i=p;i>=p-k;i--) a[i]=a[i-(p-k)]; for (i=0;i<p-k;i++) a[i]=0; en=p; } else if (k == p) en=k;
for (i=en-1;i>=0;i--) { t=a[i]+b[i]+y; if (t > 9) { t-=10; y=1; top[j]=t; j++; } else {top[j]=t; j++; y=0;} } if (y == 1) {top[j]=y; j++;} int l=0; for (i=j-1;i>=0;i--) { a[l]=top[i]; l++; } return l; } int multi(int k,int p,int n) { int tp[5000]; int cv[4000]; int y=0,v=0,vy,uz; int i,l=0,j,sy=0;
for (i=0;i<=4000;i++) tp[i]=0; for (i=k-1;i>=0;i--) { v=(a[i]*p)+vy; if (v > 9) { vy=v / 10; v-=(vy*10); } else vy=0; cv[l]=v; l++; if (vy > 0) {cv[l]=vy; l++;} l+=sy; for (j=l-1;j>=sy;j--) cv[j]=cv[j-sy]; for (j=0;j<sy;j++) cv[j]=0; for (j=0;j<l;j++) { uz=l-1; tp[j]+=cv[j]+y; if (tp[j] > 9) { tp[j]-=10; y=1; } else y=0; } for (j=0;j<l;j++) cv[j]=0; if (y == 1) { uz++; tp[uz]=y; } l=0; vy=0; sy++; y=0; } l=0; while (tp[uz] <= 0) uz--; for (j=uz;j>=0;j--) { c[n][l]=tp[j]; l++; } return l; } int main() { int n,k,i,j,u; int uz[2000];
cin>>n>>k; c[0][0]=1; c[1][0]=k-1; uz[0]=1; uz[1]=1; for (i=2;i<=n;i++) { for (j=0;j<uz[i-1];j++) a[j]=c[i-1][j]; for (j=0;j<uz[i-2];j++) b[j]=c[i-2][j]; u=sum(uz[i-1],uz[i-2]); uz[i]=multi(u,k-1,i); }
for (i=0;i<uz[n];i++) cout<<c[n][i]; cout<<endl;
return EXIT_SUCCESS; } |
|
|