QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#750274 | #9631. Median Replacement | xiaolei338 | WA | 1ms | 3636kb | C++20 | 7.9kb | 2024-11-15 13:47:15 | 2024-11-15 13:47:16 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 310, mod = 998244353, INF = 0x3f3f3f3f;
random_device rd;
mt19937_64 rng(rd());
LL n, m;
// LL a[N];
template<class T>
constexpr T power(T a, LL b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr LL mul(LL a, LL b, LL p) {
LL res = a * b - LL(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<LL P>
struct MLong {
LL x;
constexpr MLong() : x{} {}
constexpr MLong(LL x) : x{norm(x % getMod())} {}
static LL Mod;
constexpr static LL getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(LL Mod_) {
Mod = Mod_;
}
constexpr LL norm(LL x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr LL val() const {
return x;
}
explicit constexpr operator LL() const {
return x;
}
constexpr MLong operator-() const {
MLong res;
res.x = norm(getMod() - x);
return res;
}
constexpr MLong inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MLong &operator*=(MLong rhs) & {
x = mul(x, rhs.x, getMod());
return *this;
}
constexpr MLong &operator+=(MLong rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MLong &operator-=(MLong rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MLong &operator/=(MLong rhs) & {
return *this *= rhs.inv();
}
friend constexpr MLong operator*(MLong lhs, MLong rhs) {
MLong res = lhs;
res *= rhs;
return res;
}
friend constexpr MLong operator+(MLong lhs, MLong rhs) {
MLong res = lhs;
res += rhs;
return res;
}
friend constexpr MLong operator-(MLong lhs, MLong rhs) {
MLong res = lhs;
res -= rhs;
return res;
}
friend constexpr MLong operator/(MLong lhs, MLong rhs) {
MLong res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
LL v;
is >> v;
a = MLong(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {
return os << a.val();
}
friend constexpr bool operator==(MLong lhs, MLong rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MLong lhs, MLong rhs) {
return lhs.val() != rhs.val();
}
};
template<>
LL MLong<0LL>::Mod = LL(1E18) + 9;
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(LL x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
LL v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = 998244353;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 1000000007;
using Z = MInt<P>;
LL l[N], r[N], pos[N], len[N];
Z f[N][2][2][2], cx[N], cy[N];
Z ans;
void solve()
{
cin >> n;
m = 0;
for(int i = 1; i <= n; i ++)cin >> l[i], pos[++ m] = l[i] - 1;
for(int i = 1; i <= n; i ++)cin >> r[i], pos[++ m] = r[i];
for(int i = 1; i <= n; i ++)len[i] = r[i] - l[i] + 1;
pos[++ m] = 0;
ans = 0;
sort(pos + 1, pos + 1 + m);
int sz = unique(pos + 1, pos + 1 + m) - pos - 1;
auto dp = [&](int va) -> Z{
memset(f, 0, sizeof(f));
f[0][0][0][0] = 1;
for(int i = 1; i <= n; i ++){
for(int j = 0; j <= 1; j ++){
for(int k = 0; k <= 1; k ++){
for(int al = 0; al <= 1; al ++){
if(f[i - 1][j][k][al] != 0){
if(va <= r[i]){
if(va > l[i])f[i][k][0][al] += f[i - 1][j][k][al] * (va - l[i]);
f[i][k][1][al | j | k] += f[i - 1][j][k][al] * (1 + r[i] - max(l[i], (LL)va));
}else{
f[i][k][0][al] += len[i] * f[i - 1][j][k][al];
}
}
}
}
}
}
Z sum = 0;
for(int i = 0; i <= 1; i ++){
for(int j = 0; j <= 1; j ++){
sum += f[n][i][j][1];
}
}
return sum;
};
for(int k = 1; k < sz; k ++)
{
int L = pos[k] + 1, R = pos[k + 1];
if((R - L + 1) <= n){
for(int i = L; i <= R; i ++)ans += dp(i);
}else{
LL ccl = 0;
for(int i = L; i <= L + n + 1; i ++)cx[++ ccl] = i, cy[ccl] = dp(i);
for(int i = 2; i <= ccl; i ++)cy[i] += cy[i - 1];
for(int i = 1; i <= ccl; i ++)
{
Z sum = cy[i], fk = 1;
for(int j = 1; j <= ccl; j ++){
if(i != j){
Z sum1 = R - cx[j], sum2 = cx[i] - cx[j];
sum *= sum1;
fk *= sum2;
}
ans += sum * power(fk, P - 2);
}
}
}
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
LL _T = 1;
cin >> _T;
while(_T --)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3636kb
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 170 333347420 265 182 173 120 296 192 131
result:
wrong answer 3rd lines differ - expected: '650', found: '333347420'