Ultraviolet A 1647-computer Transformation

Source: Internet
Author: User

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;    }   

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.