Counterexample(gcd练习题)CodeForces - 483A

题目描述:
Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one.

Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime.

You want to find a counterexample for your friend’s statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l ≤ a < b < c ≤ r.

More specifically, you need to find three numbers (a, b, c), such that l ≤ a < b < c ≤ r, pairs (a, b) and (b, c) are coprime, and pair (a, c) is not coprime.

input:
The single line contains two positive space-separated integers l, r (1 ≤ l ≤ r ≤ 1018; r - l ≤ 50).

output:
Print three positive space-separated integers a, b, c — three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.

If the counterexample does not exist, print the single number -1.

样例输入:
2 4
样例输出:
2 3 4
样例输入:
10 11
样例输出:
-1
样例输入:
900000000000000009 900000000000000029
样例输出:
900000000000000009 900000000000000010 900000000000000021

题目大意:
我们要在r,l期间内找出三个数,这三个数要满足(a,b)互为质数,(b,c)互为质数,并且(a,c)不互为质数,如果在r,l期间有多组这样的数,随意输一组,按照小到大的顺序输出,如果没有则输出-1;
注:互为质数即两数的最大公因数是1
这个题就是典型的gcd题,并且虽然他的数据达到1e18但是,我们发现r,l之间的差值3不大于50,因此我们完全可以用三层for循环来进行查找;话不多说,上代码:

#include <iostream>
#include <cstdio>

using namespace std;
#define ll long long

ll gdc(ll a,ll b) { //找到两数的最大公因数
	while(b%a != 0){
		int ans = b%a;
		b = a;
		a = ans;
	}
	return a;
}
int main () {
	ll r,l,i,j,k;
	bool f = 0;
	cin >> r >> l;
	for (i = r;i <= l;i++) {
		for (j = i+1;j <= l;j++) {
			for (k = j+1;k <= l;k++) {
				if(gdc(i,j)==1&&gdc(j,k)==1&&gdc(i,k)!=1){//满足条件直接输出,然后跳出循环
					f = 1;
					cout << i << " " << j << " " << k << "\n";
					break;
				}
			}
			if(f)
				break;
		}
		if(f)
			break;
	}
	if(!f)
		cout << "-1\n";
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值