Advertisement
leoanjos

Akinator

Feb 8th, 2023
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define llong __int128
  6. #define Fraction pair<llong, llong>
  7.  
  8. const int MAX = 100;
  9.  
  10. int n, k;
  11. llong sum;
  12. vector<llong> a;
  13. vector<vector<vector<pair<int, Fraction>>>> memo;
  14. char buffer[MAX];
  15.  
  16. void print(llong num) {
  17.     int idx = 0;
  18.     while (num) {
  19.         char c = '0' + (num % 10);
  20.         buffer[idx++] = c;
  21.         num /= 10;
  22.     }
  23.  
  24.     if (!idx) cout << "0";
  25.     else {
  26.         for (int i = idx - 1; i >= 0; i--)
  27.             cout << buffer[i];
  28.     }
  29. }
  30.  
  31. llong gcd(llong a, llong b) {
  32.     if (!b) return a;
  33.     return gcd(b, a % b);
  34. }
  35.  
  36. Fraction irreductible(llong num, llong den) {
  37.     llong g = gcd(num, den);
  38.     return make_pair(num / g, den / g);
  39. }
  40.  
  41. Fraction operator +(Fraction &a, Fraction &b) {
  42.     llong num = a.first * b.second + a.second * b.first;
  43.     llong den = a.second * b.second;
  44.     return irreductible(num, den);
  45. }
  46.  
  47. bool operator <(Fraction &a, Fraction &b) {
  48.     return a.first * b.second < a.second * b.first;
  49. }
  50.  
  51. pair<int, Fraction> average(int i, int j, int guesses = 0) {
  52.     if (guesses > k) return make_pair(false, make_pair(0LL, 0LL));
  53.     if (i == j) return make_pair(true, irreductible(a[i] * guesses, sum));
  54.  
  55.     auto [possible, ans] = memo[i][j][guesses];
  56.     if (~possible) return make_pair(possible, ans);
  57.  
  58.     possible = false;
  59.     ans = make_pair(LLONG_MAX, 1);
  60.  
  61.     for (int m = i; m < j; m++) {
  62.         auto [p1, a1] = average(i, m, guesses + 1);
  63.         auto [p2, a2] = average(m + 1, j, guesses + 1);
  64.  
  65.         if (p1 && p2) {
  66.             possible = true;
  67.             Fraction a = a1 + a2;
  68.             if (a < ans) ans = a;
  69.         }
  70.     }
  71.  
  72.     return memo[i][j][guesses] = make_pair(possible, ans);
  73. }
  74.  
  75. int main() {
  76.     ios_base::sync_with_stdio(false);
  77.     cin.tie(NULL);
  78.  
  79.     cin >> n >> k;
  80.  
  81.     sum = 0LL;
  82.     a.resize(n);
  83.  
  84.     for (int i = 0; i < n; i++) {
  85.         long long int num; cin >> num;
  86.         a[i] = num;
  87.         sum += num;
  88.     }
  89.  
  90.     sort(a.rbegin(), a.rend());
  91.  
  92.     memo.assign(n + 5, vector<vector<pair<int, Fraction>>>(n + 5, vector<pair<int, Fraction>>(k + 5, make_pair(-1, make_pair(LLONG_MAX, 1)))));
  93.  
  94.     auto [possible, ans] = average(0, n - 1);
  95.     if (!possible) cout << "No solution\n";
  96.     else {
  97.         print(ans.first);
  98.         cout << "/";
  99.         print(ans.second);
  100.         cout << "\n";
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement