HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation 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? Every input line contains one natural number n (0 < n <= 1000). For each input n print the number of consequitive zeroes pairs that will appear in the sequence after n steps. 2 1 这是我第一次用java进行大数运算 递推很简单,00只可能是上一个的01产生,上一个的01只可能是上上一个的00 1产生 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]);
}
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |