From YoungProgrammer.com
LCM-GCD based question
Question:
We all interested in mathematics and world are divided in to two parts.
One who are interested in mathematics and other who are afraid of mathematics.
Here is a equation:
( X * N ) % Y = 0
Given two number X & Y you have to find minimum N that satisfies the equation.
Input:
Input consists of two positive integer X & Y . (1<=X,Y<=2000000000)
Output:
You have to output minimum N.
Sample Input:
1 5 6 7
Sample Output:
5
7
Note: You have to give the total of all output for each input.
For the sample input set, you have to give
7+5 = 12. So answer is 12.
Download data sets
ANS-562252
my solution
#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>
using namespace std;
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++)
typedef long long int int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;
template <class T> T sqr (T x) {return x * x;}
int gcd(int a,int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
int main()
{
int X,Y;
int total=0;
while(1)
{
cin>>X>>Y;
int hcf=gcd(X,Y);
int lcm=(X*Y)/hcf;
total+=(lcm/X);
cout<<total<<endl;
}
return 0;
}



Recent Comments