当前位置: 首页 > news >正文

区间内最远互质点对

AT_arc137_a [ARC137A] Coprime Pair - 洛谷

题目:

思路:

我们可以枚举 x 以及区间长度 len,那么 y 就是 x + len,如果有 gcd(x,y) = 1,那么就可以直接输出答案了,时间复杂度看似为 O(len²),但实则不然

具体的证明可看洛谷评论区,be like

https://en.wikipedia.org/wiki/Prime_gap

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include <iomanip>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <array>
#include <tuple>
using namespace std;
#define int long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endlint gcd(int a, int b)
{return !b ? a : gcd(b, a % b);
}void solve()
{int l, r;cin >> l >> r;int len = r - l;while (len){for (int i = l; i <= r - len; i++){if (gcd(i,i + len) == 1){cout << len << endl;return;}}len--;}
}
signed main()
{//cin.tie(0)->sync_with_stdio(false);int t = 1;//cin >> t;while (t--){solve();}return 0;
}

相关文章:

  • PostgreSQL初体验
  • ubuntu设置conda虚拟环境
  • 【TCGA-CRC】TCGA数据读取
  • 基于springboot的在线教育系统【附源码】
  • Kotlin 协程 (三)
  • 9、AI测试辅助-代码Bug分析提示词优化
  • 安卓settings单双屏显示
  • 用typoa写markdown文档笔记
  • 使用布隆过滤器实现java大数据筛选是否存在
  • 微软宣布的五大重要事项|AI日报0520
  • 微软开放代理网络愿景
  • 镜像管理(2)Dockerfile总结
  • vue3/vue2大屏适配
  • 扫盲笔记之NPM
  • Wan2.1 文生视频 支持批量生成、参数化配置和多语言提示词管理
  • C及C++的音频库与视频库介绍
  • WIFI信号状态信息 CSI 深度学习篇之CNN(Python)
  • 第5天-python饼图绘制
  • Jenkins:自动化之魂,解锁高效开发的密钥
  • 三、【数据建模篇】:用 Django Models 构建测试平台核心数据
  • 演员朱媛媛去世,其丈夫辛柏青发讣告
  • 受工友诱骗为获好处费代购免税品,海口海关:两当事人被立案
  • 国家发改委:不断完善稳就业稳经济的政策工具箱,确保必要时能够及时出台实施
  • 集齐中国泳坛“老中青”!200自潘展乐力压汪顺、孙杨夺冠
  • 4天内,云南昆明又一县市区原主官被查
  • 无人机企业从科技园区搬到乡村后,村子里变得不一样了