CF1389A题解

A.LCM Problem

题目描述

Let LCM(x,y) be the minimum positive integer that is divisible by both x and y. For example, LCM(13,37)=481
, LCM(9,6)=18.
You are given two integers l and r. Find two integers x and y such that l≤x<y≤r and l≤LCM(x,y)≤r.

Input:
The first line contains one integer t(1≤t≤10000) — the number of test cases.
Each test case is represented by one line containing two integers l and r(1≤l<r≤1e9).

Output:
For each test case, print two integers:

  • if it is impossible to find integers x and y meeting the constraints in the statement, print two integers equal to −1;
  • otherwise, print the values of x and y(if there are multiple valid answers, you may print any of them).

题意概述

给你两个数,l和r,问是否存在两个数x和y,使得l<=x<y<=r,l<=lcm(x,y)<=r.存在的话,输出满足题意的一种即可,不存在则输出-1 -1.

解题思路

由于l和r的范围为1~1e9,范围很大,首先排除暴力解法。

我们再看数据有什么联系。lcm(x,y)是求x和y的最小公倍数,并且需要满足一个重要条件l<=lcm(x,y)<=r

当x是y的约数或者y是x的约数的时候,显然满足题意;

当不相互为约数的时候,我们可以选择x和lcm(x,y),找到一种情况使得它们为约数,找得到则存在,找不到则不存在输出-1 -1.如果l<=x,2x=lcm(x,y)<=r则满足x是lcm(x,y)的约数,输出x 2x即可。

所以我们可以尝试取

  • a=l
  • b=2a=2l

然后判断b<=r是否成立,如果成立,则输出a b;否则,输出-1 -1.

AC Code

#include <bits/stdc++.h>

#define endl '\n'
#define int long long

void solve(){
  int l, r;
  std::cin >> l >> r;
  if(2*l>r)
    std::cout << "-1 -1" << endl;
  else
    std::cout << l << " " << 2 * l << endl;
}

signed main()
{
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  
  int _;
  //_=1;
  std::cin>>_;
  while(_--){
    solve();
  }

  return 0;
}
posted @ 2025-05-19 08:57  渝州炒鸡舞帝神兽大王  阅读(73)  评论(0)    收藏  举报