Algorithm
Problem link- https://www.spoj.com/problems/CEQU/
CEQU - Crucial Equation
Let us see the following equation,
ax+by=c
Given three positive integers a, b and c. You have to determine whether there exists at least one solution for some integers value of x and y where x, y may be negative or non-negative integers.
For example if a=2, b=4 and c=8 then the equation will be 2x+4y=8, and hence, for x=2 and y=1, there exists a solution.
Let us see another example for a=3, b=6 and c=7, so the equation will become 3x+6y=7 and there exists no solution satisfying this equation.
Input
Input starts with an integer T (1<=T<=105) denoting the number of test cases. Each test case contains three integers a, b, and c. (1<=a, b, c<=106).
Output
For each test case of input print the case number and “Yes” if there exists at least one solution, print “No” otherwise.
Sample Input |
Output for Sample Input |
2 |
Case 1: Yes |
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.util.*;
class A{
public static void main(String[] args){
final InputReader in = new InputReader(System.in);
final OutputWriter out = new OutputWriter(System.out);
int t = in.readInt();
Solver solveA = new Solver();
int c = 1;
while(t-- > 0){
solveA.solve(in, out, c);
c++;
}
out.flush();
out.close();
}
}
class Solver{
int gcd(int a, int b){
return (b==0)?a:gcd(b,a%b);
}
void solve(InputReader in, OutputWriter out, int count){
final int a = in.readInt();
final int b = in.readInt();
final int c = in.readInt();
int gcd = gcd(a, b);
if(c%gcd==0)
out.printLine("Case "+count+": Yes");
else
out.printLine("Case "+count+": No");
}
}
class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
class OutputWriter
{
private PrintWriter writer;
public OutputWriter(OutputStream outputStream)
{
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer)
{
this.writer = new PrintWriter(writer);
}
public void print(Object... objects)
{
for (int i = 0; i < objects.length; i++)
{
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects)
{
print(objects);
writer.println();
}
public void close()
{
writer.close();
}
public void flush()
{
writer.flush();
}
}
Copy The Code &
Try With Live Editor
Input
2 4 8
3 6 7
Output
Case 2: No
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
#define endl "\n"
#define fori(i,a,b) for(ll i=a;i<b;i++)
#define forr(i,a,b) for(ll i=a;i>=b;i--)
#define forit(it,x) for (auto it=(x).begin();it!=(x).end(); it++)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sll set<ll>
#define vll vector<ll>
#define msl map<string,ll>
#define mll map<ll,ll>
ll i, j, k;
ll gcd(ll a, ll b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
ll find_any_solution(ll a, ll b, ll c)
{
if(!(c%gcd(a,b)))
{
return 1;
}
else
return 0;
}
int main()
{
ll t, x=0;
cin >> t;
while(t--)
{
x++;
ll a, b, c;
cin >> a >> b >> c;
cout<<"Case "<<x;
if(find_any_solution(a, b, c))
{
cout <<": Yes"<<'\n';
}
else
cout <<": No"<<'\n';
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
2 4 8
3 6 7
Output
Case 2: No
Demonstration
SPOJ Solution-Crucial Equation-Solution in C, C++, Java, Python