Problem Link: HDU5620
Just read the question, a little difficult to solve, no clue.
Look at the hint to understand the point, a bit like the Fipolaci sequence, but each request is a sequence to the sum of the item. Also slightly different is that the 1th item is 1, and the 2nd item is 2. Maybe it's the reason why three steel pipes can't be triangles.
Now that you know the above, it is time to make a list, which is to save the calculation times, although sometimes it is superfluous, but most of the programs need to make a table.
When looking for, you can find it in order, just a little bit of time. Here the binary search, logic is a little bit troublesome, because this is not to find equal number, is to find a less than or equal to the number, so pay attention to the two points after the adjustment. See also: HDU5620 KK ' s Steel (c + + language edition).
One point to note is that the value of the Fipolaci sequence grows very fast, and its growth is faster, with no 95 items to reach the desired value range. This item count, as the basis for defining the size of the array, can not come casually, need to do some homework beforehand.
The procedure is as follows:
#include <stdio.h> #define MAXN 95unsigned Long fsum[maxn+1];/* Recursive method: Calculates the sum of the 1th to N of the Fibonacci sequence *//* here is slightly different, the 2nd item is 2, the other is basically the same */void fibsum (unsigned long long fsum[], int n) {fsum[0] = 0; FSUM[1] = 1; FSUM[2] = 3; if (n <= 2) return; unsigned long long f1 = 1, F2 = 2, temp; int i; for (i=3; i<=n; i++) {temp = f1 + F2; F1 = F2; F2 = temp; Fsum[i] = fsum[i-1] + temp; }}int Main (void) {//Calculates the sum of the 1th to N of the Fibonacci sequence, playing table Fibsum (Fsum, MAXN); int T, start, Mid, end; unsigned long long n; scanf ("%d", &t); while (t--) {scanf ("%llu", &n); Two-point lookup start = 0; end = MAXN; for (;;) {if (Start > End) break; Mid = (start + end)/2; if (Fsum[mid] < n) Start = mid + 1; else if (Fsum[mid] > N) end = Mid-1; else if (fsum[mid] = = n) break; } if (n < Fsum[mid]) mid--; printf ("%llu\n", mid); } return 0;}
HDU5620 KK ' s Steel (C language Edition)