跳到正文

个人记录

「HIOI & λγOI」Round 1-Magic Stone 题解

Isshiki·Iroha约 19 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 19 分钟

版本

  1. v1当前

Subtask 0

样例把答案告诉你了。

Subtask 1

直接爆搜即可,期望得分 55 分,复杂度: O(T×nk)\text{O}(T\times n^k)。

Subtask 2

记 SS 表示小柴可以选的魔法元素,记小柴选的魔法元素序列为 ai{a_i},那么答案就是

∑a1∈S∑a2∈S⋯∑am∈S⏞mgcd⁡i=1mai\overbrace{\sum_{a_1\in S}\sum_{a_2\in S}\cdots\sum_{a_m\in S}}^{m}\gcd^m_{i=1}a_i

我们考虑递推来求,即小柴放了前 ii 个时的值怎么递推出来的。我们发现,前面 i−1i-1 个选了哪些是不重要的,我们只关心他们的 gcd⁡\gcd 值,我们可以记录下来放了前 ll 个 gcd⁡\gcd 值为 qq 的方案有多少种,记为 dp[q][l]dp[q][l],那么对于 l←l+1l\leftarrow l+1,对于每个 qq 和可以选的 ii,即 dp[i][1]=1\text{dp}[i][1]=1,都有

dp[gcd⁡(q,i)][l+1]+=dp[q][l]×dp[i][1]\text{dp}[\gcd(q,i)][l+1]+=\text{dp}[q][l]\times \text{dp}[i][1]

那么最后的答案就是:

∑i=1ndp[i][m]×i\sum_{i=1}^n\text{dp}[i][m]\times i

