日期:2026年5月18日
ZeroJudge 題目連結:d261. 11000 - Bee
解題想法
只有第一隻母蜂不會死,剩下的公蜂、母蜂每年結束都會死去。第 $i$ 年的母蜂數量等於 $1$ 加上第 $i-1$ 年的公蜂數量,第 $i$ 年的公蜂數量等於第 $i-1$ 年的公蜂加母蜂數量。
Python 程式碼
使用時間約為 15 ms,記憶體約為 8.4 MB,通過測試。
def solve():
import sys
maxn = 50
f = [0]*(maxn + 1)
m = [0]*(maxn + 1)
f[0] = 1
for i in range(1, maxn + 1):
f[i] = 1 + m[i-1]
m[i] = f[i-1] + m[i-1]
result = []
data = sys.stdin.read().split()
ptr = 0
while ptr < len(data):
n = int(data[ptr])
ptr += 1
if n == -1: break
result.append(f"{m[n]:d} {f[n]+m[n]:d}\n")
sys.stdout.write("".join(result))
if __name__ == "__main__":
solve()
C++ 程式碼
不能用 int,第 44 行測資會溢位。使用時間約為 4 ms,記憶體約為 1.5 MB,通過測試。
#include <cstdio>
typedef long long LL;
int main() {
const int maxn = 50;
LL f[maxn + 1] = {0}, m[maxn + 1] = {0};
f[0] = 1LL;
for(int i=1; i <= maxn; i++) {
f[i] = 1 + m[i-1];
m[i] = f[i-1] + m[i-1];
}
LL n;
while(scanf("%lld", &n) != EOF && n != -1LL) {
printf("%lld %lld\n", m[n], f[n]+m[n]);
}
return 0;
}
C 語言程式碼
不能用 int,第 44 行測資會溢位。使用時間約為 1 ms,記憶體約為 1.5 MB,通過測試。
#include <stdio.h>
typedef long long LL;
int main() {
LL f[51] = {0}, m[51] = {0};
f[0] = 1LL;
for(int i=1; i <= 50; i++) {
f[i] = 1 + m[i-1];
m[i] = f[i-1] + m[i-1];
}
LL n;
while(scanf("%lld", &n) != EOF && n != -1LL) {
printf("%lld %lld\n", m[n], f[n]+m[n]);
}
return 0;
}
沒有留言:
張貼留言