Codeforces Round #587 (Div. 3) D. Swords

链接:

https://codeforces.com/contest/1216/problem/D

题意:

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 z swords of some single type. Note that different people might have taken different types of swords. Note that the values x,y and z are unknown for you.

The next morning the director of the theater discovers the loss. He counts all swords — exactly ai swords of the i-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=3, a=[3,12,6] then one of the possible situations is x=12, y=5 and z=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,y and z beforehand but know values of n and a.

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

思路:

将最大值设为x, 求出x-每个值的gcd, 就是每个人能拿的最大值.

代码:

#include 
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;

LL a[MAXN];
int n;

int main()
{
    cin >> n;
    LL gcd = 0, mmax = 0;
    for (int i = 1;i <= n;i++)
        cin >> a[i], mmax = max(mmax, a[i]);
    for (int i = 1;i <= n;i++)
        gcd = __gcd(gcd, mmax-a[i]);
    LL num = 0;
    for (int i = 1;i <= n;i++)
        num += (mmax-a[i])/gcd;
    cout << num << ' ' << gcd << endl;

    return 0;
}

你可能感兴趣的:(Codeforces Round #587 (Div. 3) D. Swords)