HDU-1041-Computer Transformation(規律題 && 大數題)

來源:互聯網
上載者:User

標籤:acm   java   每日一水   不知道說什麼   orz   

Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6543    Accepted Submission(s): 2378


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 Input
23
 
Sample Output
11
 
SourceSoutheastern Europe 2005 
RecommendJGShining   |   We have carefully selected several similar problems for you:  1006 1030 1032 1007 1143 

首先解釋一下題目:題目的大概意思很簡單,就是說電腦裡面存了一個初始的數字1,後面的所有規律和操作都從1開始,然後每一次產生一個變換,變換的法則如下即 在這個數列中所有的1變換成01,所有的0變換成10,這樣說可能還不明確,從1 開始,下一次則得到01(1->01),再下次則得到1001(上一次的0->10,1->01)然後得到01101001,然後依次類推.......問你經過n次變換有多少組00!(consequitive意為連續,相同的意思!)

參考網友:星夜&永恒的推理:
00隻能由01推到,即01->1001->00
01隻能由1,00推到,即1->01,00->1010->01.
現設a[n]表示n秒時1的個數,
b[n]表示n秒時00的個數,
c[n]表示n秒時01的個數,
由題知0,1過一秒都會產生一個1, 
所以a[n+1]=2^n.....0秒1個數,1秒2*1個數,2秒2*1*2個數,...n秒2*1*2*2*2..*2個數=2^n.
b[n+1]=c[n];
c[n+1]=a[n]+b[n],
所以b[n]=c[n-1]=a[n-2]+b[n-2]=2^(n-3)+b[n-2].
其實本題多寫幾個範例就能發現另一個規律b[n]=2*b[n-2]+b[n-1]. 
注意本題大數相加.


/* * 依據遞推式:b[n]=2*b[n-2]+b[n-1] */import java.io.*;import java.math.BigInteger;import java.util.*;public class Main{public static void main(String[] args){// TODO Auto-generated method stubScanner input = new Scanner(System.in);BigInteger a[] = new BigInteger[1001];BigInteger b[] = new BigInteger[1001];a[0] = BigInteger.ONE;b[2] = BigInteger.ONE;b[0] = BigInteger.ZERO;b[1] = BigInteger.ZERO;for (int i = 1; i < 1001; i++){a[i] = a[i - 1].multiply(BigInteger.valueOf(2));   //這裡算a[i]的但是這裡的a[i]=2^(i-2)if (i >= 3)  //所以下面算b[i]時候要i要多減去一個1{b[i] = a[i - 3].add(b[i - 2]);             }}while (input.hasNext()){int n = input.nextInt();System.out.println(b[n]);}}}



 

HDU-1041-Computer Transformation(規律題 && 大數題)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.