zoukankan      html  css  js  c++  java
  • A. Row GCD

    首先知道公式:

    1、gcd(a1,a2,a3,a4……)=gcd(a1,gcd(a2,a3,a4……))

    2、gcd(a1,a2) = gcd(a1 , a2-a1)

    推一下结论最后预处理一下 tp = gcd (a2 ​− a1​, ... , an ​− an−1​),

    最后输出 gcd(a1​ + bj​ , tp).

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+10;
    typedef long long ll;
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        ll a[N],b[N];
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=m;i++)
            scanf("%lld",&b[i]);
        ll c[N];
        int l=0;
        sort(a+1,a+n+1);
        for(int i=2;i<=n;i++){
            c[l]=a[i]-a[i-1];
            l++;
        }
        ll tp=c[0];
        for(int j=1;j<l;j++){
            tp=__gcd(tp,c[j]);
    
        }
        for(int i=1;i<=m;i++)
        cout<<__gcd(tp,a[1]+b[i])<<" ";
        cout<<endl;
    }
    View Code
  • 相关阅读:
    B
    B
    G
    F
    E
    A
    C
    2017icpc 乌鲁木齐网络赛
    bzoj 2038 小Z的袜子(hose)(莫队算法)
    矩阵快速幂刷题系列
  • 原文地址:https://www.cnblogs.com/sszywq/p/14310900.html
Copyright © 2011-2022 走看看