熱門文章

2025年2月23日 星期日

ZeroJudge 解題筆記:f044. 2. 史萊姆生態區 (Slime)

作者:王一哲
日期:2025年2月23日



ZeroJudge 題目連結:f044. 2. 史萊姆生態區 (Slime)

解題想法


要先找到數據的規律,列出前 5 天的數據應該就能寫出數量與天數的關係式。
day big small total
0 1 0 1 = 20
1 1 1 2 = 21
2 1 1 + 2 = 3 1 + 3 = 4 = 22
3 1 3 + 7 = 7 1 + 7 = 8 = 23
4 1 7 + 8 = 15 1 + 15 = 16 = 24
5 1 15 + 16 = 31 1 + 31 = 32 = 25


Python 程式碼


使用時間約為 26 ms,記憶體約為 3.3 MB,通過測試。
n, t = map(int, input().split())  # 史萊姆王、小史萊姆的數量比例
if n > 1:  # 如果 n 大於 1,約分
    t //= n; n = 1
d, s = 0, 1  # 天數,總數
while s < n+t:  # 當 s 小於 n+t
    d += 1  # 天數加 1
    s *= 2  # s 變成 2 倍
print(d)
直接取 $\log_2$,使用時間約為 39 ms,記憶體約為 3.3 MB,通過測試。
import math
n, t = map(int, input().split())  # 史萊姆王、小史萊姆的數量比例
if n > 1:  # 如果 n 大於 1,約分
    t //= n; n = 1
print(int(math.log(n+t, 2)))


C++ 程式碼


使用時間約為 6 ms,記憶體約為 356 kB,通過測試。
#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, t; cin >> n >> t;  // 史萊姆王、小史萊姆的數量比例
    if (n > 1) {  // 如果 n 大於 1,約分
        t /= n; n = 1;
    }
    int d = 0, s = 1;  // 天數,總數
    while(s < n+t) {  // 當 s 小於 n+t
        d++;  // 天數加 1
        s *= 2;  // s 變成 2 倍
    }
    cout << d << "\n";
    return 0;
}
直接取 $\log_2$,使用時間約為 8 ms,記憶體約為 356 kB,通過測試。
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, t; cin >> n >> t;  // 史萊姆王、小史萊姆的數量比例
    if (n > 1) {  // 如果 n 大於 1,約分
        t /= n; n = 1;
    }
    cout << int(log2(n+t)) << "\n";
    return 0;
}


沒有留言:

張貼留言