跳到正文

题解

题解:P13831 【MX-X18-T3】「FAOI-R6」比亚多西

Berd__约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年8月24日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 6 分钟
题目
P13831

版本

  1. v1当前

P13813 【MX-X18-T3】「FAOI-R6」比亚多西 题解

题意理解

题面里说的很清楚了,就是让你模拟猜数字的过程并记录步数,但是你要枚举不同的数字上限和猜的数字然后输出总步数 mod⁡998244353\operatorname{mod} 998244353 的结果。

思路

我们没有思路,怎么办呢?打表!

进行 100100 以内的打表后可得(注意观察高亮部分):

打表结果
C++
x=1,f(x)=1
x=2,f(x)=3//+2
x=3,f(x)=5//+2
x=4,f(x)=8//+3
x=5,f(x)=11//+3
x=6,f(x)=14//+3
x=7,f(x)=17//+3
x=8,f(x)=21//+4
x=9,f(x)=25//+4
......
x=15,f(x)=49//+4
x=16,f(x)=54//+5
x=17,f(x)=59//+5
x=18,f(x)=64//+5
x=19,f(x)=69//+5
......
x=29,f(x)=119//+5
x=30,f(x)=124//+5
x=31,f(x)=129//+5
x=32,f(x)=135//+6
x=33,f(x)=141//+6
x=34,f(x)=147//+6
......
x=60,f(x)=303//+6
x=61,f(x)=309//+6
x=62,f(x)=315//+6
x=63,f(x)=321//+6
x=64,f(x)=328//+7
x=65,f(x)=335//+7
x=66,f(x)=342//+7
......
x=100,f(x)=580//+7

发现每当 x=2n−1(n∈N)x=2^{n}-1(n\in \mathbb N) 时本项与下一项的差会加1,这便是我们的入手点。

推柿子

我们把这项规律总结为数学柿子(式子)可得:

f(x)=(∑i=1⌊log⁡2(x+1)⌋2(i−1)⋅i) + ⌈log⁡2(x+1)⌉⋅(x−2⌊log⁡2(x+1)⌋+1)\large f(x)=\bigg(\sum_{i=1}^{\lfloor\log_2{(x+1)}\rfloor} 2^{(i-1)} \cdot i\bigg)\space+\space \lceil\log_2 (x+1)\rceil\cdot(x-2^{\lfloor\log_2{(x+1)}\rfloor}+1)

那么现在我们需要简化这个柿子:

首先,这个柿子等价于:

f(x−1)=(∑i=1⌊log⁡2x⌋2(i−1)⋅i) + ⌈log⁡2x⌉⋅(x−2⌊log⁡2x⌋)\large f(x-1)=\bigg(\sum_{i=1}^{\lfloor\log_2{x}\rfloor} 2^{(i-1)} \cdot i\bigg)\space+\space \lceil\log_2 x\rceil\cdot(x-2^{\lfloor\log_2 x\rfloor})

若我们设 h=log⁡2xh=\log_2 x,

=(∑i=1⌊h⌋2(i−1)⋅i) + ⌈h⌉⋅(x−2⌊h⌋)\large=\bigg(\sum_{i=1}^{\lfloor h\rfloor} 2^{(i-1)} \cdot i\bigg)\space+\space \lceil h\rceil\cdot(x-2^{\lfloor h \rfloor})

发现上取整、下取整可以舍去,

=(∑i=1h2(i−1)⋅i) + (h+1)⋅(x−2h)\large=\bigg(\sum_{i=1}^{h} 2^{(i-1)} \cdot i\bigg)\space+\space (h+1)\cdot(x-2^{h})

接下来,最重要的一步,使用我们小学二年级就学过的数列知识,设 Sh=∑i=1h2(i−1)⋅i\large S_h=\sum_{i=1}^{h} 2^{(i-1)} \cdot i,

注意到 2Sh=∑i=1h2i⋅i,Sh−2Sh=−Sh=2n−1−2n⋅n\large2S_h=\sum_{i=1}^{h} 2^i\cdot i,S_h-2S_h=-S_h=2^n-1-2^n\cdot n,

所以 Sh=2n⋅(n−1)+1\large S_h=2^n\cdot(n-1)+1。

回归主线,把 ShS_h 代入得,

f(x−1)=2h⋅(h−1)+1+(h+1)⋅(x−2h)                     =(h−1)⋅(x−2h+2h)+2(x−2n)+1=(h+1)⋅x−2h+1+1       \large f(x-1)=2^h\cdot(h-1)+1+(h+1)\cdot(x-2^h)\\ \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=(h-1)\cdot (x-2^h+2^h)+2(x-2^n)+1\\ =(h+1)\cdot x-2^{h+1}+1\space\space\space\space\space\space\space

这里应该很容易看出来怎么求了吧,分段枚举 hh,累计和即可。

具体细节看代码中的注释即可。

代码

切勿复制粘贴,仅供参考
C++
#include<bits/stdc++.h>
#define endl "\n"
#define int long long
#define qrw ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int mod=998244353;
const int inv2=499122177;//2在%998244353意义下的乘法逆元
signed main() {
    qrw;
    int T;
    cin>>T;
    while (T--){
        int l,r;
        l=r=100-T;
        int ans=0;
        for(int h=1;h<=60;h++){
            int lw=1ll<<(h-1),hi=(1ll<<h)-1;//需要计算的n范围(2^(h-1)~2^h-1)
            if(lw>r) break;//如果最小的n都大于r那么break
            if(hi<l) continue;//同上

            int l0=max(l,lw);//取max,避免多算
            int r0=min(r,hi);//同上
            if (l0>r0) continue;//避免无用计算
            int len=r0-l0+1;//区间长度

            int S1=((l0+r0+2)%mod)*(len%mod)%mod;//式子中的内容
            S1=S1*inv2%mod;//使用逆元防溢出
            int t1=(h%mod)*S1%mod;//临时结果1
            int t2=(((1ll<<h)-1)%mod)*(len%mod)%mod;//临时结果2
            int c=(t1-t2+mod)%mod;//本段和
            ans=(ans+c)%mod;//累加ans
        }
        cout<<"x="<<l<<",f(x)="<<ans<<endl;//输出
    }
    return 0;
}
原文