Source Code
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using std::chrono::high_resolution_clock;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int>pi;
typedef pair<ll, ll>pl;
typedef vector<pi>vpi;
typedef vector<pl>vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vs;
const double PI = acos(-1);
const int oo = 1e9 + 7;
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
#define endl '\n'
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define read(v) for (auto& it : v) scanf("%d", &it);
#define readL(v) for (auto& it : v) scanf("%lld", &it);
#define print(v) for (auto& it : v) printf("%d ", it); puts("");
#define printL(v) for (auto& it : v) printf("%lld ", it); puts("");
auto Time() {
	return high_resolution_clock::now();
}
ll gcd(ll a, ll b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
	return ((b / gcd(a, b)) * a);
}
ll calc(ll a, ll b) {
	return (lcm(a, b) / gcd(a, b));
}
void solve() {
	ll a, n;
	scanf("%lld %lld", &n, &a);
	ll x = a + 1;
	for (ll i = n; i > 0; i--) {
		if (gcd(a, i) == 1) {
			x = i;
			break;
		}
	}
	for (ll i = a; i <= n; i++) {
		if (calc(i, a) == calc(x, a)) {
			printf("%lld\n", i);
			return;
		}
	}
}
int t = 1;
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	scanf("%d", &t);
	while (t--) solve();
}
Copy
LCM and GCD Malik
GNU G++17
2084 ms
272 KB
Time Limit Exceeded