sdutoj 3062 蝴蝶效应 (记忆化搜索/递推)

来源:互联网 发布:查潮汐软件下载 编辑:程序博客网 时间:2024/05/21 17:13

蝴蝶效应

Time Limit: 1000MSMemory 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

题意:略

两种做法  第一种记忆化搜索  见下代码 简单粗暴

第二种 递推 利用数组记录数值 for i 1->n  算出f(i)的数值  因为计算fx肯定用到fx之前的数值 所以不会出现用到没有计算的值的时候

详见代码


记忆化搜索:

/** * from Guanpx * time 2017.3.3 */import java.util.*;public class Main {static long[] f = new long[10010];static long a,b,c,d;final static int zero = 0;final static long mod = 1000000007;public static long fun(int x) {if (f[x] != -1) {return f[x]%mod;} else { f[x] = (a*fun((int)Math.max(0,x-b))%mod + c*fun((int)Math.max(0,x-d))%mod)%mod; return f[x]%mod;}}public static void main(String[] args) {Scanner cin = new Scanner(System.in);int n,i;while (cin.hasNext()) {n = cin.nextInt();for (i = 1; i <= 10000; i++)  ///可以用初始化数组的函数 考虑时间f[i] = -1;f[0] = cin.nextInt();a = cin.nextInt();b = cin.nextInt();c = cin.nextInt();d = cin.nextInt();fun(n);System.out.println(f[n]);}}}


递推:

/** * from Guanpx * time 2017.3.7 */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;}System.out.println(f[n]);}}}




0 0
原创粉丝点击