QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#809595#9804. Guess the PolygonOIer_kzcML 1ms4088kbC++205.6kb2024-12-11 16:12:422024-12-11 16:12:44

Judging History

This is the latest submission verdict.

  • [2024-12-11 16:12:44]
  • Judged
  • Verdict: ML
  • Time: 1ms
  • Memory: 4088kb
  • [2024-12-11 16:12:42]
  • Submitted

answer

#include <stdio.h>
#include <string.h>

#include <queue>
#include <algorithm>

#define LOG(FMT...) fprintf(stderr, FMT)

#define fi first
#define se second
#define em emplace
#define eb emplace_back

using namespace std;

typedef long long LL;
constexpr int N = 1005, LB = 1, B = 1 << LB;

vector<int> operator + (const vector<int> &a, const vector<int> &b) {
	vector<int> c(max(a.size(), b.size()));
	int x = 0;
	for (int i = 0; i < a.size() || i < b.size(); ++i) {
		if (i < a.size()) x += a[i];
		if (i < b.size()) x += b[i];
		c[i] = x & B - 1;
		x >>= LB;
	}
	if (x) {
		c.eb(x);
	}
	return c;
}

bool operator < (const vector<int> &a, const vector<int> &b) {
	if (a.size() != b.size()) {
		return a.size() < b.size();
	}
	for (int i = (int)a.size() - 1; ~i; --i) {
		if (a[i] != b[i]) {
			return a[i] < b[i];
		}
	}
	return false;
}

vector<int> operator - (vector<int> a, const vector<int> &b) {
	if (a < b) {
		LOG("ERR\n");
		exit(0);
	}
	for (int i = 0; i < (int)a.size(); ++i) {
		if (i < (int)b.size()) a[i] -= b[i];
		if (a[i] < 0) {
			a[i] += B;
			a[i + 1] -= 1;
		}
	}
	while (a.size() && !a.back()) {
		a.pop_back();
	}
	return a;
}

vector<int> operator * (const vector<int> &a, const vector<int> &b) {
	vector<int> c(a.size() + b.size() - 1, 0);
	for (int i = 0; i < (int)a.size(); ++i) {
		for (int j = 0; j < (int)b.size(); ++j) {
			c[i + j] += a[i] * b[j];
		}
	}
	int x = 0;
	for (int i = 0; i < (int)c.size(); ++i) {
		x += c[i];
		c[i] = x & B - 1;
		x >>= LB;
	}
	while (x) {
		c.eb(x & B - 1);
		x >>= LB;
	}
	return c;
}

void wr(const vector<int> &v) {
	if (v.empty()) {
		printf(" 0");
		return;
	}
	LL t = 0;
	for (int i = (int)v.size() - 1; ~i; --i) {
		t = t << 1 | v[i];
	}
	printf(" %lld", t);
}

vector<int> operator / (vector<int> a, const vector<int> &b) {
	if (a < b) {
		LOG("ERR div\n");
		exit(0);
	}
	if (b.empty() || b.back() == 0) {
		LOG("ERR div b\n");
		exit(0);
	}
	vector<int> ret((int)a.size() - (int)b.size() + 1, 0);
	int x = 0;
	while (a.size() >= b.size()) {
		a.back() += 2 * x;
		bool greater = true;
		for (int j = 0; j < (int)b.size(); ++j) {
			if (a[(int)a.size() - 1 - j] != b[(int)b.size() - 1 - j]) {
				if (a[(int)a.size() - 1 - j] < b[(int)b.size() - 1 - j]) {
					greater = false;
				}
				break;
			}
		}
		if (greater) {
			ret[(int)a.size() - (int)b.size()] = 1;
			for (int j = (int)b.size() - 1; ~j; --j) {
				a[(int)a.size() - 1 - j] -= b[(int)b.size() - 1 - j];
				if (a[(int)a.size() - 1 - j] < 0) {
					a[(int)a.size() - 1 - j] += B;
					a[(int)a.size() - j] -= 1;
				}
			}
		}
		x = a.back(), a.pop_back();
	}
	while (ret.size() && !ret.back()) {
		ret.pop_back();
	}
	for (int x : a) {
		if (x) {
			LOG("ERR\n");
			exit(0);
		}
	}
	return ret;
}

