QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360200 | #6549. Two Missing Numbers | ucup-team3099 | 0 | 2ms | 3940kb | C++23 | 28.2kb | 2024-03-21 14:55:11 | 2024-03-21 14:55:11 |
answer
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif
// Unique finite field of size 2^D
template<size_t D, class T, class T_large>
struct finite_field_char2_base{
#define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
#define IS_UNSIGNED(T) (is_unsigned_v<T> || is_same_v<T, __uint128_t>)
static_assert(IS_UNSIGNED(T) && IS_UNSIGNED(T_large));
static_assert(1 <= D && D <= sizeof(T) * 8);
static_assert(2 * D <= sizeof(T_large) * 8);
static constexpr size_t characteristic = 2;
static constexpr size_t dimension = D;
static constexpr T_large size = T_large(1) << D;
static constexpr unsigned int irreducible[] = {0, 0, 3, 3, 3, 5, 3, 3, 27, 3, 9, 5, 9, 27, 33, 3, 43, 9, 9, 39, 9, 5, 3, 33, 27, 9, 27, 39, 3, 5, 3, 9, 141, 75, 27, 5, 53, 63, 99, 17, 57, 9, 39, 89, 33, 27, 3, 33, 45, 113, 29, 75, 9, 71, 125, 71, 149, 17, 99, 123, 3, 39, 105, 3, 27, 27, 9, 39, 163, 101, 43};
static constexpr T _full_mask = size - 1;
static finite_field_char2_base _primitive_root;
static finite_field_char2_base primitive_root(){
if(_primitive_root) return _primitive_root;
if(D == 1) return _primitive_root = 1;
T divs[20] = {};
int cnt = 0;
T x = _full_mask;
for(auto i = 3; 1LL * i * i <= x; i += 2) if(x % i == 0){
divs[cnt ++] = i;
while(x % i == 0) x /= i;
}
if(x > 1) divs[cnt ++] = x;
for(auto g = 2; ; ++ g){
bool ok = true;
for(auto i = 0; i < cnt; ++ i) if(finite_field_char2_base{g}.power(_full_mask / divs[i]) == 1){
ok = false;
break;
}
if(ok) return _primitive_root = g;
}
}
static finite_field_char2_base generate(auto &&rng){
T res = 0;
for(auto rem = D; rem; ){
auto w = min<size_t>(32, rem);
res = res << w | rng() & _full_mask;
rem -= w;
}
return res;
}
finite_field_char2_base(){ }
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
finite_field_char2_base(U x): data(x){ }
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr> operator U() const{ return data; }
finite_field_char2_base &operator+=(const finite_field_char2_base &otr){ data ^= otr.data; return *this; }
finite_field_char2_base &operator-=(const finite_field_char2_base &otr){ data ^= otr.data; return *this; }
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr> finite_field_char2_base &operator+=(const U &otr){ return *this += finite_field_char2_base(otr); }
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr> finite_field_char2_base &operator-=(const U &otr){ return *this -= finite_field_char2_base(otr); }
finite_field_char2_base &operator++(){ data ^= 1; return *this; }
finite_field_char2_base &operator--(){ data ^= 1; return *this; }
finite_field_char2_base operator-() const{ return *this; }
finite_field_char2_base &operator*=(const finite_field_char2_base &rhs){
T_large res = 0;
for(auto i = 0; i < D; ++ i) if(rhs.data >> i & 1) res ^= (T_large)data << i;
for(auto i = 2 * D - 1; i >= D; -- i) if(res >> i & 1) res ^= (T_large)irreducible[D] << i - D;
data = res & _full_mask;
return *this;
}
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
finite_field_char2_base &inplace_power(U e){
if(e == 0) return *this = 1;
if(data == 0) return *this = {};
if(data == 1 || e == 1) return *this;
e %= _full_mask;
if(e < 0) e += _full_mask;
finite_field_char2_base res = 1;
for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this;
return *this = res;
}
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
finite_field_char2_base power(U e) const{
return finite_field_char2_base(*this).inplace_power(e);
}
finite_field_char2_base &operator/=(const finite_field_char2_base &otr){
assert(otr);
return *this *= otr.power(_full_mask - 1);
}
#define ARITHMETIC_OP(op, apply_op)\
finite_field_char2_base operator op(const finite_field_char2_base &x) const{ return finite_field_char2_base(*this) apply_op x; }\
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>\
finite_field_char2_base operator op(const U &x) const{ return finite_field_char2_base(*this) apply_op finite_field_char2_base(x); }\
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>\
friend finite_field_char2_base operator op(const U &x, const finite_field_char2_base &y){ return finite_field_char2_base(x) apply_op y; }
ARITHMETIC_OP(+, +=) ARITHMETIC_OP(-, -=) ARITHMETIC_OP(*, *=) ARITHMETIC_OP(/, /=)
#undef ARITHMETIC_OP
#define COMPARE_OP(op)\
bool operator op(const finite_field_char2_base &x) const{ return data op x.data; }\
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>\
bool operator op(const U &x) const{ return data op finite_field_char2_base(x).data; }\
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>\
friend bool operator op(const U &x, const finite_field_char2_base &y){ return finite_field_char2_base(x).data op y.data; }
COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=)
#undef COMPARE_OP
friend istream &operator>>(istream &in, finite_field_char2_base &number){
return in >> number.data;
}
friend ostream &operator<<(ostream &out, const finite_field_char2_base &x){
return out << x.data;
}
T data = 0;
#undef IS_INTEGRAL
#undef IS_UNSIGNED
};
template<size_t D>
using FF2 = finite_field_char2_base<D, unsigned int, unsigned long long>;
template<size_t D>
using FF2Large = finite_field_char2_base<D, unsigned long long, __uint128_t>;
template<class T, class multiplication_functor>
struct power_series_naive_base: vector<T>{
#define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
#define data (*this)
template<class ...Args>
power_series_naive_base(Args... args): vector<T>(args...){}
power_series_naive_base(initializer_list<T> init): vector<T>(init){}
operator bool() const{
return find_if(data.begin(), data.end(), [&](const T &x){ return x != T{0}; }) != data.end();
}
// Returns \sum_{i=0}^{n-1} a_i/i! * X^i
static power_series_naive_base EGF(vector<T> a){
int n = (int)a.size();
T fact = 1;
for(auto x = 2; x < n; ++ x) fact *= x;
fact = 1 / fact;
for(auto i = n - 1; i >= 0; -- i) a[i] *= fact, fact *= i;
return power_series_naive_base(a);
}
// Returns exp(coef * X).take(n) = \sum_{i=0}^{n-1} coef^i/i! * X^i
static power_series_naive_base EGF(int n, T coef = 1){
vector<T> a(n, 1);
for(auto i = 1; i < n; ++ i) a[i] = a[i - 1] * coef;
return EGF(a);
}
vector<T> EGF_to_seq() const{
int n = (int)data.size();
vector<T> seq(n);
T fact = 1;
for(auto i = 0; i < n; ++ i){
seq[i] = data[i] * fact;
fact *= i + 1;
}
return seq;
}
power_series_naive_base &inplace_reduce(){
while(!data.empty() && !data.back()) data.pop_back();
return *this;
}
power_series_naive_base reduce() const{
return power_series_naive_base(*this).inplace_reduce();
}
friend ostream &operator<<(ostream &out, const power_series_naive_base &p){
if(p.empty()){
return out << "{}";
}
else{
out << "{";
for(auto i = 0; i < (int)p.size(); ++ i){
out << p[i];
i + 1 < (int)p.size() ? out << ", " : out << "}";
}
return out;
}
}
power_series_naive_base &inplace_take(int n){
data.erase(data.begin() + min((int)data.size(), n), data.end());
data.resize(n, T{0});
return *this;
}
power_series_naive_base take(int n) const{
auto res = vector<T>(data.begin(), data.begin() + min((int)data.size(), n));
res.resize(n, T{0});
return res;
}
power_series_naive_base &inplace_drop(int n){
data.erase(data.begin(), data.begin() + min((int)data.size(), n));
return *this;
}
power_series_naive_base drop(int n) const{
return vector<T>(data.begin() + min((int)data.size(), n), data.end());
}
power_series_naive_base &inplace_slice(int l, int r){
assert(0 <= l && l <= r);
data.erase(data.begin(), data.begin() + min((int)data.size(), l));
data.resize(r - l, T{0});
return *this;
}
power_series_naive_base slice(int l, int r) const{
auto res = vector<T>(data.begin() + min((int)data.size(), l), data.begin() + min((int)data.size(), r));
res.resize(r - l, T{0});
return res;
}
power_series_naive_base &inplace_reverse(int n){
data.resize(max(n, (int)data.size()), T{0});
std::reverse(data.begin(), data.begin() + n);
return *this;
}
power_series_naive_base reverse(int n) const{
return power_series_naive_base(*this).inplace_reverse(n);
}
power_series_naive_base &inplace_shift(int n, T x = T{0}){
data.insert(data.begin(), n, x);
return *this;
}
power_series_naive_base shift(int n, T x = T{0}) const{
return power_series_naive_base(*this).inplace_shift(n, x);
}
T evaluate(T x) const{
T res = {};
for(auto i = (int)data.size() - 1; i >= 0; -- i) res = res * x + data[i];
return res;
}
// Takes mod x^n-1
power_series_naive_base &inplace_circularize(int n){
assert(n >= 1);
for(auto i = n; i < (int)data.size(); ++ i) data[i % n] += data[i];
data.resize(n, T{0});
return *this;
}
// Takes mod x^n-1
power_series_naive_base circularize(int n) const{
return power_series_naive_base(*this).inplace_circularize(n);
}
power_series_naive_base operator*(const power_series_naive_base &p) const{
return multiplication_functor::multiply(data, p);
}
power_series_naive_base &operator*=(const power_series_naive_base &p){
return *this = *this * p;
}
template<class U>
power_series_naive_base &operator*=(U x){
for(auto &c: data) c *= x;
return *this;
}
template<class U>
power_series_naive_base operator*(U x) const{
return power_series_naive_base(*this) *= x;
}
template<class U>
friend power_series_naive_base operator*(U x, power_series_naive_base p){
for(auto &c: p) c = x * c;
return p;
}
// Compute p^e mod x^n - 1.
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
power_series_naive_base &inplace_power_circular(U e, int n){
assert(n >= 1);
power_series_naive_base p = *this;
data.assign(n, 0);
data[0] = 1;
for(; e; e >>= 1){
if(e & 1) (*this *= p).inplace_circularize(n);
(p *= p).inplace_circularize(n);
}
return *this;
}
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
power_series_naive_base power_circular(U e, int len) const{
return power_series_naive_base(*this).inplace_power_circular(e, len);
}
power_series_naive_base &operator+=(const power_series_naive_base &p){
data.resize(max(data.size(), p.size()), T{0});
for(auto i = 0; i < (int)p.size(); ++ i) data[i] += p[i];
return *this;
}
power_series_naive_base operator+(const power_series_naive_base &p) const{
return power_series_naive_base(*this) += p;
}
template<class U>
power_series_naive_base &operator+=(const U &x){
if(data.empty()) data.emplace_back();
data[0] += x;
return *this;
}
template<class U>
power_series_naive_base operator+(const U &x) const{
return power_series_naive_base(*this) += x;
}
template<class U>
friend power_series_naive_base operator+(const U &x, const power_series_naive_base &p){
return p + x;
}
power_series_naive_base &operator-=(const power_series_naive_base &p){
data.resize(max(data.size(), p.size()), T{0});
for(auto i = 0; i < (int)p.size(); ++ i) data[i] -= p[i];
return *this;
}
power_series_naive_base operator-(const power_series_naive_base &p) const{
return power_series_naive_base(*this) -= p;
}
template<class U>
power_series_naive_base &operator-=(const U &x){
if(data.empty()) data.emplace_back();
data[0] -= x;
return *this;
}
template<class U>
power_series_naive_base operator-(const U &x) const{
return power_series_naive_base(*this) -= x;
}
template<class U>
friend power_series_naive_base operator-(const U &x, const power_series_naive_base &p){
return -p + x;
}
power_series_naive_base operator-() const{
power_series_naive_base res = *this;
for(auto i = 0; i < data.size(); ++ i) res[i] = T{} - res[i];
return res;
}
power_series_naive_base &operator++(){
if(data.empty()) data.push_back(1);
else ++ data[0];
return *this;
}
power_series_naive_base &operator--(){
if(data.empty()) data.push_back(-1);
else -- data[0];
return *this;
}
power_series_naive_base operator++(int){
power_series_naive_base result(*this);
if(data.empty()) data.push_back(1);
else ++ data[0];
return result;
}
power_series_naive_base operator--(int){
power_series_naive_base result(*this);
if(data.empty()) data.push_back(-1);
else -- data[0];
return result;
}
power_series_naive_base &inplace_clear_range(int l, int r){
assert(0 <= l && l <= r);
for(auto i = l; i < min(r, (int)data.size()); ++ i) data[i] = T{0};
return *this;
}
power_series_naive_base clear_range(int l, int r) const{
return power_series_naive_base(*this).inplace_clear_range(l, r);
}
power_series_naive_base &inplace_dot_product(const power_series_naive_base &p){
for(auto i = 0; i < min(data.size(), p.size()); ++ i) data[i] *= p[i];
return *this;
}
power_series_naive_base dot_product(const power_series_naive_base &p) const{
return power_series_naive_base(*this).inplace_power_series_product(p);
}
power_series_naive_base inverse(int n) const{
assert(!data.empty() && data[0]);
auto inv = 1 / data[0];
power_series_naive_base res{inv};
for(auto s = 1; s < n; s <<= 1) (res *= (2 - res * take(s << 1))).inplace_take(s << 1);
return res.inplace_take(n);
}
power_series_naive_base &inplace_inverse(int n){
return *this = this->inverse(n);
}
power_series_naive_base &inplace_power_series_division(power_series_naive_base p, int n){
int i = 0;
while(i < min(data.size(), p.size()) && !data[i] && !p[i]) ++ i;
data.erase(data.begin(), data.begin() + i);
p.erase(p.begin(), p.begin() + i);
(*this *= p.inverse(n)).resize(n, T{0});
return *this;
}
power_series_naive_base power_series_division(const power_series_naive_base &p, int n){
return power_series_naive_base(*this).inplace_power_series_division(p, n);
}
// Euclidean division
// O(min(n * log(n), # of non-zero indices))
power_series_naive_base &operator/=(const power_series_naive_base &p){
int n = (int)p.size();
while(n && p[n - 1] == T{0}) -- n;
assert(n >= 1);
inplace_reduce();
if(data.size() < n){
data.clear();
return *this;
}
if(n - count(p.begin(), p.begin() + n, T{0}) <= 100){
T inv = 1 / p[n - 1];
static vector<int> indices;
for(auto i = 0; i < n - 1; ++ i) if(p[i]) indices.push_back(i);
power_series_naive_base res((int)data.size() - n + 1);
for(auto i = (int)data.size() - 1; i >= n - 1; -- i) if(data[i]){
T x = data[i] * inv;
res[i - n + 1] = x;
for(auto j: indices) data[i - (n - 1 - j)] -= x * p[j];
}
indices.clear();
return *this = res;
}
power_series_naive_base b;
n = data.size() - p.size() + 1;
b.assign(n, {});
copy(p.rbegin(), p.rbegin() + min(p.size(), b.size()), b.begin());
std::reverse(data.begin(), data.end());
data *= b.inverse(n);
data.erase(data.begin() + n, data.end());
std::reverse(data.begin(), data.end());
return *this;
}
power_series_naive_base operator/(const power_series_naive_base &p) const{
return power_series_naive_base(*this) /= p;
}
template<class U>
power_series_naive_base &operator/=(U x){
assert(x);
T inv_x = T(1) / x;
for(auto &c: data) c *= inv_x;
return *this;
}
template<class U>
power_series_naive_base operator/(U x) const{
return power_series_naive_base(*this) /= x;
}
pair<power_series_naive_base, power_series_naive_base> divrem(const power_series_naive_base &p) const{
auto q = *this / p, r = *this - q * p;
while(!r.empty() && r.back() == 0) r.pop_back();
return {q, r};
}
power_series_naive_base &operator%=(const power_series_naive_base &p){
int n = (int)p.size();
while(n && p[n - 1] == T{0}) -- n;
assert(n >= 1);
inplace_reduce();
if(data.size() < n) return *this;
if(n - count(p.begin(), p.begin() + n, 0) <= 100){
T inv = 1 / p[n - 1];
static vector<int> indices;
for(auto i = 0; i < n - 1; ++ i) if(p[i]) indices.push_back(i);
for(auto i = (int)data.size() - 1; i >= n - 1; -- i) if(data[i]){
T x = data[i] * inv;
data[i] = 0;
for(auto j: indices) data[i - (n - 1 - j)] -= x * p[j];
}
indices.clear();
return inplace_reduce();
}
return *this = this->divrem(p).second;
}
power_series_naive_base operator%(const power_series_naive_base &p) const{
return power_series_naive_base(*this) %= p;
}
power_series_naive_base &inplace_derivative(){
if(!data.empty()){
for(auto i = 0; i < data.size(); ++ i) data[i] *= i;
data.erase(data.begin());
}
return *this;
}
// p'
power_series_naive_base derivative() const{
return power_series_naive_base(*this).inplace_derivative();
}
power_series_naive_base &inplace_derivative_shift(){
for(auto i = 0; i < data.size(); ++ i) data[i] *= i;
return *this;
}
// xP'
power_series_naive_base derivative_shift() const{
return power_series_naive_base(*this).inplace_derivative_shift();
}
power_series_naive_base &inplace_antiderivative(){
T::precalc_inverse(data.size());
data.push_back(0);
for(auto i = (int)data.size() - 1; i >= 1; -- i) data[i] = data[i - 1] / i;
data[0] = 0;
return *this;
}
// Integral(P)
power_series_naive_base antiderivative() const{
return power_series_naive_base(*this).inplace_antiderivative();
}
power_series_naive_base &inplace_shifted_antiderivative(){
T::precalc_inverse(data.size());
if(!data.empty()) data[0] = 0;
for(auto i = 1; i < data.size(); ++ i) data[i] /= i;
return *this;
}
// Integral(P/x)
power_series_naive_base shifted_antiderivative() const{
return power_series_naive_base(*this).inplace_shifted_antiderivative();
}
power_series_naive_base &inplace_log(int n){
assert(!data.empty() && data[0] == 1);
if(!n){
data.clear();
return *this;
}
(*this = derivative() * inverse(n)).resize(n - 1, T{0});
inplace_antiderivative();
return *this;
}
power_series_naive_base log(int n) const{
return power_series_naive_base(*this).inplace_log(n);
}
power_series_naive_base exp(int n) const{
assert(data.empty() || data[0] == T{0});
power_series_naive_base f{1}, g;
for(auto s = 1; s < n; s <<= 1){
g = f.log(s << 1).drop(s) - drop(s).take(s);
(f -= (f * g).take(s).shift(s)).inplace_take(s << 1);
}
return f.take(n);
}
power_series_naive_base &inplace_exp(int n){
return *this = this->exp(n);
}
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
power_series_naive_base &inplace_power(U e, int n){
assert(n >= 0);
data.resize(n, T{0});
if(n == 0) return *this;
if(e == 0){
fill(data.begin(), data.end(), T{0});
data[0] = T{1};
return *this;
}
if(e < 0) return inplace_inverse(n).inplace_power(-e, n);
if(all_of(data.begin(), data.end(), [&](auto x){ return x == T{0}; })) return *this;
int pivot = find_if(data.begin(), data.end(), [&](auto x){ return x; }) - data.begin();
if(pivot && e >= (n + pivot - 1) / pivot){
fill(data.begin(), data.end(), T{0});
return *this;
}
data.erase(data.begin(), data.begin() + pivot);
n -= pivot * e;
T pivot_c = T{1}, base = data[0];
for(auto x = e; x; x >>= 1, base *= base) if(x & 1) pivot_c *= base;
((*this /= data[0]).inplace_log(n) *= e).inplace_exp(n);
data.insert(data.begin(), pivot * e, T{0});
return *this *= pivot_c;
}
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
power_series_naive_base power(U e, int n) const{
return power_series_naive_base(*this).inplace_power(e, n);
}
// O(n * log(n) * log(e))
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
power_series_naive_base &inplace_power_mod(U e, power_series_naive_base mod){
mod.inplace_reduce();
assert(mod);
if((int)mod.size() == 1){
data.clear();
return *this;
}
if(e == 0){
data = {T{1}};
return *this;
}
if(e < 0) return inplace_inverse((int)mod.size()).inplace_power_mod(-e, mod);
if(!*this) return *this;
power_series_naive_base res{1};
for(; e; e >>= 1, *this = *this * *this % mod) if(e & 1) res = res * *this % mod;
return *this = res;
}
// O(n * log(n) * log(e))
template<class U, typename enable_if<IS_INTEGRAL(U)>::type* = nullptr>
power_series_naive_base power_mod(U e, const power_series_naive_base &mod) const{
return power_series_naive_base(*this).inplace_power_mod(e, mod);
}
// Suppose there are data[i] distinct objects with weight i.
// Returns a power series where i-th coefficient represents # of ways to select a set of objects with sum of weight i.
// O(n * log(n))
power_series_naive_base &inplace_set(int n){
assert(!data.empty() && data[0] == T{0});
data.resize(n);
for(auto i = n - 1; i >= 1; -- i) for(auto j = 2 * i; j < n; j += i) data[j] += data[i];
for(auto i = 1; i < n; ++ i) (data[i] /= i) *= (i & 1 ? 1 : -1);
return inplace_exp(n);
}
power_series_naive_base set(int n) const{
return power_series_naive_base(*this).inplace_set(n);
}
// Suppose there are data[i] distinct objects with weight i.
// Returns a power series where i-th coefficient represents # of ways to select a multiset of objects with sum of weight i.
// O(n * log(n))
power_series_naive_base &inplace_multiset(int n){
assert(!data.empty() && data[0] == T{0});
data.resize(n);
static vector<T> inv;
inv.resize(n);
for(auto i = 1; i < n; ++ i) inv[i] = T{1} / i;
for(auto i = n - 1; i >= 1; -- i) for(auto j = 2 * i; j < n; j += i) data[j] += data[i] * inv[j / i];
inv.clear(), inv.shrink_to_fit();
return inplace_exp(n);
}
power_series_naive_base multiset(int n) const{
return power_series_naive_base(*this).inplace_multiset(n);
}
static power_series_naive_base multiply_all(const vector<power_series_naive_base> &a){
if(a.empty()) return {1};
auto solve = [&](auto self, int l, int r)->power_series_naive_base{
if(r - l == 1) return a[l];
int m = l + (r - l >> 1);
return self(self, l, m) * self(self, m, r);
};
return solve(solve, 0, (int)a.size());
}
friend power_series_naive_base gcd(power_series_naive_base p, power_series_naive_base q){
p.inplace_reduce(), q.inplace_reduce();
while(q) p = exchange(q, (p % q).reduce());
return p;
}
friend power_series_naive_base lcm(power_series_naive_base p, power_series_naive_base q){
return p / gcd(p, q) * q;
}
#undef IS_INTEGRAL
#undef data
};
template<class T>
struct _quadratic{
static vector<T> multiply(const vector<T> &a, const vector<T> &b){
if(a.empty() || b.empty()) return {};
vector<T> q((int)a.size() + (int)b.size() - 1);
for(auto i = 0; i < (int)a.size(); ++ i) for(auto j = 0; j < (int)b.size(); ++ j) q[i + j] += a[i] * b[j];
return q;
}
};
template<class T, class FFT>
struct _with_fft{
static vector<T> multiply_naively(const vector<T> &a, const vector<T> &b){
vector<T> q((int)a.size() + (int)b.size() - 1);
for(auto i = 0; i < (int)a.size(); ++ i) for(auto j = 0; j < (int)b.size(); ++ j) q[i + j] += a[i] * b[j];
return q;
}
static vector<T> multiply(const vector<T> &a, const vector<T> &b){
if(a.empty() || b.empty()) return {};
if(min(a.size(), b.size()) <= 60) return multiply_naively(a, b);
return FFT::arbitrarily_convolute(a, b);
}
};
template<class T> using power_series = power_series_naive_base<T, _quadratic<T>>;
// using power_series = power_series_naive_base<modular, _with_fft<modular, ntt>>;
// Requires finite_field and power_series_naive
template<class FF>
struct factorizer_over_finite_field{
using P = power_series_naive_base<FF, _quadratic<FF>>;
static void _reduce_and_monicify(P &p){
p.inplace_reduce();
assert(p);
FF x = p.back();
if(x != FF{1}){
x = 1 / x;
for(auto &y: p) y *= x;
}
}
// Given p,
// find factorization p = \prod{f^e} where f is square-free
static vector<pair<P, int>> factorize_square_free(P p){
_reduce_and_monicify(p);
if((int)p.size() == 1) return {};
if(!p.derivative()){
assert(((int)p.size() - 1) % FF::characteristic == 0);
P q(((int)p.size() - 1) / FF::characteristic + 1);
for(auto i = 0; i < (int)q.size(); ++ i) q[i] = p[FF::characteristic * i];
auto fact = factorize_square_free(q);
for(auto &[_, e]: fact) e *= FF::characteristic;
return fact;
}
P g = gcd(p, p.derivative());
_reduce_and_monicify(g);
if((int)g.size() == 1) return {{p, 1}};
auto fact_left = factorize_square_free(g);
auto fact_right = factorize_square_free(p / g);
fact_left.insert(fact_left.end(), fact_right.begin(), fact_right.end());
return fact_left;
}
// Given square-free p,
// find factorization p = \prod{f} where f is a product of irreducible polynomials of equal degree d
static vector<pair<P, int>> factorize_distinct_degree(P p){
_reduce_and_monicify(p);
vector<pair<P, int>> res;
for(auto i = 1; 2 * i <= (int)p.size() - 1; ++ i){
P q{0, 1};
for(auto j = 1; j <= i; ++ j) q = q.power_mod(FF::size, p);
q -= P{0, 1};
P g = gcd(p, q);
_reduce_and_monicify(g);
if((int)g.size() != 1){
res.push_back({g, i});
(p /= g).inplace_reduce();
}
}
if((int)p.size() >= 2) res.push_back({p, (int)p.size() - 1});
return res;
}
// Given square-free p which is a product of irreducible polynomials of equal degree d,
// find factorization of it into irreducible polynomials
static vector<P> factorize_equal_degree(int d, P p){
assert(d >= 1);
_reduce_and_monicify(p);
assert(((int)p.size() - 1) % d == 0);
if((int)p.size() == 1) return {};
int obj = ((int)p.size() - 1) / d;
vector<P> res{p}, res_next;
for(mt19937 rng(1564); (int)res.size() < obj; ){
P q((int)p.size());
for(auto &x: q) x = FF::generate(rng);
P g = gcd(p, q);
_reduce_and_monicify(g);
if((int)g.size() == 1){
if(FF::characteristic >= 3){
P base = q.power_mod((FF::size - 1) / 2, p);
g = base;
for(auto i = 0; i < d - 1; ++ i) (g = g.power_mod(FF::size, p) * base % p).inplace_reduce();
++ g;
}
else{
if(FF::dimension % 2 == 0){
P base = q.power_mod((FF::size - 1) / 3, p);
g = base;
for(auto i = 0; i < d - 1; ++ i) (g = g.power_mod(FF::size, p) * base % p).inplace_reduce();
++ g;
}
else{
g = q;
for(auto i = 0; i < d - 1; ++ i) g = q + g.power_mod(FF::size, p);
(g %= p).inplace_reduce();
}
}
}
_reduce_and_monicify(g);
res_next.clear();
for(auto p: res){
p.inplace_reduce();
if((int)p.size() == d + 1){
res_next.push_back(p);
continue;
}
P h = gcd(g, p);
_reduce_and_monicify(h);
if((int)h.size() == 1 || (int)h.size() == (int)p.size()){
res_next.push_back(p);
continue;
}
res_next.insert(res_next.end(), {h, p / h});
}
swap(res, res_next);
}
return res;
}
static vector<pair<P, int>> factorize(P p){
_reduce_and_monicify(p);
vector<pair<P, int>> res;
auto fact_square_free = factorize_square_free(p);
for(auto [f0, e]: fact_square_free){
auto fact_distinct_degree = factorize_distinct_degree(f0);
for(auto &[f1, d]: fact_distinct_degree){
auto fact_equal_degree = factorize_equal_degree(d, f1);
for(auto &f2: fact_equal_degree) res.push_back({f2, e});
}
}
return res;
}
};
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
using FF = FF2Large<64>;
int run_number, n;
cin >> run_number >> n;
if(run_number == 1){
FF sum = 0, cubesum = 0;
for(auto i = 0; i < n; ++ i){
FF x;
cin >> x;
sum += x;
cubesum += x * x * x;
}
cout << sum << " " << cubesum << "\n";
}
else{
FF sum, cubesum;
cin >> sum >> cubesum;
for(auto i = 0; i < n; ++ i){
FF x;
cin >> x;
sum += x;
cubesum += x * x * x;
}
FF product = cubesum / sum - sum * sum;
power_series<FF> p{product, sum, 1};
auto res = factorizer_over_finite_field<FF>::factorize(p);
for(auto [f, e]: res){
cout << f[0] << " ";
cout.flush();
assert(e == 1);
}
cout << "\n";
}
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3940kb
First Run Input
1 5 5 1 4 4 5
First Run Output
1 1
Second Run Input
2 3 1 1 9 9 3
Second Run Output
1 3
result:
ok correct
Test #2:
score: 100
Accepted
time: 2ms
memory: 3788kb
First Run Input
1 1 0
First Run Output
0 0
Second Run Input
2 1 0 0 1
Second Run Output
0 1
result:
ok correct
Test #3:
score: 0
Stage 2: Program answer Runtime Error
First Run Input
1 1 10625130587464985929 1167154569617655189
First Run Output
10625130587464985929 12511876466917322003
Second Run Input
2 1 10625130587464985929 12511876466917322003 1167154569617655189