题意是将 1 展开成 01 ,将 0 展开成 10 ,问这样展开 n 次后序列中有多少对 0。
手写发现:0,1,1,3,5,11 ... 即 a[ i ] = a[ i -1 ] + a[ i - 2 ] * 2;
由题意知 n 能到 1000,大于 2^1000 ,用 string 存。模拟加法,本题可做模板。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 string f[1005]; 4 int n,len1,len2; 5 string add(string s1,string s2) 6 { 7 len1 = s1.length(); 8 len2 = s2.length(); 9 if(len1<len2) 10 { 11 string temp = s1; 12 s1 = s2; 13 s2 = temp; 14 } 15 for(int i = len1-1, j = len2-1; i >= 0; --i, --j) 16 { 17 s1[i] = char( s1[i]+( j>=0?(s2[j]-'0'):0) ); 18 if(s1[i]-'0' >= 10) 19 { 20 s1[i] = char( (s1[i]-'0')%10+'0' ); 21 if(i) ++s1[i-1]; 22 else s1 = "1"+s1; 23 } 24 } 25 return s1; 26 } 27 int main() 28 { 29 std::ios::sync_with_stdio(false); 30 f[1]="0"; f[2]="1"; f[3]="1"; f[4]="3"; f[5]="5"; f[6]="11"; 31 for(int i = 7; i <= 1004; ++i) 32 f[i] = add( f[i-1], add(f[i-2],f[i-2]) ); 33 while(cin >> n) 34 cout << f[n] << endl; 35 return 0; 36 }