Number of Solutions for ax=b
Programming Basics · C++
TASK
Problem
Given integers `a` and `b` in the equation `a*x = b`, print `infinite` if every `x` is a solution, `none` if there are no solutions, and `one` otherwise.
EXAMPLE
Example
0 0
infinite
LIMITS
Constraints
|a|, |b| ≤ 10^9.
LEARN
Theory for this problem
+
An `if` / `else if` / `else` chain checks alternatives from top to bottom. Put more specific or higher-priority conditions first.
Connection to “Number of Solutions for ax=b”: here, branching selects a case using a Boolean condition; test order matters when cases overlap.
NEED HELP?
Hints
+
['List the possible cases and make them mutually exclusive.', 'After solving “Number of Solutions for ax=b”, verify the algorithm on your own small example and on an allowed boundary case. Print only the required result with no extra text.']
ANSWER
Solution
+
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long long a, b;
cin >> a >> b;
if (a == 0 && b == 0) cout << "infinite";
else if (a == 0) cout << "none";
else cout << "one";
cout << '\n';
return 0;
}