Submission #1258804

#TimeUsernameProblemLanguageResultExecution timeMemory
1258804proofyIntergalactic ship (IZhO19_xorsum)C++20
100 / 100
638 ms14456 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_1() { take_input(); } Mint pow2[M + M]; void calculate_pow2() { pow2[0] = 1; for (int i = 1; i < M + M; i++) pow2[i] = pow2[i - 1] + pow2[i - 1]; } void step1_2() { calculate_pow2(); } int G[7][N]; void calculate_G() { for (int s = 0; s < 7; s++) { for (int i = 1; i <= q; i++) { auto& [l, r, x] = Q[i]; if ((x >> s) & 1) G[s][l] += 1, G[s][r + 1] -= 1; } for (int i = 1; i <= n + 1; i++) G[s][i] += G[s][i - 1]; } } void step1_3() { calculate_G(); } Mint A[1 << 4][4]; int basis[2]; void calculate_A() { for (int m = 0; m < (1 << 4); m++) { basis[0] = basis[1] = 0; int b = 0; // basis size for (int j = 0; j < 4; j++) if ((1 << j) & m) { // insert j into the basis int x = j; for (int k = 0; k < 2; k++) if ((1 << k) & x) { if (!basis[k]) { basis[k] = x; b += 1; break; } x ^= basis[k]; } } if (basis[0] == 0) basis[0] = basis[1]; for (int w = 0; w < (1 << b); w++) { int x = 0; for (int k = 0; k < b; k++) if ((1 << k) & w) x ^= basis[k]; A[m][x] = pow2[q - b]; } } } void step1_4() { calculate_A(); } int F[N][N]; void calculate_F(int s, int 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[l][r] += 1; } for (int x = 1; x <= n; x++) for (int y = n; y >= 1; --y) F[x][y] += F[x - 1][y] + F[x][y + 1] - F[x - 1][y + 1]; } void step2(int s, int t) { calculate_F(s, t); } Mint H[N][N]; int convert(int x, int y) { return (x << 1) | y; } int convert2(int x, int y, int z, int l) { l -= (x + y + z); x = !!x; y = !!y; z = !!z; l = !!l; return (x << 3) | (y << 2) | (z << 1) | l; } void calculate_H(int s, int t) { for (int x = 1; x <= n; x++) for (int y = 1; y <= n; y++) { int F_val = F[min(x, y)][max(x, y)]; if (s == t && x == y) { if ((a[x] >> s) & 1) { if (F_val) H[x][y] = pow2[q - 1]; else H[x][y] = pow2[q]; } else { H[x][y] = 0; if (F_val) H[x][y] = pow2[q - 1]; } continue; } int m = convert2(F_val, (G[s][x] - F_val), (G[t][y] - F_val), q); int bx = (a[x] >> s) & 1; int by = (a[y] >> t) & 1; H[x][y] = A[m][convert(1 - bx, 1 - by)]; } } void step3(int s, int t) { calculate_H(s, t); } Mint J[N][N]; void calculate_J(int s, int t) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { J[i][j] = pow2[s + t] * H[i][j]; J[i][j] += J[i - 1][j] + J[i][j - 1] - J[i - 1][j - 1]; } } void step4(int s, int t) { calculate_J(s, t); } int main() { ios::sync_with_stdio(0); cin.tie(0); for (auto func : {step1_1, step1_2, step1_3, step1_4}) { func(); } Mint X = 0; Mint Y = 0; for (int s = 0; s < 7; s++) for (int t = 0; t < 7; t++) { memset(F, 0, sizeof(F)); memset(H, 0, sizeof(H)); memset(J, 0, sizeof(J)); for (auto func : {step2, step3, step4}) { func(s, t); } for (int i = 1; i <= n; i++) { X += J[i][i]; for (int j = 1; j <= n; j++) Y += J[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...