vector<int> lshift(vector<int> a) {
	if (a.empty()) {
		return a;
	}
	for (int i = 1; i < (int)a.size(); ++i) {
		a[i - 1] = a[i];
	}
	a.pop_back();
	return a;
}

vector<int> rshift(vector<int> a) {
	a.eb(0);
	for (int i = (int)a.size() - 1; i; --i) {
		a[i] = a[i - 1];
	}
	a[0] = 0;
	return a;
}

vector<int> gcd(const vector<int> &a, const vector<int> &b) {
	if (a.empty()) {
		return b;
	}
	if (b.empty()) {
		return a;
	}
	if (!(a[0] & 1) && !(b[0] & 1)) {
		return rshift(gcd(lshift(a), lshift(b)));
	}
	if (!(a[0] & 1)) {
		return gcd(lshift(a), b);
	}
	if (!(b[0] & 1)) {
		return gcd(a, lshift(b));
	}
	return a < b ? gcd(a, b - a) : gcd(a - b, b);
}

void setv(vector<int> &v, LL t) {
	v.clear();
	LL x = t;
	while (x) {
		v.eb(x & B - 1);
		x >>= LB;
	}
}

LL gcd(LL x, LL y) {
	return y ? gcd(y, x % y) : x;
}

pair<LL, LL> simp(LL a, LL b) {
	if (a == 0ll) {
		return {0ll, 1ll};
	}
	LL d = gcd(a, b);
	a = a / d, b = b / d;
	return {a, b};
}

pair<LL, LL> operator + (const pair<LL, LL> &s, const pair<LL, LL> &t) {
	return simp(s.fi * t.se + s.se * t.fi, s.se * t.se);
}

pair<LL, LL> operator * (const pair<LL, LL> &s, int t) {
	return simp(s.fi * t, s.se);
}

pair<LL, LL> operator / (const pair<LL, LL> &s, int t) {
	return simp(s.fi, s.se * t);
}

struct Frac {
	vector<int> x, y;
	Frac() : x{}, y{1} {}
	Frac(const vector<int> &_x, const vector<int> &_y) : x(_x), y(_y) {}
	Frac simp(vector<int> a, vector<int> b) const {
		vector<int> d = gcd(a, b);
		a = a / d, b = b / d;
		return Frac(a, b);
	}
	Frac(const pair<LL, LL> &t) {
		setv(x, t.fi), setv(y, t.se);
	}
	Frac operator + (const Frac &t) const {
		return simp(x * t.y + y * t.x, y * t.y);
	}
	void write() const {
		printf("!");
		wr(x), wr(y);
		puts("");
		fflush(stdout);
	}
};

pair<LL, LL> Q(pair<LL, LL> t) {
	printf("? %lld %lld\n", t.fi, t.se);
	fflush(stdout);
	pair<LL, LL> ret;
	scanf("%lld%lld", &ret.fi, &ret.se);
	return ret;
}

int n;
int xs[N], szd;
pair<LL, LL> v[N];

void solve1() {
	for (int i = 1; i < n - 1; ++i) {
		v[i] = Q(simp(xs[i], 1ll));
	}
	Frac res;
	v[0] = v[n - 1] = {0ll, 1ll};
	for (int i = 1; i < n; ++i) {
		res = res + Frac((v[i] + v[i - 1]) * (xs[i] - xs[i - 1]) / 2);
	}
	res.write();
	fflush(stdout);
}

void solve2() {
	Frac res;
	for (int i = 1; i < szd; ++i) {
		res = res + Frac(Q(simp(xs[i] + xs[i - 1], 2ll)) * (xs[i] - xs[i - 1]));
	}
	res.write();
	fflush(stdout);
}

