HDU 1041 Computer Transformation(高精度)

來源:互聯網
上載者:User
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的高精度。。竟然逾時了。。。。

 

效率不高

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.