求助一道Atcoder的非主题库题(难度不大,ARC的A)
  • 板块学术版
  • 楼主van_Dijk
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/4/9 18:34
  • 上次更新2023/10/28 04:10:19
查看原帖
求助一道Atcoder的非主题库题(难度不大,ARC的A)
644697
van_Dijk楼主2022/4/9 18:34

这道

代码:

#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了,但是感觉不是太优化,毕竟101810^18呢,出组毒瘤数据可能就过不去了

所以发本帖是求更优解,顺便问一下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;
}
2022/4/9 18:34
加载中...