int main() {
	int task;
	for (scanf("%d", &task); task--; ) {
		scanf("%d", &n);
		for (int i = 0, y; i < n; ++i) {
			scanf("%d%d", xs + i, &y);
		}
		sort(xs, xs + n);
		szd = unique(xs, xs + n) - xs;
		if (szd == n) {
			solve1();
		} else {
			solve2();
		}
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3808kb

input:

2
4
3 0
1 3
1 1
0 0
1 1
1 1
3
0 0
999 1000
1000 999
1999 1000

output:

? 1 2
? 2 1
! 3 1
? 999 1
! 1999 2

result:

ok correct! (2 test cases)

Test #2:

score: 0
Accepted
time: 1ms
memory: 3804kb

input:

9
4
1 1
1 3
3 0
0 0
3 2
1 2
4
0 0
1 3
1 1
3 0
1 2
3 2
4
0 0
3 0
1 2
1 1
1 2
1 2
4
0 0
3 0
1 2
1 1
1 1
1 2
4
0 0
3 0
1 1
1 2
1 2
1 1
3
1000 0
0 0
0 1000
500 1
4
0 0
1000 0
1000 1000
0 1000
1000 1
5
0 1
1000 1000
1000 0
0 1000
1 0
1999 2
1000 1
9
4 1000
3 1
2 1000
3 1000
1 1
2 1
0 0
1 1000
4 0
500 1
1...

output:

? 1 2
? 2 1
! 5 2
? 1 2
? 2 1
! 7 2
? 1 2
? 2 1
! 3 2
? 1 2
? 2 1
! 2 1
? 1 2
? 2 1
! 5 2
? 500 1
! 500000 1
? 500 1
! 1000000 1
? 1 2
? 1001 2
! 1999999 2
? 1 2
? 3 2
? 5 2
? 7 2
! 4003 2

result:

ok correct! (9 test cases)

Test #3:

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

input:

78
8
951 614
927 614
957 614
957 604
937 614
942 619
951 610
927 604
10 1
25 2
21 2
10 1
7
562 260
602 250
582 255
587 260
602 260
562 250
577 260
10 1
15 2
15 2
10 1
3
454 98
494 68
455 68
117 4
3
526 589
566 559
527 559
117 4
3
854 496
854 466
894 466
15 1
3
797 264
827 254
857 264
10 1
3
719 737
...

output:

? 932 1
? 1879 2
? 1893 2
? 954 1
! 317 1
? 1139 2
? 1159 2
? 1169 2
? 1189 2
! 375 1
? 455 1
! 585 1
? 527 1
! 585 1
? 874 1
! 600 1
? 827 1
! 300 1
? 739 1
! 600 1
? 162 1
! 400 1
? 1489 2
? 1499 2
? 772 1
! 275 1
? 1869 2
? 1879 2
? 1889 2
? 1899 2
? 1909 2
? 1919 2
? 1929 2
? 1939 2
? 1949 2
? 1...

result:

ok correct! (78 test cases)

Test #4:

score: -100
Memory Limit Exceeded

input:

34
24
123 815
168 800
133 795
27 827
153 805
28 830
178 780
138 810
78 830
192 772
148 790
88 810
43 825
183 795
103 805
163 785
118 800
93 825
63 835
73 815
58 820
198 790
48 840
108 820
10 3
95 6
15 2
95 6
15 2
95 6
15 2
95 6
15 2
95 6
15 2
95 6
15 2
95 6
15 2
95 6
15 2
95 6
15 2
95 6
15 2
15 1
24...

output:

? 28 1
? 43 1
? 48 1
? 58 1
? 63 1
? 73 1
? 78 1
? 88 1
? 93 1
? 103 1
? 108 1
? 118 1
? 123 1
? 133 1
? 138 1
? 148 1
? 153 1
? 163 1
? 168 1
? 178 1
? 183 1
? 192 1
! 1925 1
? 54 1
? 69 1
? 74 1
? 84 1
? 89 1
? 99 1
? 104 1
? 114 1
? 119 1
? 129 1
? 134 1
? 144 1
? 149 1
? 159 1
? 164 1
? 174 1
? ...

result: