Recursive ideas:
Hand calculation derivation arr[1]=3, arr[2]=6, arr[3]=6, arr[4]=18;
Deduce arr[n]= at this time? (n>3) Taking into account that the value of n-1 is ① and the first is the same ② is different from the first place;
It is easy to understand that the first bit of n-1 is different (such a method has arr[n-1]) then the nth bit only one value is the first and the n-1 bit, then the total value is equal to arr[n-1];
However, another value of the n-1 bit and the first bit of the same so that the n-2 bit is bound to be different from the 1th (such a method of arr[n-2]) the value of the nth bit of the second is the first, the total value is equal to 2*arr[n-2];
End: Arr[i]=arr[i-1]+2*arr[i-2];(n>3);
#include <iostream> #include <string>using namespace Std;int main () { long long arr[51]={0, 3, 6, 6}; for (int i=4; i<51; i++) arr[i]=arr[i-1]+2*arr[i-2]; int N; while (cin>>n) Cout<<arr[n]<<endl;return 0;}
Hdu-2045-not Easy series of (3)--lele RPG puzzles