cf 1216d

https://codeforc.es/problemset/problem/1216/D 

贪心:

找出最大的a[i],令为t,即假设这个t就是原来的x

然后b[i]=t-a[i];  b[i]表示每个缺了的数量。  

 

最后求一个所有b[i]的最大公约数。 

答案就是累加b[i]/gcd  

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 int const N = 200000 + 10;
 5 int a[N], b[N], n, ta;
 6 int gcd(int a, int b)
 7 {
 8     return b ? gcd(b, a % b) : a;
 9 }
10 int main()
11 {
12     scanf("%d", &n);
13     for (int i = 1; i <= n; i++)
14     {
15         scanf("%d", &a[i]);
16         ta = max(ta, a[i]);
17     }
18     for (int i = 1; i <= n; i++)
19         b[i] = ta - a[i];
20     int g = b[1];
21     for (int i = 2; i <= n; i++)
22         g = gcd(g, b[i]);
23     ll ans = 0;
24     for (int i = 1; i <= n; i++)
25         ans += b[i] / g;
26     printf("%lld %d\n", ans, g);
27     return 0;
28 }
View Code

 

posted @ 2019-09-27 13:18  zjxxcn  阅读(190)  评论(0编辑  收藏  举报