Computer TransformationTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9993 Accepted Submission(s): 3752 Problem Description A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.
Input Every input line contains one natural number n (0 < n ≤1000).
Output For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
Sample Input 2 3
Sample Output 1 1
Source
Recommend JGShining | We have carefully selected several similar problems for you: 1038 1006 1030 1032 1007 题目:初始给你一个1,然后每一次1变成01,0变成10求变化n步后,有多少个00。 分析:数学题。我们观察变化。 00 -> 1010 出现 10、01 01 -> 1001 出现 10、00、01 10 -> 0110 出现 01、11、10 11 -> 0101 出现 01、10 只有01下一步会生成00,但是00、01、10、11都会生成01,每一个1都会生成01,而00也可以生成01, 由此分成两种情况计算;设O(n)是变化n步后1的个数,Z(n)是变化n步后生成的00的个数,有结论: Z(n)= Z(n-2)+ O(n-2),O(n)= 2^(n-1){无论0、1都会生成0与1,所以是位数的一半} 说明:数据较大,用数组模拟大整数运算。
|
#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;
}