標籤: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(規律題 && 大數題)