Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <vector>
- #include <string>
- #include <math.h>
- #include <algorithm>
- #include <map>
- #define lol long long
- using namespace std;
- int main()
- {
- ios_base::sync_with_stdio(0);
- int t;
- cin >> t;
- while (t--)
- {
- int a1, b1, a2, b2, z;
- cin >> a1 >> b1 >> a2 >> b2 >> z;
- int a = a2 - a1;
- int b = b1 - b2;
- if (a == 0)
- {
- if (b < 0)
- cout << max(z - 12, 0) << endl;
- else
- cout << 0 << endl;
- continue;
- }
- if (b == 0)
- {
- if (a < 0)
- cout << z << endl;
- else
- cout << 0 << endl;
- continue;
- }
- bool swapped = 0;
- if (a > 0)
- {
- swapped = 1;
- a *= -1;
- b *= -1;
- }
- double answ;
- if (a + b < 0)
- {
- answ = z;
- if (swapped)
- answ = 0;
- }
- else
- {
- int x;
- int l = 0;
- int r = z / 24;
- while (l <= r)
- {
- int m = (l + r) >> 1;
- int am = (b + a) * m;
- if (am - b >= 0)
- r = m - 1;
- else
- {
- x = m;
- l = m + 1;
- }
- }
- lol smu = (a + b) * x * 1ll * (x - 1) / 2;
- lol smd = (a + b) * x * 1ll * (x + 1) / 2;
- answ = (2 * x - smu * 1. / (-a) - smd * 1. / b) * 12;
- int left = z % 24;
- int l1 = min(left, 12);
- int l2 = max(0, left - 12);
- double w1 = (b + a) * x + a * l1 / 12.;
- if (w1 < 0)
- {
- answ += w1 / a * 12;
- double w2 = (b + a) * x + a + b * l2 / 12.;
- if (w2 > 0)
- answ += 12*(-((b + a) * x + a + 0.)/b);
- else
- answ += l2;
- }
- if (swapped)
- answ = z - answ;
- }
- cout.precision(9);
- cout << fixed << answ << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment