Codeforces Round 895 (Div. 3)C.Non-coprime Split

目录

题目及要求描述

题目 

输入输出范例以及释义

输入:

输出:

分析与解答 

题目本质解析

最终通过代码

 学习到的


 

题目及要求描述

题目 

输入输出范例以及释义

输入:

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函数
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值