QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#737710 | #9631. Median Replacement | hezlik# | WA | 0ms | 3820kb | C++20 | 3.4kb | 2024-11-12 16:40:32 | 2024-11-12 16:40:36 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long int64;
typedef long double db;
const int N=150,mod=1000000007;
int inv[N+9],fac[N+9],ifac[N+9];
void Init(int n){
inv[1]=1;
fac[0]=fac[1]=1;
ifac[0]=ifac[1]=1;
for (int i=2;i<=n;++i){
inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
fac[i]=1LL*fac[i-1]*i%mod;
ifac[i]=1LL*ifac[i-1]*inv[i]%mod;
}
}
int Lagrange(const std::vector<int> &val,int x){
int n=val.size();
std::vector<int> p(n),s(n);
p[0]=x%mod;
for (int i=1;i<n;++i)
p[i]=1LL*p[i-1]*(x+mod-i)%mod;
s[n-1]=(x+mod-(n-1))%mod;
for (int i=n-2;i>=0;--i)
s[i]=1LL*s[i+1]*(x+mod-i)%mod;
int res=0;
for (int i=0;i<n;++i){
int t=1LL*val[i]*ifac[i]%mod*ifac[n-i-1]%mod;
if (i) t=1LL*t*p[i-1]%mod;
if (i+1!=n) t=1LL*t*s[i+1]%mod;
res=((n-i)&1?(res+t):(res+mod-t))%mod;
}
return res;
}
int n;
std::pair<int,int> a[N+9];
std::vector<int> ord;
void Read(){
std::cin>>n;
ord.clear();
for (int i=1;i<=n;++i){
std::cin>>a[i].first;
--a[i].first;
ord.push_back(a[i].first);
}
for (int i=1;i<=n;++i){
std::cin>>a[i].second;
ord.push_back(a[i].second);
}
ord.push_back(0);
}
// void upd(const int &x,const int &y,int &z){z=(z+1LL*x*y)%mod;}
int dp[N+9][3],res[N+9];
int DP(int pl,int pr,int x){
// std::cerr<<" "<<pl<<' '<<pr<<' '<<x<<'\n';
// std::vector<std::vector<int>> dp(n+1,std::vector<int>({0,0,0}));
// std::vector<int> res(n+1);
// auto upd=[&](int x,int y,int &z){z=(z+1LL*x*y)%mod;};
// for (int i=0;i<=n;++i){
// for (int j=0;j<3;++j) dp[i][j]=0;
// res[i]=0;
// }
dp[0][0]=1;dp[0][1]=dp[0][2]=res[0]=0;
for (int i=1;i<=n;++i){
// std::cerr<<" "<<i<<'\n';
int c[2];
auto [l,r]=a[i];
if (r<=pl){
c[0]=r-l;
c[1]=0;
}else if (l>=pr){
c[0]=0;
c[1]=r-l;
}else{
c[0]=x-1-l;
c[1]=r-x+1;
}
// std::cerr<<" "<<i<<' '<<l<<' '<<r<<'\n';
// upd(dp[i-1][0],c[0],dp[i][0]);
// upd(dp[i-1][0],c[1],dp[i][1]);
// upd(dp[i-1][1],c[0],dp[i][2]);
// upd(dp[i-1][1],c[1],res[i]);
// upd(dp[i-1][2],c[0],dp[i][0]);
// upd(dp[i-1][2],c[1],res[i]);
// upd(res[i-1],(c[0]+c[1])%mod,res[i]);
dp[i][0]=(1LL*dp[i-1][0]*c[0]+1LL*dp[i-1][2]*c[0])%mod;
dp[i][1]=(1LL*dp[i-1][0]*c[1])%mod;
dp[i][2]=(1LL*dp[i-1][1]*c[0])%mod;
res[i]=(1LL*dp[i-1][1]*c[1]+1LL*dp[i-1][2]*c[1]+1LL*res[i-1]*(c[0]+c[1]))%mod;
// std::cerr<<" "<<i<<' '<<l<<' '<<r<<'\n';
}
// std::cerr<<" "<<pl<<' '<<pr<<' '<<x<<" : "<<res[n]<<'\n';
return res[n];
}
int Solve(int l,int r){
std::vector<int> val(n+3);
for (int i=0;i<n+3;++i){
val[i]=DP(l,r,i);
if (i) val[i]=(val[i]+val[i-1])%mod;
}
// return val[0];
return (Lagrange(val,r)-Lagrange(val,l)+mod)%mod;
}
void Solve(){
std::sort(ord.begin(),ord.end());
ord.erase(std::unique(ord.begin(),ord.end()),ord.end());
// std::cerr<<" ord :";
// for (int p:ord)
// std::cerr<<' '<<p;
// std::cerr<<'\n';
int ans=0;
for (int i=0;i+1<(int)ord.size();++i)
ans=(ans+Solve(ord[i],ord[i+1]))%mod;
std::cout<<ans<<'\n';
}
void work(){
Read();
Solve();
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t=1;
std::cin >> t;
Init(N+7);
while (t--) {
work();
}
}
/*
2
3
1 1 1
1 1 1
3
1 1 1
1 2 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3508kb
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: 3820kb
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: 3556kb
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: 0ms
memory: 3616kb
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:
157838571 244899778 711272106 123881231 497944943 9791579 539012259 963879245 315607794 495624077
result:
wrong answer 2nd lines differ - expected: '539867046', found: '244899778'