QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736818#9631. Median ReplacementOIer_kzcWA 0ms3868kbC++174.2kb2024-11-12 13:31:262024-11-12 13:31:26

Judging History

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

  • [2024-11-12 13:31:26]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3868kb
  • [2024-11-12 13:31:26]
  • 提交

answer

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

#include <vector>
#include <random>
#include <chrono>
#include <algorithm>

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

#define eb emplace_back

using namespace std;

typedef long long LL;
constexpr int N = 305;
constexpr int mod = 1e9 + 7;

constexpr int inv(int x, int k = mod - 2) {
	int r = 1;
	while (k) {
		if (k & 1) {
			r = x * (LL)r % mod;
		}
		x = x * (LL)x % mod;
		k >>= 1;
	}
	return r;
}

void Add(int &x, int y) {
	if ((x += y) >= mod) {
		x -= mod;
	}
}

void Sub(int &x, int y) {
	if ((x -= y) < 0) {
		x += mod;
	}
}

constexpr int neg(int x) {
	return x ? mod - x : 0;
}

int fact[N], infact[N];

void prefact() {
	constexpr int n = N - 1;
	*fact = 1;
	for (int i = 1; i <= n; ++i) {
		fact[i] = fact[i - 1] * (LL)i % mod;
	}
	infact[n] = inv(fact[n]);
	for (int i = n; i; --i) {
		infact[i - 1] = infact[i] * (LL)i % mod;
	}
}

int n, res, prod;
struct Seg {
	int l, r;
} sg[N];
int df[N], szd;

struct Poly {
	vector<int> a;
	Poly() {}
	Poly(int _x) : a{_x} {}
	Poly(int _x, int _y) : a{_x, _y} {}
	Poly(const vector<int> &_a) : a(_a) {}
	bool empty() const {
		return a.empty();
	}
	int size() const {
		return (int)a.size();
	}
	void clear() {
		a.clear();
	}
	void eb(int x) {
		a.eb(x);
	}
	int operator[] (int x) const {
		assert(0 <= x && x < (int)a.size());
		return a[x];
	}
	int &operator[] (int x) {
		assert(0 <= x && x < (int)a.size());
		return a[x];
	}
	void operator += (const Poly &t) {
		while (a.size() < t.size()) {
			a.eb(0);
		}
		for (int i = 0; i < t.size(); ++i) {
			Add(a[i], t[i]);
		}
	}
	Poly operator * (const Poly &t) const {
		if (a.empty() || t.empty()) {
			return Poly();
		}
		vector<int> c(a.size() + t.size() - 1, 0);
		for (int i = 0; i < t.size(); ++i) {
			for (int j = 0; j < a.size(); ++j) {
				c[i + j] = (c[i + j] + t[i] * (LL)a[j]) % mod;
			}
		}
		return Poly(c);
	}
};

Poly f[3], g[3], F;

int w[N], y[N], pr[N], sf[N];
void reset() {
	for (int i = 0; i <= n + 1; ++i) {
		w[i] = 1;
	}
}
int calc(int m) {
	y[0] = w[0];
	for (int i = 1; i <= n + 1; ++i) {
		y[i] = y[i - 1] + w[i];
		if (y[i] >= mod) {
			y[i] -= mod;
		}
	}
	if (m <= n + 1) {
		return y[m];
	}
	pr[0] = 1;
	for (int i = 0; i <= n; ++i) {
		pr[i + 1] = pr[i] * (LL)(m - i) % mod;
	}
	sf[n + 1] = 1;
	for (int i = n + 1; i; --i) {
		sf[i - 1] = sf[i] * (LL)(m - i) % mod;
	}
	int s = 0;
	for (int i = 0; i <= n + 1; ++i) {
		int v = y[i] * (LL)infact[i] % mod * infact[n + 1 - i] % mod * pr[i] % mod * sf[i] % mod;
		(n + 1 - i & 1 ? Sub : Add)(s, v);
	}
	return s;
}
void mov() {
	for (int i = 0; i <= n + 1; ++i) {
		w[i] = w[i] * (LL)i % mod;
	}
}

// L <= x < R
void solve(int L, int R) {
	f[0].clear(), f[1].clear(), f[2].clear();
	f[2].eb(1);
	for (int i = 0; i < n; ++i) {
		auto &[l, r] = sg[i];
		if (r <= L) {
			g[1] += f[0] * (r - l);
			g[2] += f[1] * (r - l);
			g[2] += f[2] * (r - l);
		} else if (l <= L) {
			g[1] += f[0] * Poly(mod - l, 1);
			g[2] += f[1] * Poly(mod - l, 1);
			g[2] += f[2] * Poly(mod - l, 1);
		}
		if (R <= l) {
			g[0] += f[2] * (r - l);
		} else if (R <= r) {
			g[0] += f[2] * Poly(r, mod - 1);
		}
		for (int j = 0; j < 3; ++j) {
			f[j] = g[j];
			g[j].clear();
		}
	}
	F.clear();
	F += f[0];
	F += f[1];
	F += f[2];
	for (int i = 0; i < F.size(); ++i) {
		F[i] = neg(F[i]);
	}
	if (F.empty()) {
		F.eb(0);
	}
	Add(F[0], prod);
	reset();
	for (int i = 0; i < F.size(); ++i) {
		res = (res + (calc(R - 1) - calc(L - 1) + mod) * (LL)F[i]) % mod;
		mov();
	}
}

int main() {
	int task;
	for (scanf("%d", &task); task--; ) {
		scanf("%d", &n);
		res = 0, prod = 1;
		for (int i = 0; i < n; ++i) {
			auto &[l, r] = sg[i];
			scanf("%d", &l);
		}
		for (int i = 0; i < n; ++i) {
			auto &[l, r] = sg[i];
			scanf("%d", &r);
		}
		for (int i = 0; i < n; ++i) {
			auto &[l, r] = sg[i];
			r += 1;
			df[szd++] = l, df[szd++] = r;
			prod = prod * (LL)(r - l) % mod;
		}
		df[szd++] = 1;
		sort(df, df + szd), szd = unique(df, df + szd) - df;
		for (int i = 0; i + 1 < szd; ++i) {
			solve(df[i], df[i + 1]);
		}
		printf("%d\n", res);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3868kb

input:

10
5
5 1 4 3 2
14 2 5 3 2
5
4 5 1 2 3
13 7 1 2 3
5
5 2 5 3 1
10 2 12 3 2
5
5 5 3 1 5
57 5 3 1 5
5
2 2 3 3 5
4 5 4 4 5
5
4 5 3 5 3
13 7 3 5 3
5
5 1 4 2 3
14 3 4 2 3
5
1 2 5 4 5
2 8 5 7 5
5
1 1 3 5 1
8 2 3 8 1
5
4 4 4 2 3
5 10 5 2 3

output:

180
999999757
999999381
265
182
999999760
120
999999823
192
131

result:

wrong answer 2nd lines differ - expected: '170', found: '999999757'