熱門文章

2025年8月6日 星期三

ZeroJudge 解題筆記:b893. 勘根定理

作者:王一哲
日期:2025年8月6日


ZeroJudge 題目連結:b893. 勘根定理

解題想法


因為 $-2147483647 \leq x^6 \leq 2147483647 ~\Rightarrow -36 \leq x \leq 36$,只要一路由 $-36$ 每次加 1 檢測到 $36$ 即可。 這題的敘述有問題,題目中說無解時輸出 N0THING! >\\<,但實際上是 N0THING! >\\\<。

Python 程式碼


使用時間約為 19 ms,記憶體約為 3.3 MB,通過測試。
def func(x):
    return a*x**5 + b*x**4 + c*x**3 + d*x**2 + e*x + f

a, b, c, d, e, f = map(int, input().split())
if a == b == c == d == e == f == 0:
    print("Too many... = =\"")
else:
    real = False
    for i in range(-36, 36):
        if func(i) == 0:
            print(f"{i:d} {i:d}")
            real = True
        elif func(i)*func(i+1) < 0:
            print(f"{i:d} {i+1:d}")
            real = True
    if not real: print("N0THING! >\\\\\\<")


C++ 程式碼


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

long a, b, c, d, e, f;
long func(long x) {
    return a*x*x*x*x*x + b*x*x*x*x + c*x*x*x + d*x*x + e*x + f;
}

int main() {
    cin >> a >> b >> c >> d >> e >> f;
    if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0 && f == 0) {
        cout << "Too many... = =\"\n";
    } else {
        bool real = false;
        for(long i=-36; i<36; i++) {
            if (func(i) == 0) {
                cout << i << " " << i << "\n";
                real = true;
            } else if (func(i)*func(i+1) < 0) {
                cout << i << " " << i+1 << "\n";
                real = true;
            }
        }
        if (!real) cout << "N0THING! >\\\\\\<\n";
    }
    return 0;
}


沒有留言:

張貼留言