GCD辗转相除的经典套路

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)
    1. a - b = 1时特判
    2. 可以推出$gcd(a+k,b+k) == gcd(a+k,b - a)$,具体证明见https://codeforces.com/blog/entry/110066
    3. 设两个的结果分别为g和h,显然a+k和b+k都是g的倍数,那么让其中一个大的倍数减掉一个小的倍数,显然还是g的倍数,充分性成立
    4. 反证法也同理,a+k和b-a肯定都是h的倍数,一个倍数加上另一个倍数,肯定还是h的倍数,必要性成立

  • 接下来只需要找到b-a的质因子判断a+k是否有重复因子
  • 一个一个加显然超时,我们可以先预处理出每个数字的最大质因子,然后利用传递性,得到关于b-a的所有质因子
  • 设当前质因子为temp,更新答案的方法就是$ans = min(ans,(temp - a%temp) % 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("-1n");
        return ;
    }
    
    int now = y - x;
    int ans = 1e9;
    for(;now != 1;now /= to[now])
        ans = min(ans, (now - x % to[now]) % to[now]);
    
    printf("%dn",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;
}

 

原文链接: https://www.cnblogs.com/cfddfc/p/17064646.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    GCD辗转相除的经典套路

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/313576

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午12:57
下一篇 2023年2月16日 下午12:57

相关推荐