QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#433714#8786. The Whole Worlducup-team159#TL 2417ms4252kbC++2314.6kb2024-06-08 13:17:202024-06-08 13:17:20

Judging History

你现在查看的是最新测评结果

  • [2024-06-08 13:17:20]
  • 评测
  • 测评结果:TL
  • 用时:2417ms
  • 内存:4252kb
  • [2024-06-08 13:17:20]
  • 提交

answer

#line 1 "F.cpp"
// #pragma GCC target("avx2,avx512f,avx512vl,avx512bw,avx512dq,avx512cd,avx512vbmi,avx512vbmi2,avx512vpopcntdq,avx512bitalg,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("Ofast")

#line 2 "/mnt/c/Users/tsigm/Documents/Cprogram/library/template.hpp"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> bool chmax(T& x, U y){
	if(x<y){ x=y; return true; }
	return false;
}
template<class T,class U> bool chmin(T& x, U y){
	if(y<x){ x=y; return true; }
	return false;
}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class T>
V<T> Vec(size_t a) {
    return V<T>(a);
}
template<class T, class... Ts>
auto Vec(size_t a, Ts... ts) {
  return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));
}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

template<class D> D divFloor(D a, D b){
	return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);
}
template<class D> D divCeil(D a, D b) {
	return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);
}

/*
x       0  1  2  3  4  5  6  7  8  9
bsr(x) -1  0  1  1  2  2  2  2  3  3
最上位bit
*/
int bsr(int x){
	return x == 0 ? -1 : 31 ^ __builtin_clz(x);
}
int bsr(uint x){
	return x == 0 ? -1 : 31 ^ __builtin_clz(x);
}
int bsr(ll x){
	return x == 0 ? -1 : 63 ^ __builtin_clzll(x);
}
int bsr(ull x){
	return x == 0 ? -1 : 63 ^ __builtin_clzll(x);
}

/*
x       0  1  2  3  4  5  6  7  8  9
bsl(x) -1  0  1  0  2  0  1  0  3  0
最下位bit
*/
int bsl(int x){
	if(x==0) return -1;
	return __builtin_ctz(x);
}
int bsl(uint x){
	if(x==0) return -1;
	return __builtin_ctz(x);
}
int bsl(ll x){
	if(x==0) return -1;
	return __builtin_ctzll(x);
}
int bsl(ull x){
	if(x==0) return -1;
	return __builtin_ctzll(x);
}


template<class T>
T rnd(T l,T r){	//[l,r)
	using D = uniform_int_distribution<T>;
	static random_device rd;
	static mt19937 gen(rd());
	return D(l,r-1)(gen);
}
template<class T>
T rnd(T n){	//[0,n)
	return rnd(T(0),n);
}
#line 5 "F.cpp"

/*
	sunset's BigInt
*/

// base and base_digits must be consistent
const int base = 1000000000;
const int base_digits = 9;

struct Int {
	vector<int> z;
	int sign;

	Int() : sign(1) {}

	Int(long long v) {
		*this = v;
	}

	Int(const string &s) {
		read(s);
	}

	void operator=(const Int &v) {
		sign = v.sign;
		z = v.z;
	}

	void operator=(long long v) {
		sign = 1;
		if (v < 0) {
			sign = -1, v = -v;
		}
		z.clear();
		for (; v > 0; v = v / base) {
			z.push_back(v % base);
		}
	}

	Int operator+(const Int &v) const {
		if (sign == v.sign) {
			Int res = v;
			for (int i = 0, carry = 0; i < (int) max(z.size(), v.z.size()) || carry; ++i) {
				if (i == (int) res.z.size()) {
					res.z.push_back(0);
				}
				res.z[i] += carry + (i < (int) z.size() ? z[i] : 0);
				carry = res.z[i] >= base;
				if (carry) {
					res.z[i] -= base;
				}
			}
			return res;
		} else {
			return *this - (-v);
		}
	}

	Int operator-(const Int &v) const {
		if (sign == v.sign) {
			if (abs() >= v.abs()) {
				Int res = *this;
				for (int i = 0, carry = 0; i < (int) v.z.size() || carry; ++i) {
					res.z[i] -= carry + (i < (int) v.z.size() ? v.z[i] : 0);
					carry = res.z[i] < 0;
					if (carry) {
						res.z[i] += base;
					}
				}
				res.trim();
				return res;
			} else {
				return -(v - *this);
			}
		} else {
			return *this + (-v);
		}
	}

