题解:CF2204F Sum of Fractions

· · 题解

::::info[引理 1 证明] 首先给出引理:对于任意一个序列,最多对一个数进行操作,并且被操作的数是该序列中最小的数。

对于两个数 x 和 y,我们不妨设 1 \le x < y \le 10^8,则:

若一次操作选择减分母,则分母最小的数不会变化;若一次操作选择加分子,分母最小的数也不会变化。

综上所述,该引理成立。
::::info[引理 2 证明]
设操作次数为 k,被操作的数的分母为 w。

首先给出引理:若 k < w,则只加分子;否则先将分母减到 1,然后全加分子。

我们以 w=10 为例:

图中 x 轴表示操作次数,y 轴表示分数的值。\ 蓝线表示只加分子的值,简称为 y_b。\ 红线表示将分母减到 1 前只减分母的值,简称为 y_r。\ 绿线表示在分母减到 1 后只加分子的值,简称为 y_g。

综上所述,该引理成立。
下文中 mod 值默认为 998244353。

O(n ^ 3 \times m \times \log mod) 做法

Solution

有了上面这两条引理,我们可以想到一个 O(n ^ 3 \times m \times \log mod) 的做法。

设 ans_j 表示 \left( \sum\limits_{l = 1}^{n}{\sum\limits_{r = l}^{n}\mathrm{MSF}(a[l \dots r], k_j)} \right) \bmod 998244353。

首先,若一个序列内有多个最小值,我们钦定第一个出现的最小值为被操作的数,可以证明,这样不影响最终答案。

枚举 l 和 r,枚举操作次数 k_j,枚举 i \in [l,r]:

我们发现可以计算出一个点在几个区间内是被操作的点,这样就不用枚举 l 和 r 了,我们预处理出第 i 个点左边第一个满足 (\displaystyle\min_{pos = j}^{i-1} a_{pos} > a_i) 的 j 和右边最后一个满足 (\displaystyle\min_{pos = i+1}^{j} a_{pos} \ge a_i) 的 j,分别记为 nl 和 nr,那么第 i 个点在 (i - nl_i + 1) \times (nr_i - i + 1) 个区间内是被操作的点,在 (nl_i - 1) \times (n - i + 1) + i \times (n - nr_i) - (nl_i - 1) \times (n - nr_i) 个区间内不是被操作的点,我们分别记为 fir 和 sec,再遍历 k_j 依次增加即可。

用快速幂将所有除法转为乘逆元,时间复杂度 O(n \times m \times \log mod)。 ::::info[Code]

#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

我们考虑用前缀和优化。

因为 k 数组有单调性,对于每一个 a_i,我们找到第一个大于等于 a_i 的 k_{id},对于 j \in [1,id) 和 j \in [id,m],分别处理即可。 ::::info[Code]

#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
}

::::