POJ2680(动态规划,大数)

Computer Transformation
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4548   Accepted: 1731

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 consequitive zeroes pairs that will appear in the sequence after n steps.

Sample Input

2
3

Sample Output

1
1

Source

 
思路详见:动态规划
java
import java.util.*;
import java.math.*;

public class Main {

    public static void main(String[] args) {
        final int maxn = 1010;
        BigInteger fa[] = new BigInteger[maxn];
        BigInteger fb[] = new BigInteger[maxn];
        BigInteger TWO = BigInteger.valueOf(2);
        fa[0] = fb[0] = BigInteger.ZERO;
        for (int i = 1; i < maxn; i ++) {
            fa[i] = fa[i-1].add(fb[i-1]);
            fb[i] = fa[i-1].add(fb[i-1]).add(BigInteger.valueOf(i).mod(TWO));
        }
        int n;
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
            n = cin.nextInt();
            System.out.println(fb[n-1]);
        }
    }
}

Python

import sys
fa = [0] * 1010
fb = [0] * 1010
fa[0] = 0;
fb[0] = 0;
for i in range(1, 1010):
    fa[i] = fa[i-1] + fb[i-1]
    fb[i] = fa[i-1] + fb[i-1] + i % 2

for line in sys.stdin:
    n = int(line)
    print(fb[n-1])
原文地址:https://www.cnblogs.com/LinKArftc/p/4969430.html