目录
题目及要求描述
题目
输入输出范例以及释义
输入:
11
11 15
1 3
18 19
41 43
777 777
8000000 10000000
2000 2023
1791791 1791791
1 4
2 3
9840769 9840769
输出:
6 9
-1
14 4
36 6
111 666
4000000 5000000
2009 7
-1
2 2
-1
6274 9834495
分析与解答
题目本质解析
对于不同的l与r的取值,找到不同的范围,从这些范围进行分类,考虑l与r 之间相等,不等,有偶数,无偶数,相等且为质数,均小于3等多种情况
最终通过代码
#include "bits/stdc++.h"
using namespace std;
//#define int long long
pair<int, int> Judge(int n) {
pair<int, int> res(0,0);
if (n % 2 == 0)
res.first = 2, res.second = n - 2;
else
for (int i = 3; i <=(int) sqrt(n); i++) {
if (n % i == 0) {
res.first = i, res.second = n - i;
break;
}
}
return res;
}
int32_t main() {
int num;
int tmp, l, r;
cin >> num;
for (int i = 0; i < num; i++) {
cin >> l >> r;
if (r <= 3)
cout << -1 << endl;
else {
if (l==4||r == 4)
cout << 2 << " " << 2 << endl;
else if (r == l) {
pair<int, int> ans;
ans = Judge(l);
if (ans.first)
cout << ans.first << " " << ans.second << endl;
else cout << -1 << endl;
} else {
for (int j = l; j <= r; j++) {
if (j % 2 == 0&&j>2) {
cout << 2 << " " << j - 2 << endl;
break;
}
}
}
}
}
return 0;
}
学习到的
- 首先就是对于判断一个数字是否为质数的代码,for循环中写的判断语句应该是i<sqrt(n)而不应该是其他。
- 其次就是对于加快时间我学会了在main函数内部开头加一个ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- 同时还要习惯如果数大了的话一定记得用long long
- 唔,对于cf上总是先告诉你有几个测试用例这种,可以使用inline函数