GCD辗转相除的经典套路

- 前两个移动很像辗转相除法(这个套路在 Codeforces 上已经出烂了)<br>
- 后两个移动可以让 g 乘上 $2^k$
class Solution { public: bool isReachable(int X, int Y) { while(!(X&1))X >>= 1; while(!(Y&1))Y >>= 1; return gcd(X,Y) == 1; } };
- https://codeforces.com/contest/1766/problem/D
题意:
找到连续的最长gcd(a+k,b+k) == 1(a < b,k = {0,1,2,...})
- 思路:
- gcd(a+k,b+k) == gcd(a+k,b - a)
- a - b = 1时特判
- 可以推出
,具体证明见https://codeforces.com/blog/entry/110066 - 设两个的结果分别为g和h,显然a+k和b+k都是g的倍数,那么让其中一个大的倍数减掉一个小的倍数,显然还是g的倍数,充分性成立
- 反证法也同理,a+k和b-a肯定都是h的倍数,一个倍数加上另一个倍数,肯定还是h的倍数,必要性成立
- 接下来只需要找到b-a的质因子判断a+k是否有重复因子
- 一个一个加显然超时,我们可以先预处理出每个数字的最大质因子,然后利用传递性,得到关于b-a的所有质因子
- 设当前质因子为temp,更新答案的方法就是
#include<bits/stdc++.h> #define debug1(a) cout<<#a<<'='<< a << endl; #define debug2(a,b) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<endl; #define debug3(a,b,c) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl; #define debug4(a,b,c,d) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<endl; #define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<" "<<#e<<" = "<<e<<endl; #define debug0(x) cout << "debug0: " << x << endl #define fr(t, i, n)for (long long i = t; i < n; i++) #define YES cout<<"Yes"<<endl #define nO cout<<"no"<<endl #define fi first #define se second //#define int long long using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; //#pragma GCC optimize(3,"Ofast","inline") //#pragma GCC optimize(2) const int N = 2e5+10,M = 1e7+10; int to[M]; void solve() { int x,y;scanf("%d%d",&x,&y); if(y == x+1) { printf("-1\n"); return ; } int now = y - x; int ans = 1e9; for(;now != 1;now /= to[now]) ans = min(ans, (now - x % to[now]) % to[now]); printf("%d\n",ans); } signed main() { /* ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ to[1] = 1;//每个数字到它最大的质因子 for(int i = 2;i < M;i ++)if(!to[i]) { for(int j = i;j < M;j += i)to[j] = i; } int T = 1; scanf("%d",&T); //cin >> T; while(T--){ solve(); } return 0; }
- https://codeforces.com/contest/1762/problem/D
题意:
给一个长度为n的permutation,每次一个询问,得到结果为gcd(i,j),请在2*n次之内找到那个是0(或者哪两个之中之一是0)
思路:
- 三个指针i,j,k(i<j<k)
- 令x=gcd(a[i],a[j]),y=gcd(a[i],a[k]);
1. 如果x==y,显然a[i]>0
2. 如果x<y,可以证明a[j]>0
3. 如果x>y,可以证明a[k]>0
- 这样就可以写出一个答案了
#include<bits/stdc++.h> #define debug1(a) cout<<#a<<'='<< a << endl; #define debug2(a,b) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<endl; #define debug3(a,b,c) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl; #define debug4(a,b,c,d) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<endl; #define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<" "<<#e<<" = "<<e<<endl; #define debug0(x) cout << "debug0: " << x << endl #define fr(t, i, n)for (long long i = t; i < n; i++) #define YES cout<<"Yes"<<endl #define nO cout<<"no"<<endl #define fi first #define se second // #define int long long using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; //#pragma GCC optimize(3,"Ofast","inline") //#pragma GCC optimize(2) const int N = 2e5+10,mod = 998244353; bool st[N]; int ask(int a, int b) { cout << "? " << a << " " << b << endl; int ans = 0; cin >> ans; return ans; } void solve() { memset(st,0,sizeof st); int n;cin >> n; int a[3] = {1,2,3}; for(;;) { sort(a,a+3); if(a[2] > n)break; int x = ask(a[0],a[1]),y = ask(a[0],a[2]); if(x == y) { st[a[0]] = 1; a[0] = a[2] + 1; }else if(x < y) { st[a[1]] = 1; a[1] = a[2] + 1; }else{ st[a[2]] = 1; a[2] = a[2] + 1; } } cout << "! " << a[0] << " " << a[1] << endl; int t;cin >> t; } signed main() { /* ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int T = 1;cin >> T; while(T--){ solve(); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具