HOJ 2148&POJ 2680(DP递推,加大数运算)

来源:互联网 发布:成都网络推广公司电话 编辑:程序博客网 时间:2024/05/21 12:44

Computer Transformation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4561 Accepted: 1738
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进行大数运算

递推很简单,00只可能是上一个的01产生,上一个的01只可能是上上一个的00 1产生
hoj的测试机好像有问题,poj里面ac的代码提交不上去hoj

import java.math.BigInteger;import java.util.Scanner;/** * * @author chenyongkang */public class Main {    public static BigInteger a;    public static BigInteger b[]=new BigInteger[1010];    public static void init()    {        BigInteger x=BigInteger.valueOf(0);        BigInteger y=BigInteger.valueOf(1);        for(int i=0;i<=1000;i++)            b[i]=BigInteger.valueOf(0);        b[1]=x;b[2]=y;        for(int i=3;i<=1000;i++)        {             a=BigInteger.valueOf(1);            for(int j=1;j<=i-3;j++)            {                a=a.multiply(BigInteger.valueOf(2));            }           b[i]=b[i].add(b[i-2]);           b[i]=b[i].add(a);        }    }    /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic here           init();        Scanner cin=new Scanner(System.in);        while(cin.hasNext())        {            int n=cin.nextInt();            System.out.println(b[n]);        }    }}
0 0
原创粉丝点击