Algorithm
Problem link- https://www.spoj.com/problems/ENIGMATH/
ENIGMATH - PLAY WITH MATH
You would have been fed up with competitive programming questions so far, now it is time to solve little math.
Assume you have a equation A * x - B * y = 0
For a given value of A and B, find the minimum positive integer value of x and y that satisfies this equation.
Input
First line contains T, number of test cases 0 <= T <=1000 followed by T lines.
First line of each test case contains two space separated integers A and B. 1 <= A, B <=1 000 000 000.
Output
For each test case, output a single line containing two integers x and y (separated by a single space).
Example
Input: 1 2 3 Output: 3 2
Note:
- Brute force won't pass the given constraint.
- Negative number cases are avoided to make the problem easy.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#include <climits>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
#define MOD (ll)1000000007
#define pb push_back
#define EPS 1e-9
#define FOR(i, n) for(int i = 0;i < n; i++)
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
int t;
int main(){
cin>>t;
while(t--){
ll a, b;
cin>>a>>b;
ll g = gcd(a, b);
ll l = a*(b/g);
cout<<l/a<<' '<<l/b<<endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
2 3
Output
Demonstration
SPOJ Solution-PLAY WITH MATH-Solution in C, C++, Java, Python