代码:
#include <bits/stdc++.h>
using namespace std;
#typedef LL long long
LL l, r;
long long gcd(long long x, long long y)//gcd函数,不解释
{
if (x % y == 0)
return y;
else
return (gcd(y, x % y));
}
int main(){
cin >> l >> r;
for (LL i = r - l; i >= 0; i--) //枚举相差大小
for (LL j = l; j + i <= r;j++)//枚举小的那个数,其中的j+i<=r就是大数不超过区间右端点
if (gcd(j, j + i) == 1){
cout << i << "\n";
return 0;
}
return 0;
}
这样算是AC了,但是感觉不是太优化,毕竟1018呢,出组毒瘤数据可能就过不去了
所以发本帖是求更优解,顺便问一下tourist代码啥意思?
/**
* author: tourist
* created: 19.03.2022 15:00:02
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long l, r;
cin >> l >> r;
for (long long s = 0; s <= r - l - 1; s++) {
for (long long x = 0; x <= s; x++) {
long long y = s - x;
long long a = l + x;
long long b = r - y;
if (__gcd(a, b) == 1) {
cout << b - a << '\n';
return 0;
}
}
}
return 0;
}