题解:P13831 【MX-X18-T3】「FAOI-R6」比亚多西
P13813 【MX-X18-T3】「FAOI-R6」比亚多西 题解
题意理解
题面里说的很清楚了,就是让你模拟猜数字的过程并记录步数,但是你要枚举不同的数字上限和猜的数字然后输出总步数
思路
我们没有思路,怎么办呢?打表!
进行
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
::::
发现每当
推柿子
我们把这项规律总结为数学柿子(式子)可得:
那么现在我们需要简化这个柿子:
首先,这个柿子等价于:
若我们设
发现上取整、下取整可以舍去,
接下来,最重要的一步,使用我们小学二年级就学过的数列知识,设
注意到
所以
回归主线,把
这里应该很容易看出来怎么求了吧,分段枚举
具体细节看代码中的注释即可。
代码
::::warning[切勿复制粘贴,仅供参考]
#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;
}
::::