Submission #1258790

#TimeUsernameProblemLanguageResultExecution timeMemory
1258790proofyIntergalactic ship (IZhO19_xorsum)C++20
36 / 100
1545 ms327680 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

template <typename T>
T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a; swap(a, m);
    u -= t * v; swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T>
class Modular {
 public:
  using Type = typename decay<decltype(T::value)>::type;

  constexpr Modular() : value() {}
  template <typename U>
  Modular(const U& x) {
    value = normalize(x);
  }

  template <typename U>
  static Type normalize(const U& x) {
    Type v;
    if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
    else v = static_cast<Type>(x % mod());
    if (v < 0) v += mod();
    return v;
  }

  const Type& operator()() const { return value; }
  template <typename U>
  explicit operator U() const { return static_cast<U>(value); }
  constexpr static Type mod() { return T::value; }

  Modular& operator+=(const Modular& other) { value += other.value; value -= (value >= mod()) * mod(); return *this; }
  Modular& operator-=(const Modular& other) { value -= other.value; value += (value < 0) * mod(); return *this; }
  template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
  template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
  Modular& operator++() { return *this += 1; }
  Modular& operator--() { return *this -= 1; }
  Modular operator++(int) { Modular result(*this); *this += 1; return result; }
  Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
  Modular operator-() const { return Modular(-value); }

  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
    value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
    int64_t q = int64_t(static_cast<long double>(value) * rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }

  Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }

  friend const Type& abs(const Modular& x) { return x.value; }

  template <typename U>
  friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename U>
  friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename V, typename U>
  friend V& operator>>(V& stream, Modular<U>& number);

 private:
  Type value;
};

template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }

template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }

template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }

template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }

template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }

template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }

template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }

template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1) res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}

template <typename T>
bool IsZero(const Modular<T>& number) {
  return number() == 0;
}

template <typename T>
string to_string(const Modular<T>& number) {
  return to_string(number());
}

// U == std::ostream? but done this way because of fastoutput
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
  return stream << number();
}

// U == std::istream? but done this way because of fastinput
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
  typename common_type<typename Modular<T>::Type, int64_t>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}

constexpr int md = 1e9 + 7;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;

const int N = 1012;
const int M = 100123;
int a[N], n, q;
tuple<int, int, int> Q[M];

void take_input() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    cin >> q;
    for (int i = 1; i <= q; i++) {
        auto& [l, r, x] = Q[i];
        cin >> l >> r >> x;
    }
}

void step1() {
    take_input();
}

int F[7][7][N][N];

void calculate_F() {
    for (int s = 0; s < 7; s++) 
        for (int t = 0; t < 7; t++) {
            for (int i = 1; i <= q; i++) {
                auto [l, r, x] = Q[i];
                int bit_s = (x >> s) & 1;
                int bit_t = (x >> t) & 1;
                if (bit_s && bit_t) 
                    F[s][t][l][r] += 1;
            }

            for (int x = 1; x <= n; x++)
                for (int y = n; y >= 1; --y) 
                    F[s][t][x][y] += F[s][t][x - 1][y] + F[s][t][x][y + 1] - F[s][t][x - 1][y + 1];
        }
}

void step2() {
    calculate_F();
}


Mint pow2[N + N];

void calculate_pow2() {
    pow2[0] = 1;
    for (int i = 1; i < N + N; i++) pow2[i] = pow2[i - 1] + pow2[i - 1];
}

void step3() {
    calculate_pow2();
}

Mint A[4];
int C[4];
int Span[4];

void calculate_A() {
    vector<int> v;
    v.reserve(4);
    for (int i = 0; i < 4; i++) if (C[i]) v.push_back(i);

    vector<int> basis(2);
    int basis_size = 0;
    for (int u : v) 
        for (int j = 0; j < 2; j++) if ((1 << j) & u) {
            if (!basis[j]) {
                basis[j] = u;
                basis_size += 1;
                break;
            }

            u ^= basis[j];
        }

    memset(Span, 0, sizeof(Span)); // contains span

    for (int s = 0; s < (1 << 2); s++) {
        int xoring = 0;
        for (int j = 0; j < 2; j++) if ((1 << j) & s) xoring ^= basis[j];
        Span[xoring] = 1;
    }

    int total = C[0] + C[1] + C[2] + C[3];

    for (int i = 0; i < 4; i++) A[i] = (Span[i] ? pow2[total - basis_size] : 0);
}

// Observe that G[s][x] = F[s][s][x][x]
Mint H[7][7][N][N];

int convert(int x, int y) {
    return (x << 1) | y;
}

void calculate_H() {
    
    for (int s = 0; s < 7; s++)
        for (int t = 0; t < 7; t++) 
            for (int x = 1; x <= n; x++)
                for (int y = 1; y <= n; y++) {
                    int F_val = F[s][t][min(x, y)][max(x, y)];

                    if (s == t && x == y) {
                        if ((a[x] >> s) & 1) {
                            if (F_val) H[s][t][x][y] = pow2[q - 1];
                            else H[s][t][x][y] = pow2[q];
                        } else {
                            H[s][t][x][y] = 0;
                            if (F_val) H[s][t][x][y] = pow2[q - 1];
                        }
                        continue;
                    }

                    A[0] = A[1] = A[2] = A[3] = 0;
                    C[3] = F_val;
                    C[2] = F[s][s][x][x] - C[3];
                    C[1] = F[t][t][y][y] - C[3];
                    C[0] = q - C[1] - C[2] - C[3];

                    calculate_A();
                    
                    int bx = (a[x] >> s) & 1;
                    int by = (a[y] >> t) & 1;
                    H[s][t][x][y] = A[convert(1 - bx, 1 - by)];
                }
}

void step4() {
    calculate_H();
}

Mint J[7][7][N][N];

void calculate_J() {
    for (int s = 0; s < 7; s++)
        for (int t = 0; t < 7; t++) {
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++) {
                    J[s][t][i][j] = pow2[s + t] * H[s][t][i][j];
                    J[s][t][i][j] += J[s][t][i - 1][j] + J[s][t][i][j - 1] - J[s][t][i - 1][j - 1];
                }
        }
}

void step5() {
    calculate_J();
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

    for (auto func : {step1, step2, step3, step4, step5}) {
        func();
    }

    Mint X = 0;
    for (int s = 0; s < 7; s++)
        for (int t = 0; t < 7; t++)
            for (int i = 1; i <= n; i++)
                X += J[s][t][i][i];

    Mint Y = 0;
    for (int s = 0; s < 7; s++)
        for (int t = 0; t < 7; t++)
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    Y += J[s][t][i][j];

    cout << Mint(n + 1) * X - Y << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...