	void operator*=(int v) {
		if (v < 0) {
			sign = -sign, v = -v;
		}
		for (int i = 0, carry = 0; i < (int) z.size() || carry; ++i) {
			if (i == (int) z.size()) {
				z.push_back(0);
			}
			long long cur = (long long) z[i] * v + carry;
			carry = cur / base;
			z[i] = cur % base;
			// asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
		}
		trim();
	}

	Int operator*(int v) const {
		Int res = *this;
		res *= v;
		return res;
	}

	friend pair<Int, Int> divmod(const Int &a1, const Int &b1) {
		int norm = base / (b1.z.back() + 1);
		Int a = a1.abs() * norm;
		Int b = b1.abs() * norm;
		Int q, r;
		q.z.resize(a.z.size());
		for (int i = a.z.size() - 1; i >= 0; i--) {
			r *= base;
			r += a.z[i];
			int s1 = b.z.size() < r.z.size() ? r.z[b.z.size()] : 0;
			int s2 = b.z.size() - 1 < r.z.size() ? r.z[b.z.size() - 1] : 0;
			int d = ((long long) s1 * base + s2) / b.z.back();
			r -= b * d;
			while (r < 0) {
				r += b, --d;
			}
			q.z[i] = d;
		}
		q.sign = a1.sign * b1.sign;
		r.sign = a1.sign;
		q.trim();
		r.trim();
		return make_pair(q, r / norm);
	}

	friend Int sqrt(const Int &a1) {
		Int a = a1;
		while (a.z.empty() || (int) a.z.size() % 2 == 1) {
			a.z.push_back(0);
		}
		int n = a.z.size();
		int firstDigit = sqrt((long long) a.z[n - 1] * base + a.z[n - 2]);
		int norm = base / (firstDigit + 1);
		a *= norm;
		a *= norm;
		while (a.z.empty() || (int) a.z.size() % 2 == 1) {
			a.z.push_back(0);
		}
		Int r = (long long) a.z[n - 1] * base + a.z[n - 2];
		firstDigit = sqrt((long long) a.z[n - 1] * base + a.z[n - 2]);
		int q = firstDigit;
		Int res;
		for (int j = n / 2 - 1; j >= 0; j--) {
			for (;; --q) {
				Int r1 =
					(r - (res * 2 * base + q) * q) * base * base +
					(j > 0 ? (long long) a.z[2 * j - 1] * base + a.z[2 * j - 2] : 0);
				if (r1 >= 0) {
					r = r1;
					break;
				}
			}
			res *= base;
			res += q;
			if (j > 0) {
				int d1 = res.z.size() + 2 < r.z.size() ? r.z[res.z.size() + 2] : 0;
				int d2 = res.z.size() + 1 < r.z.size() ? r.z[res.z.size() + 1] : 0;
				int d3 = res.z.size() < r.z.size() ? r.z[res.z.size()] : 0;
				q = ((long long) d1 * base * base + (long long) d2 * base + d3) /
						(firstDigit * 2);
			}
		}
		res.trim();
		return res / norm;
	}

	Int operator/(const Int &v) const {
		return divmod(*this, v).first;
	}

	Int operator%(const Int &v) const {
		return divmod(*this, v).second;
	}

	void operator/=(int v) {
		if (v < 0) {
			sign = -sign, v = -v;
		}
		for (int i = z.size() - 1, rem = 0; i >= 0; --i) {
			long long cur = z[i] + (long long) rem * base;
			z[i] = cur / v;
			rem = cur % v;
		}
		trim();
	}

	Int operator/(int v) const {
		Int res = *this;
		res /= v;
		return res;
	}

	int operator%(int v) const {
		if (v < 0) {
			v = -v;
		}
		int m = 0;
		for (int i = z.size() - 1; i >= 0; --i) {
			m = ((long long) m * base + z[i]) % v;
		}
		return m * sign;
	}

	void operator+=(const Int &v) {
		*this = *this + v;
	}
	void operator-=(const Int &v) {
		*this = *this - v;
	}
	void operator*=(const Int &v) {
		*this = *this * v;
	}
	void operator/=(const Int &v) {
		*this = *this / v;
	}

