QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#751038 | #9631. Median Replacement | guodong | WA | 64ms | 4236kb | C++17 | 5.7kb | 2024-11-15 16:49:32 | 2024-11-15 16:49:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Mod = 1e9 + 7;
int inv(int b,int p = Mod - 2){
int ans = 1;
while(p){
if(p & 1)
ans = ans * b % Mod;
b = b * b % Mod;
p >>= 1;
}
return ans;
}
class poly{
public:
vector<int> f;
poly(){}
poly(vector<int> _f){
f = _f;
}
unsigned int size(){
return f.size();
}
vector<int> getf(){
return f;
}
};
poly operator + (poly a, poly b){
if(a.size() < b.size()) a.f.resize(b.size());
for(int i = 0; i < b.size(); ++i)
a.f[i] += b.f[i], a.f[i] %= Mod;
return a;
}
poly operator * (poly a,const int &x){
if(x == 1)
return a;
for(int i = 0 ; i < a.size(); ++i)
a.f[i] = a.f[i] * x % Mod;
return a;
}
poly operator * (poly a, poly b){
if(a.f.empty() || b.f.empty()) {
return poly(vector<int>{0});
}
int rlen = a.f.size() + b.f.size() -1 ;
vector<int> ret(rlen);
for(int i = 0 ; i < a.size(); ++i)
for(int j = 0 ; j < b.size(); ++j)
ret[i + j] += a.f[i] * b.f[j] % Mod, ret[i + j] %= Mod;
return poly(ret);
}
int Pow[152][152];
class powSum{
public:
int power;
vector<int> ret;
powSum(int _power){
vector<int> x,y;
power = _power;
x.resize(_power + 2);
y.resize(_power + 2);
for(int i = 0; i < _power + 2; ++i){
x[i] = i;
y[i] = (i != 0 ? y[i-1] + Pow[i][power] : 0) % Mod;
}
vector<int> sum(_power + 2);
ret.resize(_power + 2,0);
ret[0] = y[0],sum[0] = 1;
int n = _power + 2;
for(int i = 1; i < n; ++i){
for(int j = n - 1; j >= i; --j)
y[j] = (y[j] - y[j - 1]) * inv(x[j] - x[j - i]) % Mod;
for(int j = i; j; --j){
sum[j] = -sum[j] * x[i-1] % Mod + sum[j-1];
sum[j] %= Mod;
ret[j] += sum[j] * y[i] % Mod;
ret[j] %= Mod;
}
sum[0] = -sum[0] * x[i - 1] % Mod;
ret[0] += sum[0] * y[i] % Mod;
ret[0] %= Mod;
}
}
int getSum(int r){
int now = 1;
int ans = 0;
for(int i = 0; i < power + 2; ++i){
ans += now * ret[i] % Mod;
now = now * r % Mod;
ans %= Mod;
}
return (ans % Mod + Mod) % Mod;
}
int calc(int l,int r){
return (getSum(r) - getSum(l - 1) + Mod) % Mod;
}
};
signed main()
{
#ifdef NICEGUODONG
freopen("data.in","r",stdin);
#endif
for(int i = 1; i <= 151; ++i){
Pow[i][0] = 1;
for(int j = 1; j <= 151; ++j){
Pow[i][j] = Pow[i][j-1] * i % Mod;
}
}
vector<powSum> ps;
for(int i = 0; i <= 150; ++i)
ps.emplace_back(powSum(i));
int T;
cin >> T;
while(T--){
vector<int> secPoint;
int n,all = 1;
cin >> n;
vector<pair<int,int>> range(n + 1);
for(int i = 1; i <= n; ++i){
cin >> range[i].first;
}
for(int i = 1; i <= n; ++i){
cin >> range[i].second;
all = all * (range[i].second - range[i].first + 1) % Mod;
secPoint.emplace_back(range[i].first);
secPoint.emplace_back(range[i].second);
}
secPoint.push_back(1);
sort(secPoint.begin(),secPoint.end());
secPoint.resize(unique(secPoint.begin(),secPoint.end()) - secPoint.begin());
vector<pair<int,int>> sec;
for(int i = 0; i < secPoint.size(); ++i){
if(i + 1 < secPoint.size() && secPoint[i] + 1 <= secPoint[i + 1] - 1)
sec.emplace_back(secPoint[i] + 1,secPoint[i + 1] - 1);
sec.emplace_back(secPoint[i],secPoint[i]);
}
int ans = 0;
for(int x = 0; x < sec.size(); ++x){
int l = sec[x].first,r = sec[x].second;
vector<int> coe(n + 1);
vector<poly> one(n + 1);
vector<poly> zero(n + 1);
coe[0] = 1;
for(int i = 1; i <= n; ++i){
coe[i] = coe[i - 1] * (range[i].second - range[i].first + 1) % Mod;
if(l > range[i].second){
zero[i] = poly(vector<int>{range[i].second - range[i].first + 1});
one[i] = poly(vector<int>{0});
}
else if (r < range[i].first){
one[i] = poly(vector<int>{range[i].second - range[i].first + 1});
zero[i] = poly(vector<int>{0});
}
else{
zero[i] = poly(vector<int>({-range[i].first,1}));
one[i] = poly(vector<int>({range[i].second + 1, -1}));
}
}
vector<vector<poly>> Dp(n + 1,vector<poly>(3,poly(vector<int>{0})));
Dp[1][0] = one[1];
Dp[1][2] = zero[1];
for(int i = 2; i <= n; ++i){
Dp[i][2] = zero[i] * (Dp[i-1][2] + Dp[i-1][1]);
Dp[i][1] = zero[i] * Dp[i-1][0];
Dp[i][0] = one[i] * (Dp[i-1][2]);
}
poly sum = Dp[n][0] + Dp[n][1] + Dp[n][2];
ans += all * (r - l + 1);
for(int i = 0; i < sum.f.size(); ++i){
ans -= sum.f[i] * ps[i].calc(l,r) % Mod;
ans %= Mod;
}
}
cout << ans << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 64ms
memory: 4016kb
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: 64ms
memory: 4008kb
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: 64ms
memory: 4236kb
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: 64ms
memory: 3948kb
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 539867046 711272106 123881231 497944943 9791579 -460987748 963879245 315607794 495624077
result:
wrong answer 7th lines differ - expected: '539012259', found: '-460987748'