2013年1月8日

UVa 11076 - Add Again


#include <stdio.h>

int main() {
  int i, j, n, fac[13] = {1};
  for (i = 1; i < 13; i++)
    fac[i] = fac[i - 1] * i;
  while (scanf("%d", &n) && n) {
    int check[10] = {}, sum = 0, kind = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &j);
      sum += j;
      if (!check[j]) kind++;
      check[j]++;
    }
    if (kind == 1) {
      if (sum / n) {
        for (i = 0; i < n; i++)
          printf("%d", sum / n);
        puts("");
      } else {
        puts("0");
      }
      continue;
    }
    int times = fac[n] / n;
    unsigned long long rec = 1;
    for (i = 0; i < 10; i++)
      if (!(sum % fac[check[i]])) sum /= fac[check[i]];
      else if (!(times % fac[check[i]])) times /= fac[check[i]];
      else rec *= fac[check[i]];
    unsigned long long temp, carry = 0, ans = 0, ten = 1;
    for (i = 0; i < n; i++) {
      temp = carry + times * sum / rec;
      carry = temp / 10;
      ans += ten * (temp % 10);
      ten *= 10;
    }
    if (carry) ans += ten * carry;
    printf("%llu\n", ans);
  }
  return 0;
}

沒有留言:

張貼留言