Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define pb push_back
- signed main()
- {
- int n , m;
- cin >> n >> m;
- int a = n , b = m;
- vector<int> A;
- //for n
- for(int i = 2; (i * i) <= n; i++){
- if(n % i)
- continue;
- while(n % i == 0){
- A.pb(i);
- n /= i;
- }
- }
- if(n > 1)
- A.pb(n);
- //for m
- vector<int> B;
- for(int i = 2; (i * i) <= m; i++){
- if(m % i)
- continue;
- while(m % i == 0){
- B.pb(i);
- m /= i;
- }
- }
- if(m > 1)
- B.pb(m);
- int x = A.size() , y = B.size();
- vector<int> GCD;
- int ind = 0;
- for(int i = 0; i < x; i++){
- while(B[ind] < A[i]){
- ind++;
- }
- if(A[i] == B[ind]){
- GCD.pb(A[i]);
- ind++;
- }
- }
- int ans = 1;
- for(int i : GCD)
- ans *= i;
- int lcm = (a * b) / ans;
- cout << ans << endl;
- cout << lcm << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement