题解:CF2204F Sum of Fractions
::::info[引理
对于两个数
- 当
1 < x 时,\forall \ a \ge 1 ,显然有\frac{a}{x-1} - \frac{a}{x} \ge \frac{a}{y-1} - \frac{a}{y} ,所以如果一次操作选择减分母,肯定是选择减分母最小的更优。 -
若一次操作选择减分母,则分母最小的数不会变化;若一次操作选择加分子,分母最小的数也不会变化。
| 综上所述,该引理成立。 |
|---|
| ::::info[引理 |
| 设操作次数为 |
首先给出引理:若
我们以
图中
| 综上所述,该引理成立。 |
|---|
| 下文中 |
O(n ^ 3 \times m \times \log mod) 做法
Solution
有了上面这两条引理,我们可以想到一个
设
首先,若一个序列内有多个最小值,我们钦定第一个出现的最小值为被操作的数,可以证明,这样不影响最终答案。
枚举
- 若
(a_i = (\displaystyle\min_{{pos} = l}^{r} a_{pos})) \wedge ((\displaystyle\min_{{pos} = l}^{i - 1} a_{pos})) > a_i) ,则当前点为区间[l,r] 中被操作的点,若a_i > k_j ,则ans_j 加上\frac{k_j + 1}{a_i} ;否则ans_j 加上k_j - a_i + 2 。 - 否则
ans_j 加上a_i 。 -
再用快速幂将所有除法转为乘逆元,时间复杂度
O(n ^ 3 \times m \times \log mod) 。O(n \times m \times \log mod) 做法Solution
我们思考如何优化上一个做法。
我们发现可以计算出一个点在几个区间内是被操作的点,这样就不用枚举
用快速幂将所有除法转为乘逆元,时间复杂度
#include<bits/stdc++.h>
#define ztyroy_AK_CF_2204
#define hyc_qcz_jmr_baoyouwo_AC
#define made_by_ztyroy return 0;
#define int long long
#define rep(i,f,s) for(int i=f;i<=s;i++)
#define fep(i,f,s) for(int i=f;i<s;i++)
#define per(i,f,s) for(int i=f;i>=s;i--)
#define pef(i,f,s) for(int i=f;i>s;i--)
namespace FastIO{
template <typename T> inline void read(T &x){
x=0;
T f=1;
T c=getchar();
for(;!isdigit(c);c=getchar()){
if(c=='-'){
f=-1;
}
}
for(;isdigit(c);c=getchar()){
x=(x<<1)+(x<<3)+(c^48);
}
x*=f;
}
template <typename T> void print(T x){
if(x<0){
x=-x;
putchar('-');
}
if(x>9){
print(x/10);
}
putchar((x%10)^48);
}
}
using namespace std;
using namespace FastIO;
struct node{
int l;
int r;
int a;
}p[500005];
int n,m,k[500005],ans[500005],thi;
const int mod=998244353;
stack<pair<int,int> >st;
int my_pow(int d,int z){
int res=1;
while(z>0){
if((z&1)==1){
res*=d;
res%=mod;
}
d*=d;
d%=mod;
z>>=1;
}
return res;
}
signed main(){
ztyroy_AK_CF_2204
hyc_qcz_jmr_baoyouwo_AC
read(n);
read(m);
rep(i,1,n){
read(p[i].a);
}
rep(i,1,m){
read(k[i]);
}
st.push({0,0});
rep(i,1,n){
while(st.top().first>p[i].a){
st.pop();
}
p[i].l=st.top().second+1;
st.push({p[i].a,i});
}
while(!st.empty()){
st.pop();
}
st.push({0,n+1});
per(i,n,1){
while(st.top().first>=p[i].a){
st.pop();
}
p[i].r=st.top().second-1;
st.push({p[i].a,i});
}
rep(i,1,n){
int fir=(i-p[i].l+1)*(p[i].r-i+1)%mod;
int sec=(p[i].l-1)*(n-i+1)%mod+i*(n-p[i].r)%mod-(p[i].l-1)*(n-p[i].r)%mod+mod;
thi+=sec*my_pow(p[i].a,mod-2)%mod;
thi%=mod;
rep(j,1,m){
if(p[i].a>k[j]){
ans[j]+=(k[j]+1)*my_pow(p[i].a,mod-2)%mod*fir%mod;
ans[j]%=mod;
}
else{
ans[j]+=(k[j]-p[i].a+2)*fir%mod;
ans[j]%=mod;
}
}
}
rep(i,1,m){
ans[i]+=thi;
ans[i]%=mod;
print(ans[i]);
putchar('\n');
}
made_by_ztyroy
}
::::
O(n \times \log mod + m) 做法
Solution
我们考虑用前缀和优化。
因为
#include<bits/stdc++.h>
#define ztyroy_AK_CF_2204
#define hyc_qcz_jmr_baoyouwo_AC
#define made_by_ztyroy return 0;
#define int long long
#define rep(i,f,s) for(int i=f;i<=s;i++)
#define fep(i,f,s) for(int i=f;i<s;i++)
#define per(i,f,s) for(int i=f;i>=s;i--)
#define pef(i,f,s) for(int i=f;i>s;i--)
namespace FastIO{
template <typename T> inline void read(T &x){
x=0;
T f=1;
T c=getchar();
for(;!isdigit(c);c=getchar()){
if(c=='-'){
f=-1;
}
}
for(;isdigit(c);c=getchar()){
x=(x<<1)+(x<<3)+(c^48);
}
x*=f;
}
template <typename T> void print(T x){
if(x<0){
x=-x;
putchar('-');
}
if(x>9){
print(x/10);
}
putchar((x%10)^48);
}
}
using namespace std;
using namespace FastIO;
struct node{
int l;
int r;
int a;
}p[500005];
int n,m,k[500005],ans[500005],thi,s[3][500005];
const int mod=998244353;
stack<pair<int,int> >st;
int my_pow(int d,int z){
int res=1;
while(z>0){
if((z&1)==1){
res*=d;
res%=mod;
}
d*=d;
d%=mod;
z>>=1;
}
return res;
}
signed main(){
ztyroy_AK_CF_2204
hyc_qcz_jmr_baoyouwo_AC
read(n);
read(m);
rep(i,1,n){
read(p[i].a);
}
rep(i,1,m){
read(k[i]);
}
st.push({0,0});
rep(i,1,n){
while(st.top().first>p[i].a){
st.pop();
}
p[i].l=st.top().second+1;
st.push({p[i].a,i});
}
while(!st.empty()){
st.pop();
}
st.push({0,n+1});
per(i,n,1){
while(st.top().first>=p[i].a){
st.pop();
}
p[i].r=st.top().second-1;
st.push({p[i].a,i});
}
rep(i,1,n){
int fir=(i-p[i].l+1)*(p[i].r-i+1)%mod;
int sec=(p[i].l-1)*(n-i+1)%mod+i*(n-p[i].r)%mod-(p[i].l-1)*(n-p[i].r)%mod+mod;
thi+=sec*my_pow(p[i].a,mod-2)%mod;
thi%=mod;
int id=lower_bound(k+1,k+m+1,p[i].a)-k;
s[0][1]+=fir*my_pow(p[i].a,mod-2)%mod;
s[0][1]%=mod;
s[0][id]-=fir*my_pow(p[i].a,mod-2)%mod;
s[0][id]+=mod;
s[0][id]%=mod;
s[1][id]+=fir;
s[1][id]%=mod;
s[2][id]-=(p[i].a-2)*fir%mod;
s[2][id]+=mod;
s[2][id]%=mod;
}
rep(i,1,m){
s[0][i]+=s[0][i-1];
s[0][i]%=mod;
s[1][i]+=s[1][i-1];
s[1][i]%=mod;
s[2][i]+=s[2][i-1];
s[2][i]%=mod;
ans[i]+=thi;
ans[i]%=mod;
ans[i]+=(k[i]+1)*s[0][i]%mod;
ans[i]%=mod;
ans[i]+=k[i]*s[1][i]%mod;
ans[i]%=mod;
ans[i]+=s[2][i]%mod;
ans[i]%=mod;
print(ans[i]);
putchar('\n');
}
made_by_ztyroy
}
::::