20B - Equation - CodeForces Solution


math *2000

Please click on ads to support us..

Python Code:

a,b,c=list(map(int,input().split()))
if a==0 and b==0 and c==0:
    print(-1)
elif a==0 and b==0:
    print(0)
elif a==0 and c==0:
    print(1)
    print(0)
elif b==0 and c==0:
    print(1)
    print(0)
elif a==0:
    print(1)
    print(-c/b)
else:
    if (b**2)-(4*a*c)>0:
        print(2)
        print(min((-b-((b**2)-(4*a*c))**0.5)/(2*a),(-b+((b**2)-(4*a*c))**0.5)/(2*a)))
        print(max((-b-((b**2)-(4*a*c))**0.5)/(2*a),(-b+((b**2)-(4*a*c))**0.5)/(2*a)))
    elif (b**2)-(4*a*c)==0:
        print(1)
        print(-b/(2*a))
    else:
        print(0)

C++ Code:

#include <bits/stdc++.h>
#include <cstring>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double db;
//typedef double db;
#define ss second
#define ff first
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define vii vector<pii>
#define vl vector<ll>
#define pll pair<ll,ll>
#define vll vector<pll>
#define vd vector<db>
#define pdd  pair<db,db>
#define vdd  vector<pdd>
#define mll map <ll,ll>
#define sl set <ll>
#define sll set <pll>
#define f(i,j,k,l) for(ll i=j;i<k;i+=l)
#define fj(j,m) for(auto j=m.begin();j!=m.end();j++)
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define endl "\n"
const db pi=3.14159265359;
const int mod = 1e9+7;
const db EPS = 0.000000001; // 1 e -9
const ll inf = (ll)1e18;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);}

int run_case()
{
    ll  r,k,u,p,i,n,h,y,z,l,e,q,w,x,j;
    cin >> k >> n >> x ;
    cout << setprecision(5) << fixed;
    if (k==0)
    {
        if (n==0)
        {
            if (x==0)
            {
                cout << -1 << endl;
            }
            else
            {
                cout << 0 << endl;
            }
        }
        else
        {
            cout << 1 << endl;
            cout << -(db)x/(db)n << endl;
        }
    }
    else
    {
        p=n*n-4*k*x;
        if (p>0)
        {
            cout << 2 << endl;
            cout << min((-(db)n-sqrt(p))/(2.0*k),(-(db)n+sqrt(p))/(2.0*k)) << endl;
            cout << max((-(db)n+sqrt(p))/(2.0*k),(-(db)n-sqrt(p))/(2.0*k)) << endl;
        }
        else if (p==0)
        {
            cout << 1 << endl;
            cout << -(db)n/(2.0*k) << endl;
        }
        else
        {
            cout << 0 << endl;
        }
    }

return 0;
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll t = 1;//cin >> t;
    while(t--){
        run_case();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes