【CFRound#587-D】Swords 差值gcd

题目链接:http://codeforces.com/contest/1216/problem/D

Description

There were n types of swords in the theater basement which had been used during the plays. Moreover there were exactly x swords of each type. y people have broken into the theater basement and each of them has taken exactly zz swords of some single type. Note that different people might have taken different types of swords. Note that the values x,yx,y and zz are unknown for you.

The next morning the director of the theater discovers the loss. He counts all swords — exactly aiai swords of the ii-th type are left untouched.

The director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken.

For example, if n=3n=3, a=[3,12,6]a=[3,12,6] then one of the possible situations is x=12x=12, y=5y=5 and z=3z=3. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values x,yx,y and zz beforehand but know values of nn and aa.

Thus he seeks for your help. Determine the minimum number of people yy, which could have broken into the theater basement, and the number of swords zz each of them has taken.

Input

The first line of the input contains one integer n(2n2105)(2≤n≤2⋅105) — the number of types of swords.

The second line of the input contains the sequence a1,a2,,ana1,a2,…,an (0ai109)(0≤ai≤109), where aiai equals to the number of swords of the ii-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices (j,k)(j,k) that ajakaj≠ak.

Output

Print two integers yy and zz — the minimum number of people which could have broken into the basement and the number of swords each of them has taken.

烫脚中文题意

n个堆,堆里石子数相同,有y个人过来洗劫,每个人固定拿走z个石子,剩下a1,a2,...an个石子,问当y最小值时,输出yz

思路

因为之前石子数相同,所以可以肯定剩下的石子中最大值即为之前所有相同石子的值。

证明过程:

max+q=ai+p+q 显然比 max=ai+p 处理数字多两个人,会使y增大。

然后求每个数和最大值之差,求个gcd就行了。

 

AC代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int SIZE=2*1e5+50;
 5 ll a[SIZE];
 6 ll d[SIZE];
 7 bool cmp(ll x,ll y){return x>y;}
 8 int main(){
 9     int n;scanf("%d",&n);
10     for(int i=0;i<n;i++){
11         scanf("%lld",&a[i]);
12     }
13     sort(a,a+n,cmp);
14 
15     //此处不特判也行
16     if(n==2){
17         printf("1 %lld
",a[0]-a[1]);return 0;
18     }
19 
20     ll sum=0;d[0]=0;
21     for(int i=1;i<n;i++){
22         d[i]=a[0]-a[i];
23         sum+=d[i];//求差值之和
24     }
25     int pos=1;
26     ll gcd;
27 
28     int flag=0;//是否需要求gcd
29     for(int i=1;i<n-1;i++){
30         if(d[i]==0)continue;
31         else{
32             gcd=__gcd(d[i],d[i+1]);
33             pos=i;flag=1;
34             break;
35         }
36     }
37 
38     if(flag){
39         for(int i=pos+1;i<n;i++){//比赛时把i<n写成了i<n-1导致一直WA,写代码要细心啊!
40             gcd=__gcd(gcd,d[i]);
41         }
42         printf("%lld %lld
",sum/gcd,gcd);
43     }
44 
45     else printf("1 %lld
",sum);
46 }
原文地址:https://www.cnblogs.com/tudouuuuu/p/11565365.html