[关闭]
@DingCao-HJJ 2015-10-19T20:34:54.000000Z 字数 1176 阅读 1107

URAL_1222 Chernobyl’ Eagles

URAL


题目链接

题目大意

一只鹰的智商精确等于它的头的数目, 而一群鹰的智商等于各只鹰的智商的乘积。问给定一群鹰的总头数n,求这群赢的智商的最大值。

思路

求余,再乘上 n剩下的部分 分割成3的幂积(指数和底数都能相对较大)
然后如果余数小于等于1的话,就再加上三,当第一个基数大一点(这样和三比较接近)。

代码

python 比较简洁,但是效率一般

  1. # Copyright (c) 2015 HuangJunjie@SYSU(SNO:13331087). All Rights Reserved.
  2. # original from http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27826
  3. import sys
  4. num = sys.stdin.readline()
  5. n = int(num)
  6. result = n % 3
  7. n -= result
  8. if (result <= 1 and n > 1):
  9. result += 3
  10. n -= 3
  11. while n:
  12. result *= 3
  13. n -= 3
  14. print result

c++ 使用高精度,效率能高一点

  1. // Copyright (c) 2015 HuangJunjie@SYSU(SNO:13331087). All Rights Reserved.
  2. // original from http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27826
  3. #include <cstdio>
  4. #define MAX_SIZE 3000
  5. #define MOD 100000000
  6. int answer[MAX_SIZE + 1] = { 0 };
  7. int highest = 0; // the index to find the most significant element
  8. void multiply3() {
  9. int carry = 0;
  10. for (int i = 0; i <= highest; i++) {
  11. int remain = (answer[i] * 3 + carry) % MOD;
  12. carry = (answer[i] * 3 + carry) / MOD;
  13. answer[i] = remain;
  14. if (i == highest && carry) highest++;
  15. }
  16. }
  17. void output() {
  18. printf("%d", answer[highest]);
  19. for (int i = highest - 1; i >= 0; i--) printf("%08d", answer[i]);
  20. printf("\n");
  21. }
  22. int main() {
  23. int N;
  24. scanf("%d", &N);
  25. answer[0] = N % 3;
  26. N -= answer[0];
  27. if (answer[0] <= 1 && N >= 3) {
  28. answer[0] += 3;
  29. N -= 3;
  30. }
  31. while (N) {
  32. multiply3();
  33. N -= 3;
  34. }
  35. output();
  36. return 0;
  37. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注