題意:
(from luckycat)
解法:
直接講的話,他的遞迴式就是跟費波那契數列的關係類似:
f[ n ] =解釋一下的話,當 n > 2 時,可以把當前情況 f[ n ] 分成兩個部分,
{ 1, if n = 0
{ 2, if n = 1
{ f[ n-1 ] + f[ n-2 ], if n > 1
就是 由中間那條線反射回來的光線數 與 由另一邊的線反射回來的光線數,
而 前者 其實就是 f[ n-1 ] 的情況加上一次的反射,
而 後者 是 f[ n-2 ] 的情況加上兩次的反射且經由中間那條線。
TAG: BigNum, Math, DP
注意: 要用大數
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tittle: 10334 - Ray Through Glasses | |
* Author: Cheng-Shih, Wong | |
* Date: 2015/06/29 | |
*/ | |
import java.util.*; | |
import java.math.*; | |
public class Main { | |
private static int N = 1005; | |
private static BigInteger[] ray = new BigInteger[N]; | |
public static void main( String[] args ) { | |
int n; | |
Scanner input = new Scanner(System.in); | |
init(); | |
while( input.hasNext() ) { | |
n = input.nextInt(); | |
System.out.println(ray[n].toString()); | |
} | |
} | |
private static void init() { | |
ray[0] = BigInteger.ONE; | |
ray[1] = BigInteger.valueOf(2); | |
for( int i=2; i<=1000; ++i ) | |
ray[i] = ray[i-1].add(ray[i-2]); | |
} | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