QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#742779 | #9631. Median Replacement | ucup-team4479# | WA | 1ms | 3584kb | C++23 | 3.6kb | 2024-11-13 17:18:22 | 2024-11-13 17:18:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N=155;
constexpr int MOD=1000000007;
int qpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1) res=(long long)res*a%MOD;
a=(long long)a*a%MOD,b>>=1;
}
return res;
}
int getinv(int x)
{
return qpow(x,MOD-2);
}
int C[N][N];
int B[N];
void init(int n)
{
for(int i=0;i<=n;i++)
{
C[i][0]=1;
for(int j=1;j<=i;j++)
C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
}
B[0]=1;
for(int i=1;i<=n;i++)
{
int sum=0;
for(int k=0;k<i;k++)
{
sum=(sum+(long long)C[i+1][k]*B[k])%MOD;
}
sum=(long long)sum*getinv(i+1)%MOD;
sum=(MOD-sum)%MOD;
B[i]=sum;
}
return;
}
int S(int n,int m)
{
int sum=0;
for(int k=0;k<=m;k++)
sum=(sum+(long long)C[m+1][k]*B[k]%MOD*qpow(n,m+1-k))%MOD;
return (long long)sum*getinv(m+1)%MOD;
}
typedef vector<int> Poly;
Poly operator*(const Poly &a,const Poly &b)
{
if(a.empty()||b.empty()) return {};
Poly 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]=(c[i+j]+(long long)a[i]*b[j])%MOD;
return c;
}
Poly operator+(const Poly &a,const Poly &b)
{
Poly c(max(a.size(),b.size()),0);
for(int i=0;i<(int)c.size();i++)
{
if(i<(int)a.size()) c[i]=(c[i]+a[i])%MOD;
if(i<(int)b.size()) c[i]=(c[i]+b[i])%MOD;
}
return c;
}
int n;
int l[N],r[N];
int v[N+N],tot;
struct Matrix
{
Poly mat[3][3];
friend Matrix operator*(const Matrix &a,const Matrix &b)
{
Matrix c;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
{
c.mat[i][j]=c.mat[i][j]+a.mat[i][k]*b.mat[k][j];
}
return c;
}
};
Matrix a[N];
Matrix solve(int l,int r)
{
if(l==r) return a[l];
int mid=(l+r)>>1;
return solve(l,mid)*solve(mid+1,r);
}
int calc(int L,int R)
{
int all=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
a[i].mat[j][k].clear();
if(r[i]<L)
{
a[i].mat[0][0]=a[i].mat[1][0]=a[i].mat[2][1]=Poly({r[i]-l[i]+1});
}
else if(l[i]>R)
{
a[i].mat[0][2]=a[i].mat[1][2]=Poly({r[i]-l[i]+1});
}
else
{
a[i].mat[0][0]=a[i].mat[1][0]=a[i].mat[2][1]=Poly({MOD-l[i],1});
a[i].mat[0][2]=a[i].mat[1][2]=Poly({r[i]+1,MOD-1});
}
all=(long long)all*(r[i]-l[i]+1)%MOD;
}
Matrix t=solve(1,n);
Poly g=t.mat[0][0]+t.mat[0][1]+t.mat[0][2];
int sum=0;
for(int i=0;i<(int)g.size();i++)
{
sum=(sum+(long long)(S(R+1,i)-S(L,i)+MOD)*g[i])%MOD;
// for(int j=L;j<=R;j++)
// sum=(sum+(long long)g[i]*qpow(j,i))%MOD;
}
int res=((long long)all*(R-L+1)-sum+MOD)%MOD;
return res;
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>l[i];
for(int i=1;i<=n;i++)
cin>>r[i];
tot=0;
for(int i=1;i<=n;i++)
v[++tot]=l[i],v[++tot]=r[i];
sort(v+1,v+tot+1);
tot=unique(v+1,v+tot+1)-v-1;
int ans=0;
v[tot+1]=v[tot]+1;
for(int i=1;i<=tot;i++)
ans=(ans+calc(v[i],v[i+1]-1))%MOD;
cout<<ans<<"\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
init(150);
int T;
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: 3584kb
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:
120 140 288 999976897 128 80 70 274 192 4
result:
wrong answer 1st lines differ - expected: '180', found: '120'