Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int, int> pii;
- const int S = 101;
- const int N = 34;
- pii dp[2][N][S][N][S];
- inline void normalize(pii &p) {
- if(p.first == 0) {
- p.second = 1;
- return ;
- }
- if(p.second == 0) {
- p.first = 1;
- return ;
- }
- int sign = 0;
- if(p.first > 0 != p.second > 0) {
- sign = 1;
- }
- p.first = abs(p.first);
- p.second = abs(p.second);
- int g = __gcd(p.first, p.second);
- p.first /= g, p.second /= g;
- if(sign) p.first *= -1;
- }
- inline pii operator + (pii a, pii b) {
- int dn = a.second * b.second;
- int up = a.first * b.second + b.first * a.second;
- pii ret(up, dn);
- normalize(ret);
- return ret;
- }
- inline pii operator - (pii a, pii b) {
- pii c = b;
- c.first *= -1;
- return a + c;
- }
- inline bool operator < (pii &a, pii &b) {
- return (a.first * b.second < a.second * b.first);
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0); cout.tie(0);
- int n, k;
- cin >> n >> k;
- vector<int> a(n);
- for(int i=0; i<n; ++i) cin >> a[i];
- int tot = accumulate(a.begin(), a.end(), 0);
- vector<int> cum(n+1, 0);
- for(int i=0; i<n; ++i) cum[i+1] = cum[i] + a[i];
- for(int c1=1; c1+2<=n; ++c1) {
- for(int s1=c1; s1<=tot; ++s1) {
- for(int c2=1; c2+1<=n-c1; ++c2) {
- for(int s2=c2; s2<=tot; ++s2) {
- int c3 = n - c1 - c2;
- int s3 = tot - s1 - s2;
- if(c3 <= 0 or s3 <= 0 or s1+s2+s3 >= S) {
- dp[n&1][c1][s1][c2][s2] = pii(S*S, 1);
- continue;
- }
- pii p(s1, c1), q(s2, c2), r(s3, c3);
- pii avg = p + q + r;
- avg.second *= 3;
- normalize(avg);
- dp[n&1][c1][s1][c2][s2] = avg;
- cerr << c1 << " " << s1 << " | " << c2 << " " << s2 << ": " << avg.first << "/" << avg.second << "\n";
- }
- }
- }
- }
- pii mine(k, 1);
- for(int i=n-1; i>=0; --i) {
- for(int c1=0; c1+2<=i; ++c1) {
- for(int s1=c1; s1<=cum[i]; ++s1) {
- for(int c2=0; c2+1<=i-c1; ++c2) {
- for(int s2=c2; s2<=cum[i]; ++s2) {
- int c3 = i - c1 - c2;
- int s3 = cum[i] - s1 - s2;
- if(c3 < 0 or s3 < 0) continue;
- vector<pii> tempos;
- if(s1+a[i] <= tot) tempos.push_back(dp[(i&1)^1][c1+1][s1+a[i]][c2][s2]);
- if(s2+a[i] <= tot) tempos.push_back(dp[(i&1)^1][c1][s1][c2+1][s2+a[i]]);
- if(s3+a[i] <= tot) tempos.push_back(dp[(i&1)^1][c1][s1][c2][s2]);
- if(tempos.empty()) {
- dp[n&1][c1][s1][c2][s2] = pii(S*S, 1);
- continue;
- }
- pii best(S*S, 1), cur(S*S, 1);
- for(auto &qq : tempos) {
- auto temp = qq - mine;
- if(temp.first < 0) temp.first *= -1;
- if(temp < cur) best = qq, cur = temp;
- else if(temp == cur and qq < best) best = qq;
- }
- dp[n&1][c1][s1][c2][s2] = best;
- }
- }
- }
- }
- }
- auto res = dp[0][0][0][0][0];
- normalize(res);
- cout << res.first << "/" << res.second << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement