Codeforces Round #657 (Div. 2)——B. Dubious Cyrpto题解

2020/7/20
题目:
Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l≤a,b,c≤r, and then he computes the encrypted value m=n⋅a+b−c.

Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that

a, b and c are integers,
l≤a,b,c≤r,
there exists a strictly positive integer n, such that n⋅a+b−c=m.
题意:
题意很简单,给你l、r、m,让你找到a,b,c(均在l和r之间),存在一个正整数n使得n*a+b-c=m;

思路:
首先考虑n*a与m的差值,不难发现,对于所有正整数n使得n*a和m的差值最小为m%a和a-m%a(当a>m的时候,则只有a-m%a)。具体见图:
在这里插入图片描述
因此,若想使得n*a+b-c=m,则只要|b-c|>=m%a,或者|b-c|<=a-m%a即可。(还是若i>m,则只能|b-c|<=a-m%a),易得|b-c|<=r-l。所以我们只需要枚举a从l到r,看看存不存在上述的情况,然后根据差值赋值即可。
代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		long long l, r, m;
		cin >> l >> r >> m;
		long long i;
		long long a, b, c;
		for (i = l; i <= r; i++)
		{
			if (m >= i)
			{
				if (m % i<= (r - l))
				{
					a = i;
					c = l;
					b = l + m % i;
					break;
				}
				if (i - m % i <= (r - l))
				{
					a = i;
					b = l;
					c = l + i - m % i;
					break;
				}
			}
			else
			{
				if (i-m <= (r - l))
				{
					a = i;
					b = l;
					c = l + i - m;
					break;
				}
			}
		}
		cout << a << ' ' << b << ' ' << c << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值