Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7210 Accepted Submission(s): 2628
How many pairs of consequitive zeroes will appear in the sequence after n steps?
而要出现1001,则父串中要出现01
父串中出现01串有两种方法,一种是我所标注的红色部分,由祖父串中的1变过来
第二种方式就是当祖父串中有00时 父串变成 1010 时会出现 01
所以这样就可以得到递推公式:第n项00的个数 = 第 n-2项 1的个数+第n-2项 00 的个数。
f[1] = 0
f[2] = 1
f[n] = f[n-2]+2^(n-3) ( n>2 )
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger [] h = new BigInteger[1001]; h[1] = new BigInteger("0"); h[2] = new BigInteger("1"); for(int i=3;i<=1000;i++){ h[i] = h[i-2].add(pow(i-3)); } Scanner sc =new Scanner (System.in); while(sc.hasNext()){ int n =sc.nextInt(); System.out.println(h[n]); } } private static BigInteger pow(int v) { BigInteger sum = BigInteger.valueOf(1); for(int i=0;i<v;i++){ sum = sum.multiply(BigInteger.valueOf(2)); } //System.out.println(sum); return sum; } }