	bool operator<(const Int &v) const {
		if (sign != v.sign) {
			return sign < v.sign;
		}
		if (z.size() != v.z.size()) {
			return z.size() * sign < v.z.size() * v.sign;
		}
		for (int i = z.size() - 1; i >= 0; i--) {
			if (z[i] != v.z[i]) {
				return z[i] * sign < v.z[i] * sign;
			}
		}
		return false;
	}

	bool operator>(const Int &v) const {
		return v < *this;
	}
	bool operator<=(const Int &v) const {
		return !(v < *this);
	}
	bool operator>=(const Int &v) const {
		return !(*this < v);
	}
	bool operator==(const Int &v) const {
		return !(*this < v) && !(v < *this);
	}
	bool operator!=(const Int &v) const {
		return *this < v || v < *this;
	}

	void trim() {
		while (!z.empty() && z.back() == 0) {
			z.pop_back();
		}
		if (z.empty()) {
			sign = 1;
		}
	}

	bool isZero() const {
		return z.empty() || ((int) z.size() == 1 && !z[0]);
	}

	Int operator-() const {
		Int res = *this;
		res.sign = -sign;
		return res;
	}

	Int abs() const {
		Int res = *this;
		res.sign *= res.sign;
		return res;
	}

	long long longValue() const {
		long long res = 0;
		for (int i = z.size() - 1; i >= 0; i--) {
			res = res * base + z[i];
		}
		return res * sign;
	}

	friend Int gcd(const Int &a, const Int &b) {
		return b.isZero() ? a : gcd(b, a % b);
	}
	friend Int lcm(const Int &a, const Int &b) {
		return a / gcd(a, b) * b;
	}

	void read(const string &s) {
		sign = 1;
		z.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';
			}
			z.push_back(x);
		}
		trim();
	}

	friend istream &operator>>(istream &stream, Int &v) {
		string s;
		stream >> s;
		v.read(s);
		return stream;
	}

	friend ostream &operator<<(ostream &stream, const Int &v) {
		if (v.sign == -1) {
			stream << '-';
		}
		stream << (v.z.empty() ? 0 : v.z.back());
		for (int i = v.z.size() - 2; i >= 0; --i) {
			stream << setw(base_digits) << setfill('0') << v.z[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(cur % p[new_digits]);
				cur /= p[new_digits];
				cur_digits -= new_digits;
			}
		}
		res.push_back(cur);
		while (!res.empty() && res.back() == 0) {
			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;
	}

	Int operator*(const Int &v) const {
		vector<int> a6 = convert_base(this->z, base_digits, 6);
		vector<int> b6 = convert_base(v.z, 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);
		Int res;
		res.sign = sign * v.sign;
		for (int i = 0, carry = 0; i < (int) c.size(); i++) {
			long long cur = c[i] + carry;
			res.z.push_back(cur % 1000000);
			carry = cur / 1000000;
		}
		res.z = convert_base(res.z, 6, base_digits);
		res.trim();
		return res;
	}
};



Int C(Int x,int i){
	Int res = 1;
	rep(j,i){
		res *= x-j;
		res /= j+1;
	}
	return res;
}

bool has_int_solution(vector<vector<Int>> A, vector<Int> b){
	assert(si(A) == si(b));
	int M = si(A);
	if(M == 0) return true;
	int N = si(A[0]);
	auto Swap = [&](int i, int j){
		rep(k,M) swap(A[k][i],A[k][j]);
	};
	auto sub = [&](int i, int j, Int c){
		// A[*][i] -= A[*][j] * c
		assert(i != j);
		rep(k,M) A[k][i] -= A[k][j] * c;
	};
	auto flip = [&](int i){
		// A[*][i] *= -1
		rep(k,M) A[k][i] = -A[k][i];
	};

	rep(i,N) if(A[0][i] < 0) flip(i);
	{
		int non0 = -1;
		rep(i,N) if(!A[0][i].isZero()) non0 = i;
		if(non0 == -1){
			if(!b[0].isZero()) return false;
			vector<vector<Int>> AA(M-1,vector<Int>(N));
			rep(i,M-1) rep(j,N) AA[i][j] = A[i+1][j];
			vector<Int> bb(M-1);
			rep(i,M-1) bb[i] = b[i+1];
			return has_int_solution(AA,bb);
		}
		Swap(0,non0);
		// assert(A[0][0] != 0);
	}
	while(true){
		int mn = -1;
		int num_non0 = 0;
		rep(i,N) if(!A[0][i].isZero()){
			num_non0++;
			if(mn == -1 or A[0][i] < A[0][mn]){
				mn = i;
			}
		}
		rep(i,N) if(i != mn){
			Int q = A[0][i]/A[0][mn];
			sub(i,mn,q);
		}
		if(num_non0 == 1){
			Swap(0,mn);
			break;
		}
	}
	if(b[0]%A[0][0] != 0) return false;
	Int q = b[0]/A[0][0];
	vector<vector<Int>> AA(M-1,vector<Int>(N-1));
	rep(k,M-1) rep(i,N-1) AA[k][i] = A[k+1][i+1];
	vector<Int> bb(M-1);
	rep(k,M-1) bb[k] = b[k+1] - q * A[k+1][0];
	return has_int_solution(AA,bb);
}

