(1) Ladder
Given two equal-length arrays A and B, A[i] and b[i] indicate a ladder with a a[i] level, each time a Class 1 or two, up to the highest number of methods to 2^b[i] take the remainder of the results.
Data range: Array length L [1..30000], number range in a [1..L], number range in B [1..30]
Requires that the complexity of the time space is O (L)
Analysis: Play Table Method--We loop can put 0. L The results are calculated f[i] = f[i-1] + f[i-2], f[0] = f[1] = 1. The key point is to take the remainder, the remainder is very special, to 2^b[i] to take the remainder, the equivalent of the result of the low b[i] bit. So we can take all the results of the low 30 bits, a good table, the output and then really take b[i] bit, you can achieve O (L) complexity.
You can also with includes, for example://#include <algorithm>const int M = (1 <<)-1;vector<int> Solution (vector<int> &a, vector<int> &b) { //write your code in c++98 int m = 0; for (int i = 0; i < a.size (); ++i) { m = max (M, A[i]); } Vector<int> F; F.resize (M + 1); F[0] = f[1] = 1; for (int i = 2; I <= m; ++i) { F[i] = (F[i-1] + f[i-2]) & M; } Vector<int> answer; for (int i = 0; i < a.size (); ++i) { answer.push_back (F[a[i]] & ((1 << b[i])-1); } return answer;}
(2) Fibfrog
the definition of Fibonacci is given first f[0] = 0, f[1] = 1, f (M) = f (M-1) + f (M-2) if M >= 2. A frog wants to jump from the bank (-1) to the opposite shore (N). There are 0 in the middle. N-1 These n positions, there is an array of a[] that indicates whether there are lotus leaves in these locations, 0 means no, and 1 indicates there are. Frogs from the shore to jump to the lotus leaf, through 0 or more lotus leaf jump to the other side, and only to the opposite side of the direction of jumping-can't jump back. And the frog each jump distance must be a Fibonacci number, ask the frog at least a few steps to jump to the opposite shore? No solution returns-1.
Data range: N [0..30000]
Complexity required: Time O (Nlogn), Space O (N).
Analysis: First we can add a 1 to the end of the array, indicating the opposite shore. It can be thought that the first end has a 1 or that the array subscript starting from 1, we now target to the last element of the array, only through 1 and jump distance is the minimum number of steps Fibonacci. This is an obvious DP (or BFS). For position I and a[i] = = 1, we have dp[i] = min (Dp[i-f[j]]) + 1 where f[j] is Fibonacci number and satisfies a[i-f[j]]==1
Because Fibonacci is exponential, the Fibonacci number of not more than N is O (Logn), and the time complexity of the entire recursion is O (Nlogn).
You can also with includes, for example://#include <algorithm>int solution (vector<int> &a) { //WRI Te your code in c++98 a.push_back (1); int n = a.size (); Vector<int> F; F.push_back (1); F.push_back (1); for (int i = 1; F[i] < n; ++i) { f.push_back (F[i] + f[i-1]); } Vector<int> answer; Answer.resize (n + 1,-1); Answer[0] = 0; for (int i = 1; I <= n; ++i) { Answer[i] =-1; if (a[i-1] = = 0) { continue; } for (int j = 0; (J < F.size ()) && (i >= f[j]); ++J) { if (Answer[i-f[j] >= 0) && ((Answer[i] < 0) | | (Answer[i] > Answer[i-f[j]] + 1)) { Answer[i] = answer[i-f[j] [+ 1; } }} return answer[n];}
Exercises on the codility (one)