大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

2 weeks ago
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <numeric>
  5. #include <algorithm>
  6. #include <cmath>
  7. using namespace std;
  8. // 计算一个数的质因数的最小公倍数
  9. long long lcm(long long a, long long b) {
  10. return a / __gcd(a, b) * b;
  11. }
  12. // 计算一个城堡的完美度
  13. long long calculateCastlePerfectness(const vector<pair<int, int>>& blocks) {
  14. long long product = 1;
  15. for (const auto& block : blocks) {
  16. product *= pow(block.first, block.second);
  17. }
  18. return product;
  19. }
  20. int main() {
  21. int n;
  22. cin >> n;
  23. vector<long long> perfectness(n);
  24. vector<vector<pair<int, int>>> castles(n);
  25. for (int i = 0; i < n; ++i) {
  26. int ti;
  27. cin >> ti;
  28. castles[i].resize(ti);
  29. for (int j = 0; j < ti; ++j) {
  30. int Pij, Cij;
  31. cin >> Pij >> Cij;
  32. castles[i][j] = {Pij, Cij};
  33. }
  34. perfectness[i] = calculateCastlePerfectness(castles[i]);
  35. }
  36. // 计算总完美度
  37. set<long long> uniquePerfectness;
  38. for (int i = 0; i < n; ++i) {
  39. long long currentLcm = 1;
  40. for (int j = 0; j < n; ++j) {
  41. if (j != i) {
  42. currentLcm = lcm(currentLcm, perfectness[j]);
  43. }
  44. }
  45. uniquePerfectness.insert(currentLcm);
  46. }
  47. // 输出不同的总完美度的种数
  48. cout << uniquePerfectness.size() << endl;
  49. return 0;
  50. }