2018CCPC桂林站

Ever_glow發表於2018-10-31

別再傻逼刪覺得重複沒用的程式碼讓自己遺憾終生了吧。

/*
Look at the star
Look at the shine for U
*/
#include<bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
#define sl(x) scanf("%lld",&x)
using namespace std;
const int N = 1e6+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll s[N],fac[N],n,cnt;
int main()
{
	ll n,i,j,k,sum,t,cas = 1;
	sl(t);
	while(t--)
	{
		sl(n);
		sum = 0;
		for(i = 0;i < n;i++){sl(s[i]),sum += s[i];}
		printf("Case %lld: ",cas++);
		if(sum == n)
		{
			puts("1");
			continue;
		}
		sort(s,s+n);
		ll flag = 0;
		for(i = 0;i < n;i++)
		{
			if(s[i]%s[0]) flag = 1;
		}
		if(!flag && s[0] != 1)
		{
			puts("0");
			continue;
		}
		ll Gcd = 0;
		for(i = 1;i < n;i++)
		{
			ll dis = s[i]-s[i-1];
			if(dis == 0) continue;
			Gcd = __gcd(Gcd,dis);
		}
		if(Gcd == 1)
		{
			puts("-1");
			continue;
		}
		flag = 0;
		for(i = 0;i < n;i++)
		{
			if(s[i]%Gcd) flag = 1;
		}
		if(!flag)
		{
			puts("0");
			continue;
		}
		cnt = 0;
		for(i = 2;i*i <= Gcd;i++)
		{
			if(Gcd%i == 0)
			{
				fac[cnt++] = i;
				fac[cnt++] = Gcd/i;
			}
		}
		fac[cnt++] = Gcd;
		sort(fac,fac+cnt);
		ll minn = 1e18;
		for(i = 0;i < cnt;i++)
			minn = min(minn,fac[i]-s[0]%fac[i]);
		printf("%lld\n",minn);
	}
}

 

相關文章