《牛客练习赛66》C

最大的gcd就是相邻的差距的gcd.

证明:

令p = 差距的gcd.

令a[1] = m*gcd. 那么

a[2] = m*gcd+k1*gcd.

a[3] = m*gcd+k2*gcd.

可以发现对于

a[2] = k1+a[1], a[3] = (k1+k2)*a[1], a[4] = (k1+k2+k3)*a[1] ....以此类推。

可以发现后面的数全都会满足a[1]的倍数.也就是gcd的倍数.

所以x要最小,需要a[1]变成向大扩展的最小的gcd的倍数.

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e6+5;
const int M = 1e6+5;
const int Mod = 1e9+9;
#define pi acos(-1)
#define INF 1e18
#define INM INT_MIN
#define rg register
#define pb(a)  push_back(a)
#define mk(a,b) make_pair(a,b)
#define dbg(x) cout << "now this num is " << x << endl;
#define met0(axx) memset(axx,0,sizeof(axx));
#define metf(axx) memset(axx,-1,sizeof(axx));
#define sd(ax) scanf("%d",&ax)
#define sld(ax) scanf("%lld",&ax)
#define sldd(ax,bx) scanf("%lld %lld",&ax,&bx)
#define sdd(ax,bx) scanf("%d %d",&ax,&bx)
#define sddd(ax,bx,cx) scanf("%d %d %d",&ax,&bx,&cx)
#define sfd(ax) scanf("%lf",&ax)
#define sfdd(ax,bx) scanf("%lf %lf",&ax,&bx)
#define pr(a) printf("%d\n",a)
#define plr(a) printf("%lld\n",a)
inline LL read()
{
    LL x = 0;int f = 1;char c = getchar();
    while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
    while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
    return x*f;
}
LL a[N],dis[N];
void run()
{
    int n;n = read();
    for(int i = 1;i <= n;++i) a[i] = read();
    sort(a+1,a+n+1);
    for(int i = 2;i <= n;++i) dis[i] = a[i]-a[i-1];
    LL tmp = dis[2];
    for(int i = 3;i <= n;++i) tmp = __gcd(tmp,dis[i]);
    LL ans;
    if(abs(a[1])%tmp == 0) ans = 0;
    else 
    {
        LL ma = abs(a[1])%tmp;
        if(a[1] < 0) ans = ma;
        else ans = tmp-ma;
    }
    printf("%lld %lld\n",tmp,ans);
}
int main()
{
    run();
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zwjzwj/p/13197217.html