1. 程式人生 > >(大數)Computer Transformation hdu1041

(大數)Computer Transformation hdu1041

sca forms mat quit util ati ima memory bsp

Computer Transformation

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8688 Accepted Submission(s): 3282

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.

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

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

java,遞推

遞推:0->10 ;

1->01;

00->1010;

10->0110;

01->1001;

11->0101;

假設a[i]表示第i 步時候的00的個數,由上面的可以看到,00是由01 得到的,所以只要知道a[i-1]的01的個數就能夠知道a[i]的00的個數了,那a[i-1]怎麽求呢,同樣看推導,01由1和00 得到,而第i步1的個數是2^(i-1),所以a[i]=2^(i-3)+a[i-2];(最後計算的是第i-2步情況)。

技術分享圖片
import java.math.BigDecimal;
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); BigInteger a[]=new BigInteger[1001]; while(in.hasNextInt()) { int n=in.nextInt(); a[1]=BigInteger.valueOf(0); a[2]=BigInteger.valueOf(1); a[3]=BigInteger.valueOf(1); for(int i=4;i<=n;i++) { a[i]=BigInteger.valueOf(0); //先進行初始化。 int m=i-3; //在大數的pow(m,n)中,n是int類型的,m是BigInteger類型的。 BigInteger q= new BigInteger("2"); a[i]=a[i].add(q.pow(m)); a[i]=a[i].add(a[i-2]); } System.out.println(a[n]); } } }
View Code

(大數)Computer Transformation hdu1041