int solve(){
	int M; cin >> M;
	vector<Int> xs(M), ys(M);
	rep(i,M) cin >> xs[i] >> ys[i];
	for(int D=0;;D++){
		show(D);
		int N = D+1;
		vector<vector<Int>> A(M,vector<Int>(N));
		vector<Int> b = ys;
		rep(i,M){
			rep(j,N){
				A[i][j] = C(xs[i],j);
			}
		}
		if(has_int_solution(A,b)){
			return D;
		}
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int T; cin >> T;
	while(T--) cout << solve() << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

input:

2
2
1 0
4 1
3
1 1
4 4
6 6

output:

3
1

result:

ok 2 number(s): "3 1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3848kb

input:

2
2
1 0
4 1
3
1 0
3 0
5 4

output:

3
3

result:

ok 2 number(s): "3 3"

Test #3:

score: 0
Accepted
time: 6ms
memory: 3660kb

input:

2
10
1 557
2 -172
3 -497
5 763
6 -149
7 -355
8 -29
9 -588
10 -171
11 -355
10
1 -461
2 -219
3 -45
4 -212
5 749
6 -294
9 -85
10 213
11 -412
12 125

output:

10
11

result:

ok 2 number(s): "10 11"

Test #4:

score: 0
Accepted
time: 505ms
memory: 3996kb

input:

20
10
1 -193165761
4 426322868
5 -408198139
7 -455731045
9 -389028341
17 -590246653
18 119481348
21 809814532
23 47591392
26 -21020402
10
3 -715116939
5 -263142266
6 -426687860
10 342227448
14 141724722
15 576758779
18 123410194
19 256532828
20 -223524833
25 386574889
10
5 34943085
7 238431559
9 168...

output:

25
22
23
20
20
25
23
25
26
23
23
25
29
23
24
29
29
27
25
19

result:

ok 20 numbers

Test #5:

score: 0
Accepted
time: 2417ms
memory: 4252kb

input:

100
10
1 158027281
3 -154375927
6 -515683907
9 -801063453
15 371607728
16 -30224647
24 -215349633
26 219182013
29 -87257968
30 186925822
10
2 205585983
9 740879281
11 -672242855
14 -53907640
16 146130715
20 -17941862
25 -424140108
26 593743162
27 -8310423
28 84863497
10
3 46810292
4 361101002
5 4687...

output:

29
25
25
20
19
25
20
29
29
19
25
19
26
26
27
21
27
26
25
25
24
26
27
25
25
27
26
23
27
23
29
25
27
26
28
29
29
20
21
23
22
25
23
16
25
29
26
25
26
18
23
18
23
19
28
19
26
26
24
18
26
19
23
27
21
23
17
26
28
25
27
23
16
19
25
26
23
25
14
23
20
20
25
23
24
23
19
19
20
20
22
26
26
25
22
23
28
17
19
19

result:

ok 100 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

100
30
1 -519015304
2 269671593
3 -163533023
4 830108438
5 337806976
6 -87888761
7 -195233355
8 -341350273
9 38092088
10 285610643
11 -240058763
12 256373103
13 297741964
14 -247379404
15 -26410774
16 -755197562
17 -643221179
18 159031836
19 689848941
20 622207228
21 -407862690
22 401550934
23 10884...

output:

29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29
29

result: