Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3845 Accepted Submission(s): 1420
How many pairs of consequitive zeroes will appear in the sequence after n steps?
/* f[n]=f[n-2]+2^(n-3); n为奇数时,f[n]=(2^(n-1)-1)/3; n为偶数时,f[n]=(2^(n-1)+1)/3; */ import java.util.*; import java.math.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(new BufferedInputStream(System.in)); BigInteger a[]=new BigInteger[1000]; a[0]=BigInteger.valueOf(1); for(int i=1;i<1000;i++) a[i]=a[i-1].multiply(BigInteger.valueOf(2)); int n; BigInteger ans; while(cin.hasNextInt()) { n=cin.nextInt(); if(n%2==0)//偶数 { ans=a[n-1].add(BigInteger.valueOf(1)); ans=ans.divide(BigInteger.valueOf(3)); } else { ans=a[n-1].subtract(BigInteger.valueOf(1)); ans=ans.divide(BigInteger.valueOf(3)); } System.out.println(ans); } } }
import java.util.*; import java.math.*; import java.io.*; public class Main { public static void main(String[] args) { int n; Scanner cin=new Scanner(new BufferedInputStream(System.in)); BigInteger a=BigInteger.valueOf(2); BigInteger ans; while(cin.hasNextInt()) { n=cin.nextInt(); if(n%2==1) ans=a.pow(n-1).subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)); else ans=a.pow(n-1).add(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)); System.out.println(ans); } } }
辛辛苦苦C++写了用份string的高精度。。竟然超时了。。。。
效率不高