日期:2026年11月15日
ZeroJudge 題目連結:a830.超級細菌( bacteria )
解題想法
每組測資第一列為 6 個整數 $m, n, w, x, y, z$,分別代表一開始有 m 個成熟細菌、n 個新生細菌個,預設 $t = w$ 時的細菌總數,新生細菌經過時間 $x$ 成熟,成熟細菌經過時間 $y$ 繁衍一次,每一個成熟細菌可以産生 $z$ 個新生細菌。
這題可以開一個長度為 $w + 1$ 的陣列 $mature$,記錄每一天剛成熟或繁衍過的成熟細菌數量,再開另一個長度為 $w + 1$ 的陣列 $immature$,記錄每一天剛產生的新生細菌數量。接下來用一個 for 迴圈跑 $t = 1$ 到 $t = w$ 天,取出當天可以繁衍的成熟細菌數量 $a$,剛成熟的新生細菌數量 $b$,更新 $mature[t] = a + b$,$immature[t] = a \times z$。最後答案為 $mature[w - y + 1]$ 到 $mature[w - 1]$ 以及 $immature[w - x + 1]$ 到 $immature[w - 1]$ 的加總。如果不想要計算索引值,可以改用 queue 儲存數量,只記錄 $y$ 天對應的成熟細菌數量,$x$ 天對應的新生細菌數量,寫起來比較簡單。
Python 程式碼
使用時間約為 14 ms,記憶體約為 9.5 MB,通過測試。
while True:
try:
# t = 0,成熟細菌 m 個、新生細菌 n 個,預設 t = w 細菌總數
# 新生細菌經過時間 x 成熟,成熟細菌經過時間 y 繁衍一次、産生 z 個新生細菌
m, n, w, x, y, z = map(int, input().split())
except EOFError:
break
# 每一天產生或剛繁衍過的成熟細菌數量,每一天產生的未成熟細菌數量,最多 w 天
mature = [0] * (w + 1)
immature = [0] * (w + 1)
mature[0] = m
immature[0] = n
# 模擬過程,計算 t = 1 ~ w 各時刻的成熟細菌、新生細菌數量
for t in range(1, w + 1):
a = mature[t - y] if t >= y else 0 # 可以繁衍的成熟細菌數量
b = immature[t - x] if t >= x else 0 # 成熟的新生細菌數量
mature[t] = a + b # 前面取出的 a 個及剛成熟的 b 個細菌加入 mature 最後面
immature[t] = a * z # a 個成熟細菌,每個産生 z 個新生細菌,加到 immature 最後面
# 印出總數
print(sum(mature[w - y + 1:]) + sum(immature[w - x + 1:]))
使用時間約為 16 ms,記憶體約為 10 MB,通過測試。
while True:
try:
# t = 0,成熟細菌 m 個、新生細菌 n 個,預設 t = w 細菌總數
# 新生細菌經過時間 x 成熟,成熟細菌經過時間 y 繁衍一次、産生 z 個新生細菌
m, n, w, x, y, z = map(int, input().split())
except EOFError:
break
from collections import deque
# 成熟細菌數量,開頭是這個時刻可以繁衍的成熟細菌數量,每次更新移除首項,其它項向前移動一格
mature = deque([0] * (y - 1) + [m])
# 新生細菌數量,開頭是這個時刻成熟的細菌數量,每次更新移除首項,其它項向前移動一格
immature = deque([0] * (x - 1) + [n])
# 模擬過程,計算 t = 1 ~ w 各時刻的成熟細菌、新生細菌數量
for _ in range(w): # 更新 w 次
a = mature.popleft() # 取出可以繁衍的成熟細菌數量
b = immature.popleft() # 取出成熟的新生細菌數量
mature.append(a + b) # 前面取出的 a 個及剛成熟的 b 個細菌加入 mature 最後面
immature.append(a * z) # a 個成熟細菌,每個産生 z 個新生細菌,加到 immature 最後面
print(sum(mature) + sum(immature)) # 印出總數
C++ 程式碼
使用時間約為 2 ms,記憶體約為 3.6 MB,通過測試。
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
// t = 0,成熟細菌 m 個、新生細菌 n 個,預設 t = w 細菌總數
// 新生細菌經過時間 x 成熟,成熟細菌經過時間 y 繁衍一次、産生 z 個新生細菌
int m, n, w, x, y, z;
while(cin >> m >> n >> w >> x >> y >> z) {
// 每一天產生或剛繁衍過的成熟細菌數量,每一天產生的未成熟細菌數量,最多 w 天
vector<int> mature (w + 1, 0), immature (w + 1, 0);
mature[0] = m;
immature[0] = n;
// 模擬過程,計算 t = 1 ~ w 各時刻的成熟細菌、新生細菌數量
for(int t = 1; t <= w; t++) { // 更新 w 次
int a = 0, b = 0; // 可以繁衍的成熟細菌數量,成熟的新生細菌數量
if (t >= y) a = mature[t - y];
if (t >= x) b = immature[t - x];
mature[t] = a + b; // 前面取出的 a 個及剛成熟的 b 個細菌加入 mature 最後面
immature[t] = a * z; // a 個成熟細菌,每個産生 z 個新生細菌,加到 immature 最後面
}
// 印出總數
int ans = 0;
for(int t = w; t > w - y; t--) {
ans += mature[t];
}
for(int t = w; t > w - x; t--) {
ans += immature[t];
}
cout << ans << "\n";
}
return 0;
}
使用時間約為 2 ms,記憶體約為 3.6 MB,通過測試。
#include <iostream>
#include <queue>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
// t = 0,成熟細菌 m 個、新生細菌 n 個,預設 t = w 細菌總數
// 新生細菌經過時間 x 成熟,成熟細菌經過時間 y 繁衍一次、産生 z 個新生細菌
int m, n, w, x, y, z;
while(cin >> m >> n >> w >> x >> y >> z) {
// 成熟細菌數量,開頭是這個時刻可以繁衍的成熟細菌數量,每次更新移除首項,其它項向前移動一格
queue<int> mature;
for(int i = 0; i < y - 1; i++) {
mature.push(0);
}
mature.push(m);
// 新生細菌數量,開頭是這個時刻成熟的細菌數量,每次更新移除首項,其它項向前移動一格
queue<int> immature;
for(int i = 0; i < x - 1; i++) {
immature.push(0);
}
immature.push(n);
// 模擬過程,計算 t = 1 ~ w 各時刻的成熟細菌、新生細菌數量
for(int t = 0; t < w; t++) { // 更新 w 次
int a = mature.front(); // 取出可以繁衍的成熟細菌數量
mature.pop();
int b = immature.front(); // 取出成熟的新生細菌數量
immature.pop();
mature.push(a + b); // 前面取出的 a 個及剛成熟的 b 個細菌加入 mature 最後面
immature.push(a * z); // a 個成熟細菌,每個産生 z 個新生細菌,加到 immature 最後面
}
// 印出總數
int ans = 0;
while(!mature.empty()) {
ans += mature.front();
mature.pop();
}
while(!immature.empty()) {
ans += immature.front();
immature.pop();
}
cout << ans << "\n";
}
return 0;
}
C 語言程式碼
使用時間約為 1 ms,記憶體約為 1.6 MB,通過測試。
#include <stdio.h>
int main() {
// t = 0,成熟細菌 m 個、新生細菌 n 個,預設 t = w 細菌總數
// 新生細菌經過時間 x 成熟,成熟細菌經過時間 y 繁衍一次、産生 z 個新生細菌
int m, n, w, x, y, z;
while(scanf("%d %d %d %d %d %d", &m, &n, &w, &x, &y, &z) != EOF) {
// 每一天產生或剛繁衍過的成熟細菌數量,每一天產生的未成熟細菌數量,最多 100 天
int mature[101] = {0}, immature[101] = {0};
mature[0] = m;
immature[0] = n;
// 模擬過程,計算 t = 1 ~ w 各時刻的成熟細菌、新生細菌數量
for(int t = 1; t <= w; t++) { // 更新 w 次
int a = 0, b = 0; // 可以繁衍的成熟細菌數量,成熟的新生細菌數量
if (t >= y) a = mature[t - y];
if (t >= x) b = immature[t - x];
mature[t] = a + b; // 前面取出的 a 個及剛成熟的 b 個細菌加入 mature 最後面
immature[t] = a * z; // a 個成熟細菌,每個産生 z 個新生細菌,加到 immature 最後面
}
// 印出總數
int ans = 0;
for(int t = w; t > w - y; t--) {
ans += mature[t];
}
for(int t = w; t > w - x; t--) {
ans += immature[t];
}
printf("%d\n", ans);
}
return 0;
}
沒有留言:
張貼留言