复杂度:O(Tn2m)\text{O}(Tn^2m)。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
using lll = __int128;
using ll = long long;
namespace Mashiro {
    char buf[1<<18],*p1=buf,*p2=buf;
    inline int getc() {
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline void read(T& x) {
        x=0;int f=1;
        char ch=getc();
        while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
        while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
        x*=f;
    }
    template <typename T,typename... Args> inline void read(T& x, Args&... args) {
        read(x);
        read(args...);
    }
    char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
    inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
    inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
    template<typename T>inline void write(T x) {
        static int buf[40],top=0;
        if(x<0)putc('-'),x=~x+1;
        while(x)buf[++top]=x%10,x/=10;
        if(top==0)buf[++top]=0;
        while (top) putc(buf[top--]^48);
        putc(' ');
        flush();
     }
     template <typename T,typename... Args> inline void write(T x, Args... args) {
         write(x);
         write(args...);
     }
}
using namespace Mashiro;
const ll Mod=998244353;
int n,T,m,k;
ll cnt[1005][205];
int ggcd[1005][1005];
int Gcd(int x,int y){
    if(ggcd[x][y])return ggcd[x][y];
    else if(y==0)return ggcd[x][y]=x;
    else return ggcd[y][x]=ggcd[x][y]=Gcd(y,x%y);
}
ll ans[maxn];
int main(){
    read(n,T,k);
    for(int i(1);i<=n;++i)cnt[i][1]=1;
    for(int i(1),t;i<=k;++i){
        read(t);
        cnt[t][1]=0;
    }
    for(int l(2);l<=205;++l){
        for(int i(1);i<=n;++i){
            for(int j(1);j<=n;++j){
                cnt[Gcd(i,j)][l]=(cnt[Gcd(i,j)][l]+cnt[i][l-1]*cnt[j][1]%Mod)%Mod;
            }
        }
    }
    while(T--){
        read(m);
        ll ans=0;
        for(int i(1);i<=n;++i){
            ans=(ans+cnt[i][m]*i%Mod)%Mod;
        }
        write(ans);
    }

    return 0;
}

Subtask 3

我们改变一下 dp 顺序,我们固定 dp[gcd⁡(q,i)][l+1]←dp[q][l]+dp[gcd⁡(q,i)][l+1]\text{dp}[\gcd(q,i)][l+1]\leftarrow \text{dp}[q][l]+\text{dp}[\gcd(q,i)][l+1] 里面的 gcd⁡(q,i)\gcd(q,i) 反过来枚举 i,qi,q。那么对于 l←l+1l\leftarrow l+1,对于每个 qq 和可以选的 ii,都有

dp[d][l+1]+=dp[q][l]×dp[i][1],gcd⁡(i,q)=d\text{dp}[d][l+1]+=\text{dp}[q][l]\times \text{dp}[i][1],\gcd(i,q)=d

我们改成 dd 的倍数形式:

dp[d][l+1]+=dp[q][l]×dp[i][1],gcd⁡(i,q)=d\text{dp}[d][l+1]+=\text{dp}[q][l]\times \text{dp}[i][1],\gcd(i,q)=d

这个是一个比较显然的容斥。现在我们先去掉 gcd⁡(i,q)=d\gcd(i,q)=d 的限制,改为 d∣gcd⁡(i,q)d|\gcd(i,q),那么显然,dp[k×d][l+1],k×d≤n\text{dp}[k\times d][l+1],k\times d \le n 的方案数也会被算进去,如果我们先知道了这些值,那么减去他们即可,注意到 k×d≥dk\times d \ge d,倒序枚举即可。

现在的问题变成了怎么求上面那个式子,我们把完整式子写出来:

dp[d][l+1]=∑dp[q][l]×dp[i][1],d∣gcd⁡(i,q)\text{dp}[d][l+1]=\sum\text{dp}[q][l]\times \text{dp}[i][1],d|\gcd(i,q)

我们枚举 dd 的倍数:

dp[d][l+1]=∑d1=1⌊nd⌋∑d1=2⌊nd⌋dp[d1×d][l]×dp[d2×d][1]\text{dp}[d][l+1]=\sum_{d_1=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{d_1=2}^{\left \lfloor \frac{n}{d} \right \rfloor}\text{dp}[d_1 \times d][l]\times \text{dp}[d_2\times d][1]

交换求和顺序:

dp[d][l+1]=∑d1=1⌊nd⌋dp[d1×d][l]∑d1=2⌊nd⌋×dp[d2×d][1]\text{dp}[d][l+1]=\sum_{d_1=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\text{dp}[d_1 \times d][l]\sum_{d_1=2}^{\left \lfloor \frac{n}{d} \right \rfloor}\times \text{dp}[d_2\times d][1]

我们记 f[i][l]=∑j=1⌊ni⌋dp[i][l]f[i][l]=\sum_{j=1}^{\left \lfloor \frac{n}{i} \right \rfloor} \text{dp}[i][l] ,用调和级数算计算 f[][l]f[][l] 的复杂度就是 O(nlog⁡n)\text{O}(n\log n)。

那么:

dp[d][l+1]=f[d][l]×f[d][1]\text{dp}[d][l+1]=f[d][l]\times f[d][1]

然后再减去 dp[k×d][l+1],k×d≤n\text{dp}[k\times d][l+1],k\times d \le n 即可。

复杂度:O(Tmnlog⁡n)\text{O}(Tmn\log n),期望得分:5555。

C++
#include<bits/stdc++.h>
using namespace std;
using lll = __int128;
using ll = long long;
namespace Mashiro {
    char buf[1<<18],*p1=buf,*p2=buf;
    inline int getc() {
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline void read(T& x) {
        x=0;int f=1;
        char ch=getc();
        while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
        while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
        x*=f;
    }
    template <typename T,typename... Args> inline void read(T& x, Args&... args) {
        read(x);
        read(args...);
    }
    char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
    inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
    inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
    template<typename T>inline void write(T x) {
        static int buf[40],top=0;
        if(x<0)putc('-'),x=~x+1;
        while(x)buf[++top]=x%10,x/=10;
        if(top==0)buf[++top]=0;
        while (top) putc(buf[top--]^48);
        putc(' ');
        flush();
     }
     template <typename T,typename... Args> inline void write(T x, Args... args) {
         write(x);
         write(args...);
     }
}
using namespace Mashiro;
const int maxn=1e5+10;
const int LogN=31;
const ll Mod=998244353;
int n,T,m,k;
ll cnt[3005][3005];
ll pre[3005][3005];
int ggcd[3005][3005];
int Gcd(int x,int y){
    if(ggcd[x][y])return ggcd[x][y];
    else if(y==0)return ggcd[x][y]=x;
    else return ggcd[y][x]=ggcd[x][y]=Gcd(y,x%y);
}
int main(){
    read(n,T,k);
    for(int i(1);i<=n;++i)cnt[i][1]=1;
    for(int i(1),t;i<=k;++i){
        read(t);
        cnt[t][1]=0;
    }
    for(int i(1);i<=n;++i){
        for(int j(1);j<=n/i;++j){
            pre[i][1]+=cnt[i*j][1];
        }
    }
    for(int l(2);l<=3000;++l){
        for(int i(n);i>=1;--i){
            cnt[i][l]=pre[i][l-1]*pre[i][1]%Mod;
            for(int j(2);j<=n/i;++j){
                cnt[i][l]=(cnt[i][l]-cnt[i*j][l]+Mod)%Mod;
            }
            for(int j(1);j<=n/i;++j){
                pre[i][l]=(cnt[i*j][l]+pre[i][l])%Mod;
            }
        }
    }
    while(T--){
        read(m);
        ll ans=0;
        for(int i(1);i<=n;++i){
            ans=(ans+cnt[i][m]*i%Mod)%Mod;
        }
        write(ans);
    }
    return 0;
}

Subtask 4

有了上面的启示,我们发现其实可以不用一个一个去推,因为有:

dp[d][l]=f[d][k]×f[d][n−k],k≤l\text{dp}[d][l]=f[d][k]\times f[d][n-k],k\le l

然后再容斥。

对于 mm 非常大,我们时间空间都经不住,我们考虑少存一些东西,一个比较好想的东西就是我们可以只存放了 2j2^j 个的,类似于倍增,那么记 dp[i][j]dp[i][j] 表示选了前 2j2^j 个,最大公约数为 ii 的方案数。转移和上面一样。

dp[d][l]=f2[d][l−1]−∑d1=1⌊nd⌋dp[k×d][l]\text{dp}[d][l]=f^2[d][l-1]-\sum_{d_1=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\text{dp}[k\times d][l] f[i][l]=∑j=1⌊ni⌋dp[i][l]f[i][l]=\sum_{j=1}^{\left \lfloor \frac{n}{i} \right \rfloor} \text{dp}[i][l]

计算的时候类似的开一个 Dp[],F[]Dp[],F[] 表示当前的值,然后做法参考倍增做法。

复杂度:O(Tnlog⁡nlog⁡m)\text{O}(Tn\log n\log m),期望得分:100100。

C++
#include<bits/stdc++.h>
using namespace std;
using lll = __int128;
using ll = long long;
namespace Mashiro {
    char buf[1<<18],*p1=buf,*p2=buf;
    inline int getc() {
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline void read(T& x) {
        x=0;int f=1;
        char ch=getc();
        while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
        while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
        x*=f;
    }
    template <typename T,typename... Args> inline void read(T& x, Args&... args) {
        read(x);
        read(args...);
    }
    char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
    inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
    inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
    template<typename T>inline void write(T x) {
        static int buf[40],top=0;
        if(x<0)putc('-'),x=~x+1;
        while(x)buf[++top]=x%10,x/=10;
        if(top==0)buf[++top]=0;
        while (top) putc(buf[top--]^48);
        putc(' ');
        flush();
     }
     template <typename T,typename... Args> inline void write(T x, Args... args) {
         write(x);
         write(args...);
     }
}
using namespace Mashiro;
const int maxn=1e5+10;
const int LogN=31;
const ll Mod=998244353;
int n,T,m,k;
ll dp[maxn][LogN],f[maxn][LogN];
ll Dp[maxn],F[maxn];
using namespace Misaka;
int main(){
    read(n,T,k);
    for(int i(1);i<=n;++i){
        dp[i][0]=1;
    }
    for(int i(1),t;i<=k;++i){
        read(t);
        dp[t][0]=0;
    }
    for(int i(1);i<=n;++i){
        for(int j(1);j<=n/i;++j){
            f[i][0]+=dp[j*i][0];
        }
    }
    for(int l(1);l<31;++l){
        for(int i(n);i>=1;--i){
            dp[i][l]=f[i][l-1]*f[i][l-1]%Mod;
            for(int j(2);j<=n/i;++j){
                dp[i][l]=(dp[i][l]-dp[j*i][l]+Mod)%Mod;
            }
            for(int j(1);j<=n/i;++j){
                f[i][l]=(f[i][l]+dp[i*j][l])%Mod;
            }
        }

    }
    while(T--){
        read(m);
        for(int i(30);~i;--i){
            if(m&(1<<i)){
                for(int j(1);j<=n;++j){
                    Dp[j]=dp[j][i];
                    F[j]=f[j][i];
                }
                m-=(1<<i);
                break;
            }
        }
        for(int j(30);~j;--j){
            if(!m)break;
            if(m&(1<<j)){
                for(int i(n);i>=1;--i){
                    Dp[i]=F[i]*f[i][j]%Mod;
                    for(int l(2);l<=n/i;++l){
                        Dp[i]=(Dp[i]-Dp[l*i]+Mod)%Mod;
                    }
                    F[i]=0;
                    for(int l(1);l<=n/i;++l){
                        F[i]=(F[i]+Dp[l*i])%Mod;
                    }
                }
            }
        }
        ll ans=0;
        for(int i(1);i<=n;++i){
            ans=(ans+Dp[i]*i%Mod)%Mod;
        }
        write(ans);
    }
    return 0;
}
代码
原文