Computer Transformation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3845 Accepted Submission(s): 1420
Problem DescriptionA 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?
InputEvery input line contains one natural number n (0 < n ≤1000).
OutputFor each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
Sample Input2 3
Sample Output1 1
SourceSoutheastern Europe 2005
RecommendJGShining 先找到規律,推公式。1->01 , 0->10 而且很容易知道連續的0肯定是兩個連續的0. 設f[n]為n步操作後連續0的個數 則連續的0怎麼樣來呢?只能由上一層的01變成,也就是上一層的01一定可以產生連續00上一層的01可以由再上一層的1得到。或者由上一層的00也可以產生一個01.所以遞推公式產生了:f[n]=f[n-2]+2^(n-3). 由這個遞推公式很容易產生通項公式:當n為偶數時,f[n]=(2^(n-1)+1)/3;當n為奇數時,f[n]=(2^(n-1)-1)/3; 所有用大數公式就得出來了。。 用JAVA寫大數寫出來的。。
/* 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的高精度。。竟然逾時了。。。。
效率不高