Advertisement
Guest User

Candy's Candy Solution (CT S02E11, Problem C)

a guest
Dec 3rd, 2014
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. //Solution by sandyeep
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. long long n, a[100100], f, gcd, ans, mn;
  6. vector<long long> factors;
  7.  
  8. void jain()
  9. {
  10.     gcd = 0;
  11.     for(int i=0; i<n; i++)  {
  12.         cin >> a[i];
  13.         gcd = __gcd(gcd, a[i]);
  14.     }
  15.     factors.clear();
  16.     for(int i=1; i * i <= gcd; i++) {
  17.         if(gcd % i == 0)    {
  18.             factors.push_back(i);
  19.             if(i * i != gcd)    {
  20.                 factors.push_back(gcd / i);
  21.             }
  22.         }
  23.     }
  24.     sort(factors.begin(), factors.end());
  25.     ans = 0;
  26.     for(auto k : factors)   {
  27.         bool ok = true;
  28.         long long rem = (a[0] / k) % n;
  29.         mn = (a[0] / k - 1) / n;
  30.         for(int i=1; i < n; i++)    {
  31.             if((a[i] / k) <= n or ((a[i] / k) % n) != rem)  {
  32.                 ok = false;
  33.             }
  34.             mn = min(mn, (a[i] / k - 1) / n);
  35.         }
  36.         if(ok)  {
  37.             ans += mn;
  38.         }
  39.     }
  40.     cout << ans << "\n";
  41. }
  42.  
  43. int main()
  44. {
  45.     ios::sync_with_stdio(false);
  46.     while(1)    {
  47.         cin >> n;
  48.         if(n == 0)  {
  49.             break;
  50.         }
  51.         jain();
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement