QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#837764 | #9631. Median Replacement | propane | WA | 3ms | 5172kb | C++20 | 5.3kb | 2024-12-30 12:55:31 | 2024-12-30 12:55:31 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<array>
#include<stdint.h>
#include<algorithm>
using namespace std;
using LL = long long;
const int maxn = 2e5 + 5, mod = 1e9 + 7;
template<const int T>
struct ModInt {
const static int mod = T;
int x;
ModInt(int x = 0) : x(x % mod) {}
ModInt(long long x) : x(int(x % mod)) {}
int val() { return x; }
ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
bool operator == (const ModInt &a) const { return x == a.x; };
bool operator != (const ModInt &a) const { return x != a.x; };
void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
void operator /= (const ModInt &a) { *this = *this / a; }
friend ModInt operator + (int y, const ModInt &a){ int x0 = y + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
friend ModInt operator - (int y, const ModInt &a){ int x0 = y - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
friend ModInt operator * (int y, const ModInt &a){ return ModInt(1LL * y * a.x % mod);}
friend ModInt operator / (int y, const ModInt &a){ return ModInt(y) / a;}
friend ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x;}
friend istream &operator>>(istream &is, ModInt &t){return is >> t.x;}
ModInt pow(int64_t n) const {
ModInt res(1), mul(x);
while(n){
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
ModInt inv() const {
int a = x, b = mod, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
if (u < 0) u += mod;
return u;
}
};
using mint = ModInt<mod>;
mint fact[maxn], invfact[maxn];
void init(){
fact[0] = invfact[0] = 1;
for(int i = 1; i < maxn; i++) fact[i] = fact[i - 1] * i;
invfact[maxn - 1] = fact[maxn - 1].inv();
for(int i = maxn - 2; i; i--)
invfact[i] = invfact[i + 1] * (i + 1);
}
inline mint C(int a, int b){
if (a < 0 || b < 0 || a < b) return 0;
return fact[a] * invfact[b] * invfact[a - b];
}
// O(n)
// x = a, a + 1, a + 2 ...
mint Lagrange(const vector<mint> &x, const vector<mint> &y, mint k){
const int n = (int)x.size() - 1;
vector<mint> pre(n + 2), suf(n + 2);
mint ans = 0;
pre[0] = k - x[0], suf[n + 1] = 1;
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] * (k - x[i]);
for (int i = n; i >= 0; i--) suf[i] = suf[i + 1] * (k - x[i]);
for (int i = 0; i <= n; i++){
mint up = (i == 0 ? 1 : pre[i - 1]) * suf[i + 1];
mint down = invfact[i] * invfact[n - i] * (((n - i) % 2 == 1) ? mint(0) - 1 : 1);
ans = (ans + y[i] * up * down);
}
return ans;
}
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
init();
int T;
cin >> T;
while(T--){
int n;
cin >> n;
vector<int> l(n), r(n);
vector<int> nums;
for(int i = 0; i < n; i++){
cin >> l[i];
nums.push_back(l[i]);
}
for(int i = 0; i < n; i++){
cin >> r[i];
nums.push_back(r[i] + 1);
}
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
auto get = [&](int x){
vector<array<array<mint, 2>, 2> > dp(n + 1);
dp[0][0][0] = 1;
mint all = 1;
for(int i = 0; i < n; i++){
all *= (r[i] - l[i] + 1);
mint c[2];
c[0] = max(0, x - l[i]);
c[1] = max(0, r[i] - x + 1);
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
for(int t = 0; t < 2; t++){
if (j + k + t <= 1){
dp[i + 1][k][t] += dp[i][j][k] * c[t];
}
}
}
}
}
mint ans = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
ans += dp[n][i][j];
}
}
return all - ans;
};
mint ans = 0;
for(int i = 0; i + 1 < nums.size(); i++){
int l = nums[i], r = nums[i + 1] - 1;
vector<mint> x(min(r - l + 1, 200));
vector<mint> y(min(r - l + 1, 200));
for(int j = 0; j < x.size(); j++){
x[j] = j + 1;
y[j] = get(l + j);
if (j - 1 >= 0) y[j] += y[j - 1];
}
ans += Lagrange(x, y, r - l + 1);
}
cout << ans << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 5172kb
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:
999520087 999733685 999864449 149303878 124 999852485 999519947 999997957 999984287 999973787
result:
wrong answer 1st lines differ - expected: '180', found: '999520087'