Panasonic Programming Contest 2020 C (Sqrt Inequality) Solution

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed

Problem Statement

Given three integers $a$, $b$, and $c$, output Yes if $\sqrt a + \sqrt b < \sqrt c$ holds, otherwise output No.

Examples

Sample Input #1

1
2 3 9

Sample Output #1

1
No

$\sqrt 2 + \sqrt 3 < \sqrt 9$ does not hold.

Sample Input #2

1
2 3 10

Sample Output #2

1
Yes

$\sqrt 2 + \sqrt 3 < \sqrt 10$ holds.

Analysis

Incorrect Approach

Directly using the system’s sqrt function with floating-point precision errors will result in wrong answers:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <cstdio>
#include <cmath>
using namespace std;

int main(int argc, char** argv)
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    double d = sqrt(double(a)) + sqrt(double(b));
    puts(d * d < c? "Yes": "No");
    return 0;
}

This approach requires special handling to avoid precision issues!

Correct Approach

Derivation steps:
$\sqrt a + \sqrt b < \sqrt c$
$(\sqrt a + \sqrt b)^2 < (\sqrt c)^2$
$a + b + 2\sqrt{ab} < c$
$2\sqrt{ab} < c - a - b$
$(2\sqrt{ab})^2 < (c - a - b)^2$
$4ab < (c - a - b)^2$

Note: An additional case occurs when $c - a - b < 0$ (i.e., $c < a + b$), which should directly output No. Failure to consider this will result in WA, as $(c - a - b)^2$ ignores negative values!

Solution Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <cstdio>
using namespace std;

int main(int argc, char** argv)
{
    long long a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    long long d = c - a - b;
    if(d < 0) puts("No"); // Special case when c - a - b < 0 directly output No
    else puts((d * d > 4LL * a * b)? "Yes": "No");
    return 0;
}

AC Screenshot

Built with Hugo
Theme Stack designed by Jimmy