Question: first, give you a 1, and then change 1 to 0, 0 to 10. After changing n steps, how many 00 are there.
Analysis: mathematical problems. We observe the changes.
00-> 1010 appears 10, 01
01-> 1001: 10, 00, and 01
10-> 0110 appears 01, 11, 10
11-> 0101 appears 01, 10
Only 01 will generate 00 next, but 00, 01, 10, and 11 will generate 01. 01 will be generated for every 1, and 01 can be generated for 00,
The calculation is divided into two cases. If O (n) is the number of 1 after N steps, and Z (n) is the number of 00 after N steps are changed, the conclusion is as follows:
Z (n) = z (n-2) + O (n-2), O (n) = 2 ^ (n-1) {0, 1 will generate 0 and 1, so it's half the number of digits}
Note: The data size is large. Use arrays to simulate large integer operations.
#include <iostream>#include <cstdlib> #include <cstring>#include <cstdio> using namespace std; int O[1010][100];int Z[1010][100]; int main(){memset( O, 0, sizeof(O) );memset( Z, 0, sizeof(Z) );O[0][0] = O[1][0] = 1; for ( int i = 2 ; i < 1001 ; ++ i ) for ( int k = 0 ; k < 100 ; ++ k ) {O[i][k] += O[i-1][k] + O[i-1][k];Z[i][k] += O[i-2][k] + Z[i-2][k];O[i][k+1] += O[i][k]/10000; O[i][k] %= 10000;Z[i][k+1] += Z[i][k]/10000; Z[i][k] %= 10000;} int n; while( ~scanf("%d",&n) ) { int end = 99;while ( end > 0 && !Z[n][end] ) -- end;printf("%d",Z[n][end --]);while ( end >= 0 )printf("%04d",Z[n][end --]);printf("\n"); } return 0; }