蝴蝶效應_JAVA

蝴蝶效應

Time Limit: 1000MS Memory Limit: 65536KB 

Problem Description

蝴蝶效應是氣象學家洛倫茲1963年提出來的。其大意爲:一隻南美洲亞馬孫河流域熱帶雨林中的蝴蝶,偶爾扇動幾下翅膀,可能在兩週後引起美國德克薩斯引起一場龍捲風。其原因在於:蝴蝶翅膀的運動,導致其身邊的空氣系統發生變化,並引起微弱氣流的產生,而微弱氣流的產生又會引起它四周空氣或其他系統產生相應的變化,由此引起連鎖反應,最終導致其他系統的極大變化。此效應說明,事物發展的結果,對初始條件具有極爲敏感的依賴性,初始條件的極小偏差,將會引起結果的極大差異。

我們將問題簡化爲方程 f(x) = (a*f(max(0,x-b)) + c*f(max(0,x-d)))%1000000007

現在給出不同的f(0)n以及參數a,b,c,d,計算出f(n)

Input

多組輸入。

對於每組數據,有六個個整數n,f0(1 <= n <= 100001 <= f0 <= 10000)a,b,c,d(1 <= a,b,c,d <= 10000)

Output

對於每組數據輸出f(n)

Example Input

1 2 3 4 5 6

Example Output

16

import java.util.*;  

public class Main {  
  
    static long[] f = new long[10010];//注意爲什麼要定義在這兒
    final static int zero = 0;  //注意細節
    final static long mod = 1000000007;  //注意細節
  
    public static void main(String[] args) {  
        Scanner cin = new Scanner(System.in);  
        int n, i, a, b, c, d;  
        while (cin.hasNext()) {  
            n = cin.nextInt();  
            f[0] = cin.nextInt();  
            a = cin.nextInt();  
            b = cin.nextInt();  
            c = cin.nextInt();  
            d = cin.nextInt();  
            for (i = 1; i <= n; i++) {  //用遞推的方法,用遞歸會超時,也可用內部類
                f[i] = (a * f[Math.max(0, i - b)] + c * f[Math.max(0, i - d)]) % mod;//注意使用java自帶方法  
            }  
            System.out.println(f[n]);  
        }  
    }  
}  


發佈了54 篇原創文章 · 獲贊 20 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章