csu-2018年11月月赛Round2-div2题解
A(2193):昆虫繁殖
Description
科学家在热带森林中发现了一种特殊的昆虫,这种昆虫的繁殖能力很强。每对成虫过x个月产y对卵,每对卵要过两个月长成成虫。假设每个成虫不死,第一个月只有一对成虫,且卵长成成虫后的第一个月不产卵(过X个月产卵),问过Z个月以后,共有成虫多少对?0=<X<=20,1<=Y<=20,X=<Z<=50
Input
单组数据 x,y,z的数值
Output
过Z个月以后,共有成虫对数
Sample Input
1 2 8
Sample Output
37
题解:
从x+1到z+1循环,设a[i]为第i个月成虫的数量,b[i]为第i个月卵的数量。所以a[i] = a[i - 1] + b[i - 2](卵两个月长成成虫),b[i] = y * a[i - x](过x月产y对卵),则a[z + 1]就是答案。
初始条件为从1到x,a[i] = 1, b[i] = 0;
注意x == 0时,i初始值为1,b[i - 2] 会数组越界,这时单独判断一下。
代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline ll getnum() {
ll ans = 0; char c; ll flag = 1;
while (!isdigit(c = getchar()) && c != '-');
if (c == '-') flag = -1; else ans = c - '0';
while (isdigit(c = getchar())) ans = ans * 10 + c - '0';
return ans * flag;
}
ll a[100], b[100];
int main() {
ll x = getnum(), y = getnum(), z = getnum();
a[1] = 1;
for (int i = 1; i <= x; i++) {
a[i] = 1;
b[i] = 0;
}
for (int i = x + 1; i <= z + 1; i++) {
if (i == 1) {
a[i] = 1;
}
else a[i] = a[i - 1] + b[i - 2];
b[i] =