The following is the log replay for the sophomore time:
"The topic extends to the K -order,
kThe order Fibonacci sequence, 1 order (i.e.k=1):1,1,1,1,1,1,1、......
a0=a[1-1]=1,a1=1,a2=1,a3=1,a4=1,a5=1,a6=1 ...
3Order (k=3):0,0,1,1,2,4,7、、、、、
A0=0,a1=0,a2=a[3-1]=1,a3=0+0+1=1,a4=0+1+1=2,a5=1+2+4=7
4Order:0,0,0,1,1,2,4,8, the, -...
a0=0,a1=0,a2=0,a3=a[4-1]=1,a4=1,a5=2,a6=4 ... a[8]=1+2+4+8=15 ...
The problem is generalized and it can be seen that:
k-2 0 ;
Section k-1 the value of the item is 1 ;
Sectionkto the2k-1the value of the item is2of theNThe Second party (also the formerkitems and But at the same time with2of theNThe same as the second-party value);
Section 2k The value after the item is the previous k items and;
Web-based method of ' recursion ', and only correspondence 2 step, not extended to K step, the younger brother also skilled ' recursion ', or continue to use the order method.
c
Status fibonacci (int k, int m, int &f)/* the value of item m of the K-order Fibonacci sequence f */{ int a[100],i,y,z=0; /* defines an array with an element number of 100. */ if (k<=1| | M<0) return error; /* returns an error warning */ else if (m<k-1) If the input parameter is not canonical f=0; /* constant equals data segment processing */ else if (m==k-1) f=1; else if (m<=2*k-1) F=pow ( 2,M-K); /* two-segment processing */ else /*k stage processing, time complexity */ { for (y=0;y<k;y++) /* assigns a value */ a[y]=pow (2,y) to the K array element with a for loop; for (i=2*k;i<=m;i++) { &NBSP;A[K]=0;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;/*A[K] */ for (y=0;y<k;y++) /* embedded in the For loop body inside the For loop body */ { a[k] Each time the loop is cleared 01 times =a[k]+a[y]; /* K-Item plus */ } a[z]=a[k]; /* Each move to the value of the smallest element of the eradication, change to the maximum value, do not have to assign a value, save a large amount of time, again added "output A[K] value" can achieve the first m-k and output */ z++; /*z each time you add 1, make sure to overwrite the smallest item with the largest item of the top K item */ if (z==k) z=0; /* when z==k, Z zeroing */ } f=a[k]; /* output: The value of item m of the K-order Fibonacci sequence number F */ } return ok;}
The following is an optimized algorithm that has a faster processing time in the Obline judge.
Status fibonacci (int k, int m, int &f)/* the value of item m of the K-order Fibonacci sequence f */{ long a[300], i, y, z=0; /* defines an array with an element number of 300, The limit in long is less than the No. 300 number. */ if (k <= 1| | M&NBSP;<&NBSP;0) return error; /* returns an error warning */&NBSP;ELSE&NBSP;IF if the input parameter is not canonical (m <&NBSP;K-1) f=0; /* constant equals data segment processing */ else if (m == k-1) f=1; Else for (i=0; i<k; i++) a[i]=0; a[k]=1; /* input k 0 and A[k]=1 initial digital */ for (y=k+1; y<=m; y++) a[y]=a[y-1]*2-a[y-k]; /* Law: The number of y is equal to y-1, multiplied by 2 minus the y-k number */ f=a[y-1]; /* output: The value of item m of the K-order Fibonacci sequence number f */ } return ok;}
This article is from the "zgysolution" blog, make sure to keep this source http://zgysolution.blog.51cto.com/9605745/1582596
C-Language algorithm: Perfecting the K-order Fibonacci sequence of the year