一,思路:
1.首先直接找两个数且满足条件是不好做的,不如换一种思路,我们根据范围去造两个数,是他们满足条件。那么该如何造呢?
2.首先我们要知道,我们不会拿一个质数去造那是造不出来的,而在所有的数中只有2,3这两个质数是连在一起的,也就是说我们根据l,r范围枚举的次数不会超过三次。
3.至于一个合数 x 我们找它的最小因数 p (质数找不到就跳过,最多也就遍历 sqrt(x) 次),那么答案必然是 p ,x - p。
二,代码
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
//找最小因数
int get(int x) {
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) return i;
}
return 0;
}
void sovle() {
int l, r;
cin >> l >> r;
for (int i = l; i <= r; i++) {
int p = get(i);
if (p == 0) continue;
else {
cout << p <<" "<< i - p << endl;
return;
}
}
cout << "-1" << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
sovle();
}
return 0;
}