zoukankan      html  css  js  c++  java
  • 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])
  • 相关阅读:
    计算长度的方法
    自动装箱和拆箱
    基本数据包装类
    Date类
    文档参数解析
    权限修饰符
    IO流
    ArrayList集合类
    对象数组
    二维数组
  • 原文地址:https://www.cnblogs.com/LinKArftc/p/4969430.html
Copyright © 2011-2022 走看看