Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using pld = pair<ld, ld>;
- #define fi first
- #define se second
- #define left BAO
- #define right ANH
- #define pb push_back
- #define pf push_front
- #define mp make_pair
- #define ins insert
- #define btpc __builtin_popcount
- #define btclz __builtin_clz
- #define sz(x) (int)(x.size());
- #define all(x) x.begin(), x.end()
- #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
- int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
- int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
- template<class X, class Y>
- bool minimize(X &x, const Y &y) {
- if (x > y)
- {
- x = y;
- return true;
- }
- return false;
- }
- template<class X, class Y>
- bool maximize(X &x, const Y &y) {
- if (x < y)
- {
- x = y;
- return true;
- }
- return false;
- }
- const int MOD = 1e9 + 7; //998244353
- template<class X, class Y>
- void add(X &x, const Y &y) {
- x = (x + y);
- if(x >= MOD) x -= MOD;
- }
- template<class X, class Y>
- void sub(X &x, const Y &y) {
- x = (x - y);
- if(x < 0) x += MOD;
- }
- /* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
- const ll INF = 1e9;
- const int N = 1e5 + 10;
- const int base = 1000000000;
- const int base_digits = 9;
- struct BigNum {
- vector<int> a;
- int sign;
- /*<arpa>*/
- int size() {
- if (a.empty())return 0;
- int ans = (a.size() - 1) * base_digits;
- int ca = a.back();
- while (ca)
- ans++, ca /= 10;
- return ans;
- }
- BigNum operator ^(const BigNum &v) {
- BigNum ans = 1, a = *this, b = v;
- while (!b.isZero()) {
- if (b % 2)
- ans *= a;
- a *= a, b /= 2;
- }
- return ans;
- }
- string to_string() {
- stringstream ss;
- ss << *this;
- string s;
- ss >> s;
- return s;
- }
- int sumof() {
- string s = to_string();
- int ans = 0;
- for (auto c : s) ans += c - '0';
- return ans;
- }
- /*</arpa>*/
- BigNum() :
- sign(1) {
- }
- BigNum(long long v) {
- *this = v;
- }
- BigNum(const string &s) {
- read(s);
- }
- void operator=(const BigNum &v) {
- sign = v.sign;
- a = v.a;
- }
- void operator=(long long v) {
- sign = 1;
- a.clear();
- if (v < 0)
- sign = -1, v = -v;
- for (; v > 0; v = v / base)
- a.push_back(v % base);
- }
- BigNum operator+(const BigNum &v) const {
- if (sign == v.sign) {
- BigNum res = v;
- for (int i = 0, carry = 0; i < (int) max(a.size(), v.a.size()) || carry; ++i) {
- if (i == (int) res.a.size())
- res.a.push_back(0);
- res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
- carry = res.a[i] >= base;
- if (carry)
- res.a[i] -= base;
- }
- return res;
- }
- return *this - (-v);
- }
- BigNum operator-(const BigNum &v) const {
- if (sign == v.sign) {
- if (abs() >= v.abs()) {
- BigNum res = *this;
- for (int i = 0, carry = 0; i < (int) v.a.size() || carry; ++i) {
- res.a[i] -= carry + (i < (int) v.a.size() ? v.a[i] : 0);
- carry = res.a[i] < 0;
- if (carry)
- res.a[i] += base;
- }
- res.trim();
- return res;
- }
- return -(v - *this);
- }
- return *this + (-v);
- }
- void operator*=(int v) {
- if (v < 0)
- sign = -sign, v = -v;
- for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {
- if (i == (int) a.size())
- a.push_back(0);
- long long cur = a[i] * (long long) v + carry;
- carry = (int) (cur / base);
- a[i] = (int) (cur % base);
- //asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
- }
- trim();
- }
- BigNum operator*(int v) const {
- BigNum res = *this;
- res *= v;
- return res;
- }
- friend pair<BigNum, BigNum> divmod(const BigNum &a1, const BigNum &b1) {
- int norm = base / (b1.a.back() + 1);
- BigNum a = a1.abs() * norm;
- BigNum b = b1.abs() * norm;
- BigNum q, r;
- q.a.resize(a.a.size());
- for (int i = a.a.size() - 1; i >= 0; i--) {
- r *= base;
- r += a.a[i];
- int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
- int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
- int d = ((long long) base * s1 + s2) / b.a.back();
- r -= b * d;
- while (r < 0)
- r += b, --d;
- q.a[i] = d;
- }
- q.sign = a1.sign * b1.sign;
- r.sign = a1.sign;
- q.trim();
- r.trim();
- return make_pair(q, r / norm);
- }
- BigNum operator/(const BigNum &v) const {
- return divmod(*this, v).first;
- }
- BigNum operator%(const BigNum &v) const {
- return divmod(*this, v).second;
- }
- void operator/=(int v) {
- if (v < 0)
- sign = -sign, v = -v;
- for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
- long long cur = a[i] + rem * (long long) base;
- a[i] = (int) (cur / v);
- rem = (int) (cur % v);
- }
- trim();
- }
- BigNum operator/(int v) const {
- BigNum res = *this;
- res /= v;
- return res;
- }
- int operator%(int v) const {
- if (v < 0)
- v = -v;
- int m = 0;
- for (int i = a.size() - 1; i >= 0; --i)
- m = (a[i] + m * (long long) base) % v;
- return m * sign;
- }
- void operator+=(const BigNum &v) {
- *this = *this + v;
- }
- void operator-=(const BigNum &v) {
- *this = *this - v;
- }
- void operator*=(const BigNum &v) {
- *this = *this * v;
- }
- void operator/=(const BigNum &v) {
- *this = *this / v;
- }
- bool operator<(const BigNum &v) const {
- if (sign != v.sign)
- return sign < v.sign;
- if (a.size() != v.a.size())
- return a.size() * sign < v.a.size() * v.sign;
- for (int i = a.size() - 1; i >= 0; i--)
- if (a[i] != v.a[i])
- return a[i] * sign < v.a[i] * sign;
- return false;
- }
- bool operator>(const BigNum &v) const {
- return v < *this;
- }
- bool operator<=(const BigNum &v) const {
- return !(v < *this);
- }
- bool operator>=(const BigNum &v) const {
- return !(*this < v);
- }
- bool operator==(const BigNum &v) const {
- return !(*this < v) && !(v < *this);
- }
- bool operator!=(const BigNum &v) const {
- return *this < v || v < *this;
- }
- void trim() {
- while (!a.empty() && !a.back())
- a.pop_back();
- if (a.empty())
- sign = 1;
- }
- bool isZero() const {
- return a.empty() || (a.size() == 1 && !a[0]);
- }
- BigNum operator-() const {
- BigNum res = *this;
- res.sign = -sign;
- return res;
- }
- BigNum abs() const {
- BigNum res = *this;
- res.sign *= res.sign;
- return res;
- }
- long long longValue() const {
- long long res = 0;
- for (int i = a.size() - 1; i >= 0; i--)
- res = res * base + a[i];
- return res * sign;
- }
- friend BigNum gcd(const BigNum &a, const BigNum &b) {
- return b.isZero() ? a : gcd(b, a % b);
- }
- friend BigNum lcm(const BigNum &a, const BigNum &b) {
- return a / gcd(a, b) * b;
- }
- void read(const string &s) {
- sign = 1;
- a.clear();
- int pos = 0;
- while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) {
- if (s[pos] == '-')
- sign = -sign;
- ++pos;
- }
- for (int i = s.size() - 1; i >= pos; i -= base_digits) {
- int x = 0;
- for (int j = max(pos, i - base_digits + 1); j <= i; j++)
- x = x * 10 + s[j] - '0';
- a.push_back(x);
- }
- trim();
- }
- friend istream& operator>>(istream &stream, BigNum &v) {
- string s;
- stream >> s;
- v.read(s);
- return stream;
- }
- friend ostream& operator<<(ostream &stream, const BigNum &v) {
- if (v.sign == -1)
- stream << '-';
- stream << (v.a.empty() ? 0 : v.a.back());
- for (int i = (int) v.a.size() - 2; i >= 0; --i)
- stream << setw(base_digits) << setfill('0') << v.a[i];
- return stream;
- }
- static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {
- vector<long long> p(max(old_digits, new_digits) + 1);
- p[0] = 1;
- for (int i = 1; i < (int) p.size(); i++)
- p[i] = p[i - 1] * 10;
- vector<int> res;
- long long cur = 0;
- int cur_digits = 0;
- for (int i = 0; i < (int) a.size(); i++) {
- cur += a[i] * p[cur_digits];
- cur_digits += old_digits;
- while (cur_digits >= new_digits) {
- res.push_back((int)(cur % p[new_digits]));
- cur /= p[new_digits];
- cur_digits -= new_digits;
- }
- }
- res.push_back((int) cur);
- while (!res.empty() && !res.back())
- res.pop_back();
- return res;
- }
- typedef vector<long long> vll;
- static vll karatsubaMultiply(const vll &a, const vll &b) {
- int n = a.size();
- vll res(n + n);
- if (n <= 32) {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- res[i + j] += a[i] * b[j];
- return res;
- }
- int k = n >> 1;
- vll a1(a.begin(), a.begin() + k);
- vll a2(a.begin() + k, a.end());
- vll b1(b.begin(), b.begin() + k);
- vll b2(b.begin() + k, b.end());
- vll a1b1 = karatsubaMultiply(a1, b1);
- vll a2b2 = karatsubaMultiply(a2, b2);
- for (int i = 0; i < k; i++)
- a2[i] += a1[i];
- for (int i = 0; i < k; i++)
- b2[i] += b1[i];
- vll r = karatsubaMultiply(a2, b2);
- for (int i = 0; i < (int) a1b1.size(); i++)
- r[i] -= a1b1[i];
- for (int i = 0; i < (int) a2b2.size(); i++)
- r[i] -= a2b2[i];
- for (int i = 0; i < (int) r.size(); i++)
- res[i + k] += r[i];
- for (int i = 0; i < (int) a1b1.size(); i++)
- res[i] += a1b1[i];
- for (int i = 0; i < (int) a2b2.size(); i++)
- res[i + n] += a2b2[i];
- return res;
- }
- BigNum operator*(const BigNum &v) const {
- vector<int> a6 = convert_base(this->a, base_digits, 6);
- vector<int> b6 = convert_base(v.a, base_digits, 6);
- vll a(a6.begin(), a6.end());
- vll b(b6.begin(), b6.end());
- while (a.size() < b.size())
- a.push_back(0);
- while (b.size() < a.size())
- b.push_back(0);
- while (a.size() & (a.size() - 1))
- a.push_back(0), b.push_back(0);
- vll c = karatsubaMultiply(a, b);
- BigNum res;
- res.sign = sign * v.sign;
- for (int i = 0, carry = 0; i < (int) c.size(); i++) {
- long long cur = c[i] + carry;
- res.a.push_back((int) (cur % 1000000));
- carry = (int) (cur / 1000000);
- }
- res.a = convert_base(res.a, 6, base_digits);
- res.trim();
- return res;
- }
- };
- const int LOG = 666;
- int fa[LOG + 2], fb[LOG + 2], fc[LOG + 2], fd[LOG + 2];
- BigNum f[LOG + 2][2][2][2][2], dp[LOG + 2][2][2][2][2], pw[LOG + 2];
- BigNum calc(int index, int a, int b, int c, int d) {
- if(index == 0) return 1;
- if(dp[index][a][b][c][d] != -1) return dp[index][a][b][c][d];
- BigNum res = 0;
- for(int x = 0; x < 2; x++) {
- for(int y = 0; y < 2; y++) {
- if(!a && x < fa[index - 1]) continue;
- if(!b && x > fb[index - 1]) continue;
- if(!c && y < fc[index - 1]) continue;
- if(!d && y > fd[index - 1]) continue;
- int na = (a || x > fa[index - 1]);
- int nb = (b || x < fb[index - 1]);
- int nc = (c || y > fc[index - 1]);
- int nd = (d || y < fd[index - 1]);
- res += calc(index - 1, na, nb, nc, nd);
- }
- }
- dp[index][a][b][c][d] = res;
- return res;
- }
- void BaoJiaoPisu() {
- BigNum a, b, c, d; cin >> a >> b >> c >> d;
- BigNum num = (b - a + 1) * (d - c + 1);
- num = (num + 1) / 2;
- for(int i = 0; i <= LOG; i++) {
- for(int x = 0; x < 2; x++) {
- for(int y = 0; y < 2; y++) {
- for(int z = 0; z < 2; z++) {
- for(int t = 0; t < 2; t++) {
- f[i][x][y][z][t] = 0;
- dp[i][x][y][z][t] = -1;
- }
- }
- }
- }
- }
- for(int i = 0; i <= LOG; i++) {
- fa[i] = (a % 2); a /= 2;
- fb[i] = (b % 2); b /= 2;
- fc[i] = (c % 2); c /= 2;
- fd[i] = (d % 2); d /= 2;
- }
- f[LOG][0][0][0][0] = 1;
- pw[0] = 1;
- BigNum ans = 0;
- for(int i = 1; i <= LOG; i++) pw[i] = pw[i - 1] * 2;
- for(int i = LOG; i > 0; i--) {
- BigNum one = 0, zero = 0;
- bool larger = false;
- for(int a = 0; a < 2; a++) {
- for(int b = 0; b < 2; b++) {
- for(int c = 0; c < 2; c++) {
- for(int d = 0; d < 2; d++) {
- if(f[i][a][b][c][d] == 0) continue;
- for(int x = 0; x < 2; x++) {
- for(int y = 0; y < 2; y++) {
- if(larger) continue;
- if(!a && x < fa[i - 1]) continue;
- if(!b && x > fb[i - 1]) continue;
- if(!c && y < fc[i - 1]) continue;
- if(!d && y > fd[i - 1]) continue;
- int na = (a || x > fa[i - 1]);
- int nb = (b || x < fb[i - 1]);
- int nc = (c || y > fc[i - 1]);
- int nd = (d || y < fd[i - 1]);
- if(x == y) zero += f[i][a][b][c][d] * calc(i - 1, na, nb, nc, nd);
- larger = (zero >= num);
- }
- }
- }
- }
- }
- }
- auto add = [&](int bit) -> void {
- for(int a = 0; a < 2; a++) {
- for(int b = 0; b < 2; b++) {
- for(int c = 0; c < 2; c++) {
- for(int d = 0; d < 2; d++) {
- if(f[i][a][b][c][d] == 0) continue;
- for(int x = 0; x < 2; x++) {
- for(int y = 0; y < 2; y++) {
- if((x ^ y) != bit) continue;
- if(!a && x < fa[i - 1]) continue;
- if(!b && x > fb[i - 1]) continue;
- if(!c && y < fc[i - 1]) continue;
- if(!d && y > fd[i - 1]) continue;
- int na = (a || x > fa[i - 1]);
- int nb = (b || x < fb[i - 1]);
- int nc = (c || y > fc[i - 1]);
- int nd = (d || y < fd[i - 1]);
- f[i - 1][na][nb][nc][nd] += f[i][a][b][c][d];
- }
- }
- }
- }
- }
- }
- };
- if(num > zero) {
- num -= zero; ans += pw[i - 1];
- add(1);
- } else add(0);
- }
- cout << ans << '\n';
- }
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- //online
- #endif
- int tc = 1, ddd = 0;
- cin >> tc;
- while(tc--) {
- //ddd++;
- //cout << "Case #" << ddd << ": ";
- BaoJiaoPisu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement