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

文章讲述了如何解决Codeforces问题,涉及在一个给定的区间[l,r]内找到两个数a和b,满足l 摘要由CSDN通过智能技术生成

Problem - C - Codeforces

题目大意:给出t个区间l,r,要求在每个区间内找到两个数a,b满足l<=a+b<=r,且gcd(a,b)!=1

1<=t<=500;1<=l<=r<=1e7

思路:记个结论:gcd(a,b-a)=gcd(a,b)

然后如果区间内有偶数就将其平分,如果只有奇数,就取一个非1的因数即可满足条件,因数和他倍数的gcd就是那个因数

//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
using namespace std;
const int N = 500 + 5;
typedef long long ll;
pair<int, int>a[105];
int n;
void init()
{

}
void solve()
{
	int l, r;
	cin >> l >> r;
	if (l!=r||(l==r&&r%2==0))
	{
		if (r <4)
		{//偶数不能是2,因为不能拆出1
			cout << -1 << endl;
			return;
		}
		if (r & 1)
		{
			cout << (r - 1) / 2 << " " << (r - 1) / 2 << endl;
		}
		else
		{
			cout << r / 2 << " " << r / 2 << endl;
		}
		return;
	}
	else
	{
		for (int i = 3; i * i <= l; i++)
		{
			if (l % i == 0)
			{
				cout << i << " " << l - i << endl;
				return;
			}
		}
		cout << -1 << endl;
	}
	init();
	
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timidcatt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值