HDU 1041 computer Transformation (High Precision)

Source: Internet
Author: User
Computer Transformation

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3845 accepted submission (s): 1420

Problem descriptiona 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 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.

How many pairs of consequitive zeroes will appear in the sequence after N steps?

 

Inputevery input line contains one natural number N (0 <n = 1000 ).

 

Outputfor each input n print the number of consecutive zeroes pairs that will appear in the sequence after N steps.

 

Sample input2 3

 

Sample output1 1 1

 

Sourcesoutheastern Europe 2005

 

Recommendjgshining first finds the rule and pushes the formula. 1-> 01, 0-> 10 and it is easy to know that the continuous 0 must be two consecutive 0. how does one set f [N] to the number of consecutive 0 after N steps? It can only be changed from the 01 of the previous layer, that is, the 01 of the previous layer can certainly generate the 01 of the previous layer for consecutive 00, which can be obtained from the 1 of the previous layer. Or a 01 can be generated by 00 on the previous layer. so the recursive formula produces: F [N] = f [N-2] + 2 ^ (n-3 ). this recursive formula can easily generate a general formula: WHEN n is an even number, F [N] = (2 ^ (n-1) + 1)/3; when n is an odd number, f [N] = (2 ^ (n-1)-1)/3; all are obtained using the formula of large numbers .. Write large numbers in Java ..
 /* F [N] = f [N-2] + 2 ^ (n-3); when n is an odd number, F [N] = (2 ^ (n-1)-1)/3; if n is an even number, F [N] = (2 ^ (n-1) + 1)/3;  */  Import java. util. * ; Import java. Math. * ; Import java. Io. * ;  Public   Class  Main {  Public   Static   Void  Main (string [] ARGs) {Cin cin = New Partition ( New Bufferedinputstream (system. In  ); Biginteger A [] = New Biginteger [ 1000  ]; [  0 ] = Biginteger. valueof ( 1  );  For ( Int I = 1 ; I < 1000 ; I ++) A [I] = A [I- 1 ]. Multiply (biginteger. valueof ( 2  ));  Int  N; biginteger ans;  While  (CIN. hasnextint () {n = Cin. nextint ();  If (N % 2 = 0 ) //  Even {Ans = A [n- 1 ]. Add (biginteger. valueof ( 1  ); Ans = Ans. Divide (biginteger. valueof ( 3  ));}  Else  {Ans = A [n- 1 ]. Subtract (biginteger. valueof ( 1  ); Ans = Ans. Divide (biginteger. valueof ( 3 );} System.  Out  . Println (ANS );}}} 

 

 

 

Import java. util .* ; Import java. Math. * ; Import java. Io. * ;  Public   Class  Main {  Public   Static   Void  Main (string [] ARGs ){ Int  N; CIN = New Partition ( New Bufferedinputstream (system. In  ); Biginteger = Biginteger. valueof ( 2  ); Biginteger ans;  While  (CIN. hasnextint () {n = Cin. nextint ();  If (N % 2 =1  ) Ans = A. Pow (n- 1 ). Subtract (biginteger. valueof ( 1 ). Divide (biginteger. valueof ( 3  ));  Else  Ans = A. Pow (n- 1 ). Add (biginteger. valueof ( 1 ). Divide (biginteger. valueof ( 3  ); System.  Out . Println (ANS );}}} 

 

 

I 've worked so hard on C ++ to write a string of high precision .. It timed out ....

 

Inefficient

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.