QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#110513 | #6542. Optimal Quadratic Function | hjroh0315 | TL | 6377ms | 262648kb | C++20 | 26.8kb | 2023-06-02 18:07:37 | 2023-06-02 18:07:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define assert_msg(x,y) assert((x && y))
class Rng{
private:
static std::mt19937 engine;
public:
static std::mt19937& get_engine(){
return engine;
}
template<typename T>
static void set_seed(T const& seed){
engine = std::mt19937(seed);
}
static void timebased_seed(){
engine = std::mt19937(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
}
template<typename T>
static typename std::enable_if<std::is_integral<T>::value, T>::type uniform(T l, T r){
return std::uniform_int_distribution<T>(l, r)(engine);
}
template<typename T>
static typename std::enable_if<std::is_floating_point<T>::value, T>::type uniform(T l, T r){
return std::uniform_real_distribution<T>(l, r)(engine);
}
};
std::mt19937 Rng::engine(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
class Timer{
public:
template<typename S, typename T, typename... U>
static S execute_timed(T func, std::string const& name, U&&... u){
auto time_begin = std::chrono::high_resolution_clock::now();
S ret = func(std::forward<U>(u)...);
auto time_end = std::chrono::high_resolution_clock::now();
auto timespan = std::chrono::duration_cast<std::chrono::nanoseconds>(time_end - time_begin);
std::cerr << "Execution of '" << name << "' took " << std::fixed << std::setprecision(6) << timespan.count()*1e-9 << "\n";
return ret;
}
};
/**
* signed Bigint in base 10^18, used for Input / Output
* don't use for computations, doesn't support most operations
*
*/
class Bigint_base10{
public:
static constexpr int64_t BASE = 1e18;
static constexpr int DIGITS = 18;
private:
bool is_neg;
vector<int64_t> data;
public:
Bigint_base10():is_neg(false), data(1, 0){}
Bigint_base10(int64_t const&val):is_neg(val<0){
int64_t abs_val = abs(val);
if(abs_val < BASE){
data = {abs_val};
} else {
data = {abs_val%BASE, abs_val/BASE};
}
}
Bigint_base10 operator+(Bigint_base10 const&o)const{
assert_msg(is_neg == o.is_neg, "Addition operands need to have equal sign");
Bigint_base10 ret;
ret.is_neg = is_neg;
ret.data.assign(1+max(data.size(), o.data.size()), 0);
copy(data.begin(), data.end(), ret.data.begin());
int64_t carry = 0;
for(unsigned int i=0;i<o.data.size();++i){
ret.data[i]+=o.data[i] + carry;
carry = 0;
if(ret.data[i] >= BASE){
carry = 1;
ret.data[i]-=BASE;
}
}
for(unsigned int i=o.data.size();carry;++i){
ret.data[i]+=carry;
carry = 0;
if(ret.data[i] >= BASE){
carry = 1;
ret.data[i]-=BASE;
}
}
return ret.trim();
}
Bigint_base10 operator*(int64_t const&o)const{
if(o == 0){
return Bigint_base10(0);
}
if(o<0){
return operator*(-o).negate();
}
if(o&1){
return operator+(operator*(o-1));
}
return operator+(*this)*(o/2);
}
Bigint_base10& operator+=(Bigint_base10 const&o){
*this = operator+(o);
return *this;
}
Bigint_base10& operator*=(int64_t const&o){
*this = operator*(o);
return *this;
}
Bigint_base10& trim(){
while(data.size()>1 && data.back() == 0){
data.pop_back();
}
return *this;
}
bool is_zero()const{
for(auto const&e:data) if(e) return false;
return true;
}
Bigint_base10& negate(){
is_neg = !is_neg;
if(is_zero()) is_neg = false;
return *this;
}
friend ostream& operator<<(ostream&o, Bigint_base10 const&b){
if(b.is_neg) o << '-';
o << b.data.back();
o << setfill('0');
for(auto it = next(b.data.rbegin());it != b.data.rend();++it){
o << setw(9) << *it;
}
o << setw(0) << setfill(' ');
return o;
}
friend istream& operator>>(istream&in, Bigint_base10 &b){
static string tmp;
in >> tmp;
assert_msg(in, "input should be readable as a string");
if(tmp[0] == '-'){
b.is_neg = true;
tmp = tmp.substr(1, -1);
} else {
b.is_neg = false;
}
assert_msg(all_of(tmp.begin(), tmp.end(), [](char const&c){return '0'<=c && c<='9';}), "Input should consist of digits and possibly a '-'");
assert_msg(!tmp.empty(), "Input should contain at least one digit");
b.data.resize((tmp.size()+DIGITS-1)/DIGITS);
unsigned int i, j;
for(i=tmp.size()-DIGITS, j=0;i>0;i-=DIGITS, ++j){
b.data[j] = stoll(tmp.substr(i, DIGITS));
}
b.data[j] = stoll(tmp.substr(0, i+DIGITS));
return in;
}
};
/**
* Biginteger with fixed precision
* Has 31*len bits, first bit is sign
*
* Is quite fast
*/
template<size_t len>
struct Bigint_Fixedsize{
unsigned int data[len];
static constexpr unsigned int bits = 31;
Bigint_Fixedsize(){memset(data, 0, sizeof(data));}
Bigint_Fixedsize(long long const&_a){
memset(data, 0, sizeof(data));
unsigned long long a = _a;
data[0] = a&((1u<<bits)-1);
data[1] = a>>bits;
data[1]&=~(1u<<bits);
if(a>~a){ // negative number, use complement
for(size_t i=2;i<len;++i){
data[i] = (1u<<bits)-1;
}
}
}
//__attribute__((optimize("unroll-loops")))
Bigint_Fixedsize& operator+=(Bigint_Fixedsize const&o){
unsigned int carry = 0;
for(size_t i=0;i<len;++i){
data[i]+=o.data[i] + carry;
carry = data[i]>>bits;
data[i]&=~(1u<<bits);
}
return *this;
}
//__attribute__((optimize("unroll-loops")))
Bigint_Fixedsize& operator-=(Bigint_Fixedsize const&o){
unsigned int carry = 0;
for(size_t i=0;i<len;++i){
data[i]-=o.data[i] + carry;
carry = data[i]>>bits;
data[i]&=~(1u<<bits);
}
return *this;
}
Bigint_Fixedsize operator+(Bigint_Fixedsize const&o)const{
Bigint_Fixedsize ret(*this);
ret+=o;
return ret;
}
Bigint_Fixedsize operator-(Bigint_Fixedsize const&o)const{
Bigint_Fixedsize ret(*this);
ret-=o;
return ret;
}
//__attribute__((optimize("unroll-loops")))
void multo(Bigint_Fixedsize const&o, Bigint_Fixedsize &ret)const{
static unsigned int tmp[len+1];
memset(tmp, 0, sizeof(tmp));
for(size_t i=0;i<len;++i){
unsigned long long val = 0;
for(size_t j=0;j<len-i;++j){
val+= data[i]*(unsigned long long)o.data[j]+tmp[i+j];
tmp[i+j] = val&((1u<<bits)-1);
val>>=bits;
}
}
memcpy(ret.data, tmp, sizeof(ret.data));
}
Bigint_Fixedsize& operator*=(Bigint_Fixedsize const&o){
multo(o, *this);
return *this;
}
Bigint_Fixedsize operator*(Bigint_Fixedsize const&o)const{
Bigint_Fixedsize ret;
multo(o, ret);
return ret;
}
Bigint_Fixedsize& negate(){
unsigned int carry = 0;
for(size_t i=0;i<len;++i){
data[i] = ~data[i] + !carry;
carry = (data[i]>>bits);
data[i]&=~(1u<<bits);
}
return *this;
}
Bigint_Fixedsize operator-()const{
Bigint_Fixedsize ret(*this);
ret.negate();
return ret;
}
bool operator<(Bigint_Fixedsize const&o)const{
// treat sign bit
if(data[len-1]>>(bits-1)!=o.data[len-1]>>(bits-1)){
return data[len-1]>>(bits-1);
}
for(size_t i=len-1;~i;--i){
if(data[i]!=o.data[i]) return data[i]<o.data[i];
}
return false;
}
bool operator>(Bigint_Fixedsize const&o)const{
return o<*this;
}
bool operator<=(Bigint_Fixedsize const&o)const{
return !(operator>(o));
}
bool operator>=(Bigint_Fixedsize const&o)const{
return !(operator<(o));
}
bool operator==(Bigint_Fixedsize const&o)const{
for(size_t i=0;i<len;++i){
if(data[i] !=o.data[i]) return false;
}
return true;
}
bool operator!=(Bigint_Fixedsize const&o)const{
return !operator==(o);
}
bool operator!()const{
for(size_t i=0;i<len;++i){
if(data[i]) return false;
}
return true;
}
bool is_negative()const{
return data[len-1]>>(bits-1);
}
void print_binary(ostream&o, Bigint_Fixedsize const&b){
o << "[";
for(size_t i=len;i>0;--i){
o << bitset<bits>(b.data[i-1]);
}
o << "]";
}
friend ostream& operator<<(ostream&o, Bigint_Fixedsize const&b){
if(b.is_negative()){
return o << '-' << -b << "\n";
}
Bigint_base10 ret(0);
int64_t base = 1u<<bits;
for(int i = len-1;i>=0;--i){
ret*=base;
ret+=Bigint_base10(b.data[i]);
}
o << ret;
return o;
}
explicit operator long double()const{
if(is_negative()){
return (long double)operator-();
}
long double ret = 0.0;
long double base = 1u<<bits;
for(int i = len-1;i>=0;--i){
ret = ret * base + data[i];
}
return ret;
}
/// TODO: implement for larger inputs
friend istream& operator>>(istream&i, Bigint_Fixedsize &b){
int64_t tmp;
i >> tmp;
b = Bigint_Fixedsize(tmp);
return i;
}
};
/**
* Biginteger with fixed precision
* Has 32*len bits, is signed
*
* Trying out more optimizations.
*/
template<size_t len>
struct Bigint_Fixedsize_Fast{
unsigned int data[len];
uint16_t siz;
bool sign;
static constexpr unsigned int bits = 32;
Bigint_Fixedsize_Fast(){
data[0] = 0;
siz = 1;
sign = false;
}
Bigint_Fixedsize_Fast(long long a){
sign = false;
if(a<0){
sign = true;
a=-a;
}
siz = 0;
do{
long long b = a>>bits;
data[siz] = a - (b<<bits);
a = b;
++siz;
} while(a);
}
void trim(){
while(siz>1 && !data[siz-1]) --siz;
if(siz == 1 && data[0] == 0) sign=false;
}
int comp_unsigned(Bigint_Fixedsize_Fast const&o)const{
uint16_t lim = min(siz, o.siz);
for(unsigned int i=lim;i<siz;++i){
if(data[i]){
return 1;
}
}
for(unsigned int i=lim;i<o.siz;++i){
if(o.data[i]){
return -1;
}
}
for(unsigned int i=lim-1;i+1;--i){
if(data[i]!=o.data[i]){
return data[i] < o.data[i]?-1:1;
}
}
return 0;
}
int comp(Bigint_Fixedsize_Fast const&o)const{
int sign_mul = 1-2*sign;
if(sign != o.sign){
return sign_mul;
}
return sign_mul * comp_unsigned(o);
}
bool operator<(Bigint_Fixedsize_Fast const&o)const{
return comp(o)<0;
}
bool operator>(Bigint_Fixedsize_Fast const&o)const{
return comp(o)>0;
}
bool operator<=(Bigint_Fixedsize_Fast const&o)const{
return comp(o)<=0;
}
bool operator>=(Bigint_Fixedsize_Fast const&o)const{
return comp(o)>=0;
}
bool operator==(Bigint_Fixedsize_Fast const&o)const{
return comp(o)==0;
}
bool operator!=(Bigint_Fixedsize_Fast const&o)const{
return comp(o)!=0;
}
bool operator!()const{
return operator==(ZERO);
}
Bigint_Fixedsize_Fast operator-()const{
Bigint_Fixedsize_Fast ret(*this);
if(!!ret){
ret.sign ^=1;
}
return ret;
}
Bigint_Fixedsize_Fast operator*(Bigint_Fixedsize_Fast const&o)const{
Bigint_Fixedsize_Fast ret;
ret.siz = min(siz+o.siz, (int)len);
ret.sign = (sign!=o.sign);
fill(ret.data, ret.data+ret.siz, 0);
for(unsigned int i=0;i<siz;++i){
unsigned long long carry = 0, carry_2;
for(unsigned int j=0;j<o.siz;++j){
carry+= data[i]*(unsigned long long)o.data[j] + ret.data[i+j];
carry_2 = carry >> bits;
ret.data[i+j] = carry - (carry_2<<bits);
carry = carry_2;
}
for(unsigned int j=i+o.siz;carry;++j){
carry+= ret.data[j];
carry_2 = carry >> bits;
ret.data[j] = carry - (carry_2<<bits);
carry = carry_2;
}
}
ret.trim();
return ret;
}
Bigint_Fixedsize_Fast& operator*=(Bigint_Fixedsize_Fast const&o){
*this = operator*(o);
return *this;
}
static void unsigned_add(Bigint_Fixedsize_Fast &ret, Bigint_Fixedsize_Fast const&A, Bigint_Fixedsize_Fast const&B){
const Bigint_Fixedsize_Fast *a = &A, *b = &B;
if(a->siz < b->siz) swap(a, b);
ret.sign = A.sign;
unsigned long long carry = 0, carry_2;
unsigned int j;
for(j=0;j<b->siz;++j){
carry+=(unsigned long long)a->data[j] + (unsigned long long)b->data[j];
carry_2 = carry>>bits;
ret.data[j] = carry - (carry_2<<bits);
carry = carry_2;
}
for(;j<a->siz;++j){
carry+=a->data[j];
carry_2 = carry>>bits;
ret.data[j] = carry - (carry_2<<bits);
carry = carry_2;
}
if(carry){
ret.data[j++] = carry;
}
ret.siz = j;
ret.trim();
}
static void unsigned_subtract(Bigint_Fixedsize_Fast &ret, Bigint_Fixedsize_Fast const&A, Bigint_Fixedsize_Fast const&B){
int com = A.comp_unsigned(B);
if(com == 0){
ret.sign = false;
ret.siz = 1;
ret.data[0] = 0;
return;
}
ret.sign = A.sign;
const Bigint_Fixedsize_Fast *a = &A, *b = &B;
if(com < 0){
ret.sign ^= true;
swap(a, b);
}
// deal with case then o is not trimed.
unsigned int min_siz = min(A.siz, B.siz);
unsigned long long carry = 0, carry_2;
unsigned int j;
for(j=0;j<min_siz;++j){
carry+=(unsigned long long)a->data[j] - (unsigned long long)b->data[j];
carry_2 = carry>>bits;
ret.data[j] = carry - (carry_2<<bits);
carry = -(carry_2 & 1u);
}
for(;carry;++j){
assert(j < a->siz);
carry+=a->data[j];
carry_2 = carry>>bits;
ret.data[j] = carry - (carry_2<<bits);
carry = -(carry_2 & 1u);
}
copy(a->data+j, a->data+a->siz, ret.data+j);
ret.siz = a->siz;
ret.trim();
}
static void add(Bigint_Fixedsize_Fast &ret, Bigint_Fixedsize_Fast const&A, Bigint_Fixedsize_Fast const&B){
if(A.sign == B.sign){
unsigned_add(ret, A, B);
} else {
unsigned_subtract(ret, A, B);
}
}
static void sub(Bigint_Fixedsize_Fast &ret, Bigint_Fixedsize_Fast const&A, Bigint_Fixedsize_Fast const&B){
if(A.sign != B.sign){
unsigned_add(ret, A, B);
} else {
unsigned_subtract(ret, A, B);
}
}
Bigint_Fixedsize_Fast operator+(Bigint_Fixedsize_Fast const&o)const{
Bigint_Fixedsize_Fast ret;
add(ret, *this, o);
return ret;
}
Bigint_Fixedsize_Fast& operator+=(Bigint_Fixedsize_Fast const&o){
add(*this, *this, o);
return *this;
}
Bigint_Fixedsize_Fast operator-(Bigint_Fixedsize_Fast const&o)const{
Bigint_Fixedsize_Fast ret;
sub(ret, *this, o);
return ret;
}
Bigint_Fixedsize_Fast operator-=(Bigint_Fixedsize_Fast const&o){
sub(*this, *this, o);
return *this;
}
/// TODO: more operators
void print_binary(ostream&o, Bigint_Fixedsize_Fast const&b){
o << "[";
o << sign << "/" << len << "/";
for(size_t i=siz;i>0;--i){
o << bitset<bits>(b.data[i-1]);
}
o << "]";
}
friend ostream& operator<<(ostream&o, Bigint_Fixedsize_Fast const&b){
if(b.sign){
return o << '-' << -b;
}
Bigint_base10 ret(0);
int64_t base = 1ll<<bits;
for(int i = b.siz-1;i>=0;--i){
ret*=base;
ret+=Bigint_base10(b.data[i]);
}
o << ret;
return o;
}
explicit operator long double()const{
if(sign){
return (long double)operator-();
}
long double ret = 0.0;
long double base = 1ll<<bits;
for(int i = siz-1;i>=0;--i){
ret = ret * base + data[i];
}
return ret;
}
/// TODO: implement for larger inputs
friend istream& operator>>(istream&i, Bigint_Fixedsize_Fast &b){
int64_t tmp;
i >> tmp;
b = Bigint_Fixedsize_Fast(tmp);
return i;
}
static const Bigint_Fixedsize_Fast ZERO;
};
template<size_t len>
const Bigint_Fixedsize_Fast<len> Bigint_Fixedsize_Fast<len>::ZERO(0);
#define lp_debug(x) do{}while(0)
//#define lp_debug(x) do{cerr << x; }while(0)
#define lp_debug(x) do{}while(0)
/**
* Represents an integer of the form
* <val> + <inf_part> * inf
* where inf is a symbol bigger than any integer
*/
template<typename INT>
class Barrier_Int{
private:
Barrier_Int(INT const&_val, INT const&_inf_part):val(_val), inf_part(_inf_part){}
public:
Barrier_Int():val(0), inf_part(0){}
explicit Barrier_Int(INT const&_val):val(_val), inf_part(0){}
static Barrier_Int infinity(){
return Barrier_Int(0, 1);
}
static Barrier_Int negative_infinity(){
return Barrier_Int(0, -1);
}
Barrier_Int operator-()const{
return Barrier_Int(-val, -inf_part);
}
Barrier_Int& operator+=(Barrier_Int const&o){
val+=o.val;
inf_part+=o.inf_part;
return *this;
}
Barrier_Int operator+(Barrier_Int const&o)const{
return Barrier_Int(val+o.val, inf_part+o.inf_part);
}
Barrier_Int& operator-=(Barrier_Int const&o){
val-=o.val;
inf_part-=o.inf_part;
return *this;
}
Barrier_Int operator-(Barrier_Int const&o)const{
return Barrier_Int(val-o.val, inf_part-o.inf_part);
}
Barrier_Int& operator*=(INT const&o){
val*=o;
inf_part*=o;
return *this;
}
Barrier_Int operator*(INT const&o)const{
return Barrier_Int(val*o, inf_part*o);
}
bool operator<(Barrier_Int const&o)const{
if(inf_part != o.inf_part) return inf_part < o.inf_part;
return val < o.val;
}
bool operator>(Barrier_Int const&o)const{
return o < *this;
}
bool operator>=(Barrier_Int const&o)const{
return !operator<(o);
}
bool operator<=(Barrier_Int const&o)const{
return !operator>(o);
}
bool operator==(Barrier_Int const&o)const{
return val == o.val && inf_part == o.inf_part;
}
bool operator!=(Barrier_Int const&o)const{
return val!=o.val || inf_part != o.inf_part;
}
friend ostream& operator<<(ostream&o, Barrier_Int const&b){
if(!b.inf_part){
return o << b.val;
}
o << b.inf_part << numeric_limits<double>::infinity() << "+" << b.val;
return o;
}
explicit operator long double()const{
if(inf_part != INT(0)){
return inf_part<INT(0) ? -numeric_limits<long double>::infinity() : numeric_limits<long double>::infinity();
}
return (long double)val;
}
public:
INT val;
INT inf_part;
};
/**
* Randomized LP in expected
* O(d! 4^d n)
* Does exact calculations.
* Does not need a barrier bound for dealing with unbounded parts
*/
template<typename FLOAT>
class Lp_Seidel_Barierless{
private:
// orthogonal projection of 'vec' into 'plane'
vector<FLOAT> proj_down(vector<FLOAT> const&vec, vector<FLOAT> const&plane, size_t j){
assert(vec.size() <= plane.size() && plane.size()<=vec.size()+1);
assert(j+1 < plane.size());
assert(!!plane[j]);
vector<FLOAT> ret (vec.size()-1);
//FLOAT tmp;
if(plane[j] < FLOAT(0)){
for(size_t i=0;i+1<vec.size();++i){
ret[i] = vec[j] * plane[i+(i>=j)] - vec[i+(i>=j)]* plane[j];
}
} else {
for(size_t i=0;i+1<vec.size();++i){
ret[i] = vec[i+(i>=j)]*plane[j] - vec[j]*plane[i+(i>=j)];
}
}
return ret;
}
// orthogonal projection of 'vec' out of 'plane'
vector<Barrier_Int<FLOAT> > proj_up(vector<Barrier_Int<FLOAT> > const&vec, vector<FLOAT> const&plane, size_t j){
assert(vec.size()+1 == plane.size());
assert(j+1 < plane.size());
assert(!!plane[j]);
vector<Barrier_Int<FLOAT> > ret(vec.size()+1);
copy(vec.begin(), vec.begin()+j, ret.begin());
copy(vec.begin()+j, vec.end(), ret.begin()+j+1);
for(size_t i=0;i<vec.size();++i){
ret[j]+=vec[i]*plane[i+(i>=j)];
}
FLOAT denom = plane[j];
if(denom < FLOAT(0)){
denom = -denom;
}
for(size_t i=0;i<vec.size();++i){
ret[i+(i>=j)]*=denom;
}
if(plane[j] >= FLOAT(0)){
ret[j] = -ret[j];
}
return ret;
}
Barrier_Int<FLOAT> planescal(vector<Barrier_Int<FLOAT> > const&x, vector<FLOAT> const&a){
assert(x.size() == a.size());
Barrier_Int<FLOAT> ret(0);
for(size_t i=0;i<x.size();++i){
ret+=x[i]*a[i];
}
return ret;
}
// solve lp recursively
vector<Barrier_Int<FLOAT>> solve(vector<vector<FLOAT> > const &A, vector<FLOAT> const&c, int d){
int n=A.size();
if(d==1){ // base case: single dimension
vector<Barrier_Int<FLOAT> > ret(2);
if(c[0] != FLOAT(0)){
ret[0] = (c[0]<FLOAT(0) ? Barrier_Int<FLOAT>::negative_infinity() : Barrier_Int<FLOAT>::infinity());
}
ret[1].val = FLOAT(1ull);
for(int i=0;i<n;++i){
if(ret[0]*A[i][0]+ret[1]*A[i].back()>Barrier_Int<FLOAT>(0)){
if(!A[i][0]){
lp_debug("infeasible single\n");
return vector<Barrier_Int<FLOAT>>();
}
ret[0] = Barrier_Int<FLOAT>(-A[i].back());
ret[1] = Barrier_Int<FLOAT>(A[i][0]);
if(ret[1] < Barrier_Int<FLOAT>(0)){
ret[1] = -ret[1];
ret[0] = -ret[0];
}
lp_debug(" -> " << i << " " << ret[0] << " " << ret[1] << "\n");
}
}
for(int i=0;i<n;++i){
if(ret[0]*A[i][0]+ret[1]*A[i].back()>Barrier_Int<FLOAT>(0)){
lp_debug("infeasible\n");
return vector<Barrier_Int<FLOAT>>();
}
}
return ret;
}
// initial solution
vector<Barrier_Int<FLOAT>> x(d+1);
for(int i=0;i<d;++i){
if(c[i] != FLOAT(0)){
x[i] = (c[i]<FLOAT(0) ? Barrier_Int<FLOAT>::negative_infinity() : Barrier_Int<FLOAT>::infinity());
}
}
x.back() = Barrier_Int<FLOAT>(1);
for(size_t i=0;i<A.size();++i){
if(planescal(x, A[i])>Barrier_Int<FLOAT>(0)){
int k = 0;
while(k<d && !A[i][k]) ++k;
// recurse
if(k==d) {lp_debug("what?\n"); return vector<Barrier_Int<FLOAT>>();} // degenerate failing plane??????
vector<vector<FLOAT> > A2(i);
for(size_t j=0;j<A2.size();++j){
A2[j] = proj_down(A[j], A[i], k);
}
shuffle(A2.begin(), A2.end(), Rng::get_engine());
lp_debug(string(2*d, ' ') << i << "\n");
vector<FLOAT> c2 = proj_down(c, A[i], k);
vector<Barrier_Int<FLOAT>> x2 = solve(A2, c2, d-1);
if(x2.empty()) return x2; // infeasible
x = proj_up(x2, A[i], k);
lp_debug(string(2*d, ' ') << ":");
lp_debug("";for(auto const&e:x) lp_debug(" " << e));
lp_debug("\n");
}
}
return x;
}
public:
vector<Barrier_Int<FLOAT>> solve(vector<vector<FLOAT> > const &A, vector<FLOAT> const&c){
assert(A.empty() || A[0].size() == c.size()+1);
return solve(A, c, c.size());
}
/**
* Maximize c^T x
* subject to Ax <= b
*
* Returns empty vector if infeasible
*/
vector<Barrier_Int<FLOAT>> solve(vector<vector<FLOAT> > A, vector<FLOAT> const&b, vector<FLOAT> const&c){
assert(A.size() == b.size());
for(unsigned int i=0;i<A.size();++i){
A[i].push_back(-b[i]);
}
return solve(A, c);
}
};
using ll=long long;
using lf=long double;
using elf=Bigint_Fixedsize_Fast<15>;
int main()
{
cin.tie(0)->sync_with_stdio(0);
// simplex with 7 dimensions, each being r, a1, a2, b1, b2, c1, c2 (1 and 2 exist due to bypassing nonnegativity constraints)
// max -r
// s.t. -r - a1*x^2 - b1*x - c1 <= -y
// -r + a1*x^2 + b1*x + c1 <= y
vector<elf>c{-1,0,0,0};
int q;cin>>q;
while(q--)
{
vector<vector<elf>>A;
vector<elf>B;
vector<elf>x;
int n;cin>>n;
while(n--)
{
ll x,y;
cin>>x>>y;
B.push_back(-y);
A.push_back({-1,-x*x,-x,-1});
B.push_back(y);
A.push_back({-1,x*x,x,1});
}
Lp_Seidel_Barierless<elf>solver;
auto res=solver.solve(A,B,c);
lf ans=(lf)res[0]/(lf)res[4];
cout<<setprecision(12)<<fixed<<ans*ans<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3776kb
input:
1 4 0 0 1 3 2 9 3 0
output:
5.062500000000
result:
ok found '5.0625000', expected '5.0625000', error '0.0000000'
Test #2:
score: 0
Accepted
time: 362ms
memory: 6852kb
input:
60 1 1000 -990 2 171 -638 949 -99 2 633 227 -257 -602 3 634 -994 633 999 -374 995 3 445 -110 586 -121 462 29 9 -995 -224 -458 -833 691 -670 456 -259 -376 55 -563 -12 834 827 -826 -220 299 744 17 997 991 997 976 997 988 998 -986 999 -982 999 -980 999 -996 998 -988 998 -991 997 987 1000 996 999 -1000 ...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 543160.125996398143 121.000000000000 0.832006181212 412780.607179428399 12.250000000000 15750.250000000000 118751.380086098421 880245.505473519532 1.000000000000 15.363373360329 854986.713164986710 20.250000000000 24567.66738...
result:
ok 60 numbers
Test #3:
score: 0
Accepted
time: 5ms
memory: 3792kb
input:
1000 1 -585478 527569 1 152984 679945 1 -174472 172630 1 235983 471538 1 -250372 650998 1 521028 -109032 1 121457 989514 1 916700 -223410 1 25908 939341 1 999032 369442 1 249207 -874185 1 -921949 719467 1 -692065 -756006 1 580461 644861 1 -382986 975568 1 644060 -113069 1 -588888 717169 1 2947 -3929...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 ...
result:
ok 1000 numbers
Test #4:
score: 0
Accepted
time: 9ms
memory: 3732kb
input:
1000 2 578578 -462573 -614596 -50411 2 568651 926188 -15389 -281674 2 -56242 -213116 215036 310015 2 -568452 -743741 -314862 -573269 2 -428037 -926383 -172945 -31965 2 -58020 145819 -69585 116311 2 -629887 -794837 704590 -761914 2 243217 -433618 98814 -457689 2 147490 681479 665176 -119632 2 -851707...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 ...
result:
ok 1000 numbers
Test #5:
score: 0
Accepted
time: 19ms
memory: 3788kb
input:
1000 3 -734917 -489090 419510 102330 712427 633246 3 36286 -456156 747264 -743132 260371 -674274 3 429263 14588 352092 -105774 547767 232534 3 -913665 328259 240305 -680653 -295994 -678964 3 597443 -368402 -231672 43641 -590555 396735 3 -603016 904082 -607928 649743 464117 526551 3 350193 -351624 33...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 ...
result:
ok 1000 numbers
Test #6:
score: 0
Accepted
time: 34ms
memory: 3792kb
input:
1000 4 -48411 -514672 165369 -349660 -281244 -842990 50473 -422110 4 -487482 -318709 861670 -709796 -491931 -335068 -523699 -455262 4 -817575 -338873 869501 905839 -717462 -668516 841972 769497 4 530706 615905 128991 -871809 82920 -948448 -317630 -725769 4 144451 772470 -923425 791489 513030 193835 ...
output:
0.001635252927 0.441888893679 0.095087558884 3655702.651955105179 769858.665807108583 876.964418129029 2.383378035147 0.003314331357 0.046074289956 0.414087095747 31534.752837082105 0.011533310762 1322109.793439251046 2475705.141855127644 5494047.076382468022 0.604758111540 36615.982224410417 15433....
result:
ok 1000 numbers
Test #7:
score: 0
Accepted
time: 59ms
memory: 3916kb
input:
1000 5 -425128 981633 -381689 946206 957441 996145 84010 712860 -8814 738024 5 235841 662950 864929 -477349 823444 -108225 714735 661226 300163 983888 5 -972539 106077 -856485 556296 -951397 193386 -207377 778279 -862794 555900 5 -877483 818676 537271 -193411 341352 408858 167065 819835 451709 87895...
output:
162.217423560921 24899.703037345875 111166879.556401949863 440.668043947199 45502620.026469050281 0.908165720214 48119370.938288595604 1331743.107360073524 1145.041044423147 11911.409089635788 898995.931063926371 0.281486398633 7.835265455787 0.770599173806 1167.305707531013 40318098119.960337292403...
result:
ok 1000 numbers
Test #8:
score: 0
Accepted
time: 183ms
memory: 3836kb
input:
1000 10 860001 272235 -30508 220967 711207 504388 77794 647164 303746 959200 592742 534104 57277 254211 266565 968002 919148 568676 991753 -20796 10 95213 204518 35283 198770 69842 203724 -316246 248661 -319918 245804 -923990 767251 -689125 503455 175418 229272 90053 206083 -815528 637145 10 -808164...
output:
52855287328.797471806407 4736213.545027937557 325.276604038309 11692527.619325693792 61306.467095067794 24947264304.330530360341 492734951022.492099851370 0.965345707591 1913.495504072973 385.474663045163 6.448445177198 42078421804.716278254986 5867468.015624116890 0.606672064838 24254772.5148242599...
result:
ok 1000 numbers
Test #9:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
1 4 -1000000 -1000000 -999999 1000000 999999 1000000 1000000 -1000000
output:
0.000000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3864kb
input:
1 4 -1000000 -1000000 -999999 1000000 -999998 1000000 -999997 -100000
output:
12656249999.999999998137
result:
ok found '12656250000.0000000', expected '12656250000.0000000', error '0.0000000'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3696kb
input:
1 4 -1000000 -1000000 -999999 1000000 -999998 1000000 -999997 -1000000
output:
0.000000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Test #12:
score: 0
Accepted
time: 2ms
memory: 3884kb
input:
1 4 -1000000 -300000 -999999 300000 -999998 300000 -999997 -300000
output:
0.000000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Test #13:
score: 0
Accepted
time: 2ms
memory: 3696kb
input:
1 4 -1000000 -999999 -999999 999998 -999998 999996 -999997 -999999
output:
0.562500000000
result:
ok found '0.5625000', expected '0.5625000', error '0.0000000'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3804kb
input:
8 4 -1000000 -1000000 -999999 1000000 999999 999977 1000000 -1000000 4 -1000000 -1000000 -999999 1000000 999999 999770 1000000 -1000000 4 -1000000 -1000000 -999999 1000000 999999 997770 1000000 -1000000 4 -1000000 -1000000 -999999 1000000 999999 977770 1000000 -1000000 4 -1000000 -1000000 -999999 10...
output:
33.062533062525 3306.253306252480 310806.560806483105 30885837.135829414370 62499875000.000000033528 561097185.448957108485 0.563062922156 2770157895.491001281422
result:
ok 8 numbers
Test #15:
score: 0
Accepted
time: 24ms
memory: 3884kb
input:
50 20 -78760 901241 -290160 346799 -100100 886312 -400033 -7842 -128289 858428 -443380 -236792 -204313 613533 870820 96059 812309 226162 -35539 980448 797663 345545 -445875 -256648 -460410 -299719 627726 793426 832862 169452 656272 795052 -339551 196857 -34433 992148 -388395 11457 -255059 482328 20 ...
output:
1995515551.235050740419 81676587.162761471642 15097.239959080308 23.395512429438 579359934.002431530855 3853.663650178120 50.381382962813 2611489.365440987983 131464690.062370108943 2137.820036269479 284.143965129583 564775635.752647274523 39822891364.848458692431 909.273877028092 16588.063569248844...
result:
ok 50 numbers
Test #16:
score: 0
Accepted
time: 4369ms
memory: 4704kb
input:
500 300 -574218 -271807 -443150 -83950 15479 867073 -467689 -121944 -318587 129168 -24306 766466 -968754 -612160 -814705 -519500 -60831 677156 -195474 372912 -44244 717366 -134450 505915 -523893 -204101 -179966 405956 -732527 -448979 -886997 -569400 -190507 383431 -538163 -223837 -885831 -568677 -60...
output:
223.573986308187 11176.342872447977 1192.744752752307 453187006.523357130325 554031869.361881787248 9.206159011827 126.713163644150 2.791225143125 7790357819.290426067542 13298746917.732571498491 138873066385.756835281849 4982989809.283447164577 67327474.877917289617 4313.350012823093 2045627.963990...
result:
ok 500 numbers
Test #17:
score: 0
Accepted
time: 6306ms
memory: 262648kb
input:
2 100000 856014 -110712 -748941 799207 -390374 -391739 448342 -991095 -64136 -981770 583018 -785726 -94728 -935377 768587 -365471 -102072 -963217 -547043 88834 -57865 -990529 -569447 175470 -331771 -501999 -123570 -924764 -86739 -946110 -481573 -114452 -143293 -909698 -188793 -835029 -368557 -415082...
output:
1147482388.192809438333 27.055180794732
result:
ok 2 numbers
Test #18:
score: 0
Accepted
time: 2868ms
memory: 4212kb
input:
1000 100 -119975 -664365 949391 58457 740004 -370401 540289 -639167 -788110 721100 416862 -768862 -510399 -3617 -438471 -204422 -820344 865532 -788887 755411 601194 -601478 -489457 -66989 -702681 494011 581714 -590937 621839 -564798 -596490 195550 390610 -797275 -511753 -13584 -295498 -456173 260700...
output:
601442791.649372832791 24.854725015605 12179361657.976363155991 3766193.376661313255 162530385978.939829275012 12661609.461962327060 4212.845367022982 47746959069.049593526870 38556717282.975946828723 34153564.440029987240 109150.232109145542 20645.945118390166 1045764154.094279524579 4163.031420954...
result:
ok 1000 numbers
Test #19:
score: 0
Accepted
time: 5815ms
memory: 4508kb
input:
1000 200 789124 -486241 -585609 207780 -420071 646265 -719341 -271629 798017 -517521 -358192 786061 882118 -897750 -572109 237809 584895 259362 -626674 62322 -504210 442329 -343020 802463 477414 549189 709507 -158428 802832 -539018 -273113 927143 639188 90473 -780485 -522065 330923 858488 719753 -19...
output:
91156684.238086921730 3501.157259590953 3851361410.710967715830 2454734.159761274972 5.035905023544 82.768320704366 284923642635.163662046194 380786260.886463643837 17.167559108997 5682477627.397740963846 4.455315979065 1265.284103345971 68.279475933634 1036.497538194536 2393.819117880462 791577964....
result:
ok 1000 numbers
Test #20:
score: 0
Accepted
time: 5826ms
memory: 4692kb
input:
666 300 733074 -538691 -186759 708121 19954 783003 144458 730949 204506 673961 553123 25690 226219 653422 624395 -178962 414482 359209 438237 305003 -273026 615231 -614391 -107992 -400931 412346 -483918 233375 -285489 597497 582783 -58643 -327538 537216 514448 125717 347458 484719 806041 -811603 783...
output:
14218890.034433960013 1560980.928444947096 135793682616.934556789696 597586.355954869641 2293035.645490295183 29924753323.643191620708 102.669386386863 2001978.225152625335 32.629073726008 3.322092988990 12200829391.231673670001 204120702.974940405882 59027030680.153708692640 6296990.545126972561 32...
result:
ok 666 numbers
Test #21:
score: 0
Accepted
time: 6026ms
memory: 5004kb
input:
500 400 -357828 -511505 811629 806298 595389 228873 228176 -405417 -718847 -22231 43107 -557461 478070 -20663 375542 -203854 468039 -41469 224127 -408358 87768 -530964 -370957 -500036 518859 60735 -647687 -152816 762116 659689 -140768 -600992 -314127 -540262 691144 464427 -822638 195188 640167 33654...
output:
2164267.283137999378 60833.544911473781 30687521.121466788658 409369199255.895405799150 156645670.157307220958 1133.028808246869 1180.614519619067 9652051207.435424483381 1404312.117983130425 116471.295269831420 19045036850.028315760195 47077498545.714740589261 136739.889082685168 132245.86845501955...
result:
ok 500 numbers
Test #22:
score: 0
Accepted
time: 5841ms
memory: 5344kb
input:
400 500 -500380 -894037 849708 881783 -558459 209296 851475 918558 -514750 -630889 829936 476110 -549945 42354 787624 -354093 814192 160977 -502873 -848842 772553 -637093 832402 525820 777452 -545681 -516265 -602612 813882 154790 822592 328889 818427 245057 846509 816020 798227 -150855 -540278 -1459...
output:
336358.682842416889 739007712.313469593588 8062740.943840485167 46895.725806574437 6280.637151560179 18764.817711915761 4518783186.362140861340 163.266277089865 444006974975.418383538723 43.545280607724 8.723681980637 3172.478944781044 1539479650.007746148389 143.955332874099 2.626373141941 11143941...
result:
ok 400 numbers
Test #23:
score: 0
Accepted
time: 5730ms
memory: 5780kb
input:
333 600 397147 736863 710330 449692 713165 445736 742693 406407 -80845 715759 -131224 681331 256197 788247 4673 760369 -636028 -4806 600988 576750 -287208 535378 644821 529254 699244 463843 -791164 -339893 804029 317447 -256744 568277 894333 169764 991373 -10561 701153 461309 -500555 240181 -655423 ...
output:
51526.628582581562 5144056.831773761380 1108984120.796890642960 536.957274890133 2.181490610248 2855000.521424936621 7.895070916774 26330215618.176730643958 70553.222649386660 4296.461204550854 158697.164637318537 27333878.097813651637 347065432.787239318830 11.780691242172 32965.376255889887 109260...
result:
ok 333 numbers
Test #24:
score: 0
Accepted
time: 5901ms
memory: 5856kb
input:
285 700 950915 -131859 287194 -348466 732575 15902 252777 -413880 216880 -487731 981137 -169278 536902 -34166 642935 14401 833007 -25394 549734 -25558 747152 12749 470592 -90277 905736 -83662 693698 19630 838447 -28990 954527 -136018 230712 -458599 739346 14424 369980 -213227 237154 -445291 106468 -...
output:
7872.481725145289 137.323613832406 7.321763401845 108838738.727765142285 143181888199.528975918889 180745.030983438276 3757486297.451869042125 49308496.072532682276 3.421210123941 3719.395042904195 5468199288.922670815606 36176108156.892100151628 167194.534382224159 4519810.729870232295 5.9426154848...
result:
ok 285 numbers
Test #25:
score: 0
Accepted
time: 5861ms
memory: 6148kb
input:
250 800 215344 949751 609032 593146 -105838 241410 -305554 -651790 -63750 385360 190202 926700 977848 -964837 372668 969088 723821 235110 -183595 -65083 2137 579745 874313 -407943 -274645 -490837 -352061 -909644 -108768 230807 118691 831006 7827 594698 -353540 -918151 479010 860107 528736 775396 137...
output:
1220.641278658595 3841279.090076846672 12.579284521272 575705766647.107006192207 1558427.697873099713 103457223182.733334295452 3.124442148396 4899838959.074188692495 787527992820.601503133774 398.600383507315 102710.607904608136 213705230964.380473181605 101864.459371873382 7589694361.195575245190 ...
result:
ok 250 numbers
Test #26:
score: 0
Accepted
time: 5670ms
memory: 6424kb
input:
222 900 159295 -261280 -490655 -364459 -564376 -402728 -604271 -425659 -11582 -247668 558244 -406118 307888 -296721 818686 -586122 623984 -445193 -316296 -295538 -829005 -584571 -271531 -282717 885932 -643583 -20565 -247759 944306 -697087 198978 -268580 189596 -266716 564738 -409799 652943 -463779 -...
output:
191.797796221606 888649.482900829887 2548.312980468019 237569579369.701734945178 24.760431996642 1529.443612273955 2001480394.485307409428 7281629.924218421376 50766.110966427898 168430563771.424650713801 806829.977464678186 1571.277728324362 6.718946983938 1568004021.378870078246 595.639091674366 5...
result:
ok 222 numbers
Test #27:
score: 0
Accepted
time: 5988ms
memory: 6720kb
input:
200 1000 921973 -681192 106799 -803185 -788145 993628 -416714 -4302 967514 -643801 151959 -857032 914844 -693738 440285 -958716 76084 -767643 -234739 -342853 -783938 978294 88850 -779199 994748 -589724 547596 -955741 827485 -776166 -509420 218589 -103043 -577817 -480852 137104 -207939 -394029 636786...
output:
451896268.535869163898 7.049967204322 5671529.651457276663 664528040183.412058472633 85.120650936934 7648180137.926402493380 1992099913.640064448584 44806.031678430998 1741949577.062794155907 3.191682230697 59567996.320442014967 831472726.286510259262 5.061339593504 53358834.160847968295 39478.58235...
result:
ok 200 numbers
Test #28:
score: 0
Accepted
time: 6377ms
memory: 34564kb
input:
20 10000 367451 -409708 455070 -596428 -849725 742244 471078 -632029 165323 -32253 -955469 715329 -923101 725677 481472 -655412 605011 -948267 -407637 634856 147682 -2823 212276 -113335 54592 143212 -915359 727879 -706038 746189 185824 -67108 -390828 623713 445008 -574302 488755 -671854 209758 -1088...
output:
4742.417475735073 5708564220.375649565831 173867306652.438182383776 262985.123262478953 430508202.913528355217 84862534.918685415912 880.520404815416 45696595.620074804199 315.429554562562 305020763584.989866018295 857.447656878375 138426330564.141373857856 7316.911907954080 81.128207016024 1490448....
result:
ok 20 numbers
Test #29:
score: -100
Time Limit Exceeded
input:
2 100000 456841 473364 686221 501790 971762 617932 -460757 766289 699 504584 432758 447040 374796 454319 572086 478648 -193285 584233 491439 463152 -248925 592962 990805 603449 271131 432749 632227 506041 663581 521394 835276 572284 -686905 908087 878120 545025 9952 525303 -230097 589015 520429 4563...