Y病毒
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int delay = in.nextInt(); int forget = in.nextInt(); int arr[] = new int[n+1]; int brr[] = new int[n+1]; //对特殊情况的判断 if (forget<=delay){//无法繁殖情况 if (n <= forget+1)//forget为几天后 System.out.println(1); else System.out.println(0); return; } for (int i = 1; i <= delay; i++) { if (i==1) arr[1] = 1;//第一天生成一个病毒 else arr[i] = 0; } //使用数组记录每天增长的数量 //1+delay天后开始繁殖 for (int i = delay+1 ; i <= n; i++) { int preSum = 0; //for (int j = i-1-(forget-delay); j < i-1; j++) {//求解目前在繁殖期的所有病毒 for (int j = i-forget+1; j < i-delay+1; j++) {//求解目前在繁殖期的所有病毒 //从delay --- forget 是他的繁殖时间长度,即求解 i-1-2 === i-2 if(j<1) continue;//前面的左边界可能会越界 preSum += arr[j]; } arr [i] =preSum;//此时要算有繁殖能力的病毒总数 } //用brr数组存储该时刻消失的病毒 for (int i = 1; i <= forget; i++) { brr[i] = 0; } for (int i = forget+1 ; i <= n; i++) {//forget后开始消失 brr [i] =arr[i-forget];//此时要算正好forget天前生成的病毒 } int ans = 0; for (int i = 1; i <= n; i++) { ans+=(arr[i]-brr[i])%1000000007; } System.out.println(ans); } }
测试用例:
6 2 4
5