QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#750691 | #9631. Median Replacement | guodong | WA | 64ms | 4244kb | C++17 | 5.3kb | 2024-11-15 15:30:10 | 2024-11-15 15:30:11 |
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 < a.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()) assert(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;
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;
secPoint.emplace_back(range[i].first);
secPoint.emplace_back(range[i].second);
}
secPoint.push_back(*min_element(secPoint.begin(),secPoint.end()) - 1);
sort(secPoint.begin(),secPoint.end());
secPoint.resize(unique(secPoint.begin(),secPoint.end()) - secPoint.begin());
int ans = 0;
for(int i = 0; i + 1 < secPoint.size(); ++i){
int l = secPoint[i] + 1,r = secPoint[i + 1];
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>(1,range[i].second - range[i].first + 1));
one[i] = poly(vector<int>{0});
}
else if (r < range[i].first){
one[i] = poly(vector<int>(1,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}));
}
}
poly accum = poly(vector<int>(1,1));
poly sum;
for(int i = n; i >= 3 ; --i){
poly lst = one[i] * (one[i-1] * one[i-2]) + one[i] * (zero[i-1] * one[i-2]) + one[i] * (one[i-1] * zero[i - 2]);
sum = sum + (lst * coe[i - 3] * accum);
accum = accum * zero[i];
}
for(int i = 0; i < sum.f.size(); ++i){
ans += sum.f[i] * ps[i].calc(r,l) % Mod;
ans %= Mod;
}
}
cout << ans << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 64ms
memory: 4244kb
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 60 194 53 132 90 120 168 192 97
result:
wrong answer 2nd lines differ - expected: '170', found: '60'