QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#757005 | #9631. Median Replacement | dsakhdkas | WA | 1ms | 3716kb | C++14 | 3.3kb | 2024-11-16 23:27:08 | 2024-11-16 23:27:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int Mod=1e9+7,N=160;
int L[155],R[155];
int a[310],n;
int sum;
int f[155][3];
int ans=0;
int c[160];
int inv[500];
int ksm(int aa,int bb)
{
int res=1;
for (;bb;bb>>=1) {
if (bb&1) res=1LL*res*aa%Mod;
aa=1LL*aa*aa%Mod;
}
return res;
}
int Calc(int l,int r)
{
int res=0;
for (int i=1;i<=n+2;i++) {
int s=c[i];
for (int j=1;j<=n+2;j++)
if (j!=i) s=1LL*s*(r-(l+j-1))*inv[i-j+N]%Mod;
res=(res+s)%Mod;
}
return res;
}
void Solve(int l,int r)
{
if (r-l+1<=n+2) {
for (int x=l;x<=r;x++) {
f[0][0]=1;f[0][1]=0;f[0][2]=0;
for (int i=1;i<=n;i++) {
f[i][0]=f[i][1]=f[i][2]=0;
if (L[i]>x) {
f[i][2]=1LL*f[i-1][0]*(R[i]-L[i]+1)%Mod;
}
else if (R[i]<x) {
f[i][0]=1LL*(f[i-1][0]+f[i-1][1])%Mod*(R[i]-L[i]+1)%Mod;
f[i][1]=1LL*f[i-1][2]*(R[i]-L[i]+1)%Mod;
}
else {
f[i][0]=1LL*(f[i-1][0]+f[i-1][1])%Mod*(x-L[i])%Mod;
f[i][1]=1LL*f[i-1][2]*(x-L[i])%Mod;
f[i][2]=1LL*f[i-1][0]*(R[i]-x+1)%Mod;
}
}
ans=(ans+sum)%Mod;
ans=(ans-f[n][0])%Mod;
ans=(ans-f[n][1])%Mod;
ans=(ans-f[n][2])%Mod;
ans=(ans+Mod)%Mod;
}
}
else {
for (int x=l;x<=l+n+1;x++) {
f[0][0]=1;f[0][1]=0;f[0][2]=0;
for (int i=1;i<=n;i++) {
f[i][0]=f[i][1]=f[i][2]=0;
if (L[i]>x) {
f[i][2]=1LL*f[i-1][0]*(R[i]-L[i]+1)%Mod;
}
else if (R[i]<x) {
f[i][0]=1LL*(f[i-1][0]+f[i-1][1])%Mod*(R[i]-L[i]+1)%Mod;
f[i][1]=1LL*f[i-1][2]*(R[i]-L[i]+1)%Mod;
}
else {
f[i][0]=1LL*(f[i-1][0]+f[i-1][1])%Mod*(x-L[i])%Mod;
f[i][1]=1LL*f[i-1][2]*(x-L[i])%Mod;
f[i][2]=1LL*f[i-1][0]*(R[i]-x+1)%Mod;
}
}
int res=sum;
res=(res-f[n][0])%Mod;
res=(res-f[n][1])%Mod;
res=(res-f[n][2])%Mod;
res=(res+Mod)%Mod;
c[x-l+1]=(c[x-l]+res)%Mod;
}
ans=(ans+Calc(l,r))%Mod;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
for (int i=-N;i<=N;i++) inv[i+N]=ksm((i+Mod)%Mod,Mod-2);
while (T--) {
cin>>n;
int num=0;
a[++num]=1;
for (int i=1;i<=n;i++) {
cin>>L[i];
a[++num]=L[i];
}
for (int i=1;i<=n;i++) {
cin>>R[i];
a[++num]=R[i];
}
sort(a+1,a+num+1);
int tot=0;
for (int i=1;i<=num;i++)
if (a[i]!=a[tot]) a[++tot]=a[i];
sum=1;
for (int i=1;i<=n;i++) sum=1LL*sum*(R[i]-L[i]+1)%Mod;
ans=0;
for (int i=1;i<tot;i++)
if (a[i]+1<=a[i+1]-1) Solve(a[i]+1,a[i+1]-1);
for (int i=1;i<=tot;i++) Solve(a[i],a[i]);
cout<<ans<<'\n';
}
return 0;
}
/*
1
5
5 1 4 3 2
14 2 5 3 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3656kb
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 650 265 182 173 120 296 192 131
result:
ok 10 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
10 5 1 2 2 5 3 6 4 2 6 3 5 4 4 1 4 3 6 7 2 5 3 5 5 3 4 2 4 5 7 5 2 6 5 1 5 3 5 2 7 7 3 5 2 5 1 3 3 2 2 10 5 3 2 2 5 4 4 4 5 3 4 11 9 5 3 5 5 3 2 1 3 13 5 2 1 5 5 5 4 1 2 5 10 6 1 2 5 5 3 5 3 4 2 5 9 3 5 2 5 1 1 3 2 1 7 3 3 3 1
output:
120 230 144 110 110 289 324 89 140 122
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
10 5 3 1 3 4 4 9 1 3 10 4 5 1 1 3 1 1 9 1 3 3 1 5 5 1 2 3 1 74 1 2 3 1 5 2 5 5 3 4 5 6 8 3 4 5 2 1 3 4 5 2 4 6 4 5 5 2 4 2 1 3 2 11 3 2 3 5 1 5 4 4 2 1 14 6 6 2 5 4 1 3 5 1 9 2 4 5 1 5 4 1 2 4 4 6 1 6 4 4 5 3 2 5 3 5 8 8 5 3 5
output:
196 76 140 172 72 80 486 84 65 224
result:
ok 10 lines
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 3716kb
input:
10 5 676437428 903889545 700650370 965758082 146716866 676437431 903889567 700650370 965758082 146716866 5 517457740 64600397 388618400 783268973 388618400 517457797 64600397 388618400 783268973 388618400 5 971452763 106948541 259878781 537741075 9504353 971452780 106948544 259878781 537741075 95043...
output:
825993591 914556031 638362939 858937930 259410291 182584923 42617394 494243041 948543027 19543785
result:
wrong answer 1st lines differ - expected: '157838571', found: '825993591'