fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. void Code_By_Mohamed_Khaled() {
  6. ios_base::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8. #ifndef ONLINE_JUDGE
  9. freopen("input.txt", "r", stdin);
  10. freopen("output.txt", "w", stdout);
  11. #endif
  12. }
  13.  
  14. vector<ll> get_divisors(ll n) {
  15. vector<ll> div;
  16. n = abs(n);
  17. for (ll i = 1; i * i <= n; ++i) {
  18. if (n % i == 0) {
  19. div.push_back(i);
  20. div.push_back(-i);
  21. if (i * i != n) {
  22. div.push_back(n / i);
  23. div.push_back(-(n / i));
  24. }
  25. }
  26. }
  27. sort(div.begin(), div.end());
  28. return div;
  29. }
  30.  
  31. ll solve_cubic(ll a, ll b, ll c, ll d) {
  32. vector<ll> divisors = get_divisors(d);
  33. for (ll x : divisors) {
  34. ll val = a * x * x * x + b * x * x + c * x + d;
  35. if (val == 0) {
  36. return x; // Found integer root
  37. }
  38. }
  39. return LLONG_MIN; // No integer root found
  40. }
  41.  
  42. int main() {
  43. Code_By_Mohamed_Khaled();
  44. ll t;
  45. cin >> t;
  46. while (t--) {
  47. ll a, b, c, d;
  48. cin >> a >> b >> c >> d;
  49. ll root = solve_cubic(a, b, c, d);
  50. if (root == LLONG_MIN) {
  51. cout << "NO\n";
  52. } else {
  53. cout << "YES\n" << root << "\n";
  54. }
  55. }
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0.01s 5292KB
stdin
4
 1 0 0 0
 1 -1 1 -1
 1 1 1 1
 2 2 -23 443
stdout
NO
YES
1
YES
-1
NO