题解:P14605 [NWRRC 2025] Faulty Fraction
Bill_luogu
·
2025-11-29 20:03:34
·
题解
设 \text{len(s)} 表示的是 s 的长度,则 \text{len(114514)=6,len(100)=3} 。
题目分析:
理解题目后,发现是一道很简单的数学题,只需在 s 中枚举除数即可,用高精度进行计算。一看数据范围:1\le \text{len(s)}\le10^5 ,而高精度乘法时间复杂度为 O(\text{len(s)}^2) ,用在本题时间复杂度为 O((10^5)^2)=O(10^{10}) ,无法通过此题。
正解思考:
思考高精度中乘法结果位数的计算,当 b\times c 的最高位不进位时,\text{len(a)}=\text{len(b)}+\text{len(c)}-1 ,有进位时,\text{len(a)}=\text{len(b)}+\text{len(c)} 。
现在我们已知 \text{len(s)} 并且已知 \text{len(a)}+\text{len(b)}=\text{len(s)}\to \text{len(a)}=\text{len(s)}-\text{len(b)}\in\set{\text{len(b)}+\text{len(c)}+1,\text{len(b)}+\text{len(c)}} ,即 \text{len(b)}=(\text{len(s)}-\text{len(c)}+1)\div2 或 \text{len(b)}=(\text{len(s)}-\text{len(c)})\div2 。当 \text{len(s)}-\text{len(c)} 为偶数时,两者均可,当 \text{len(s)}-\text{len(c)} 为奇数时,因为除法向下取整,所以只有前者才能输出正确答案,即 \text{len(b)}=\lfloor\frac{\text{len(s)}-\text{len(c)}+1}{2}\rfloor 。
AC代码:
#include<iostream>
#include<cstring>
using namespace std;
string a,b;
int x;
int len1,len2;
int main()
{
cin>>a>>b;
len1=a.size();
len2=b.size();
x=(len1-len2+1)/2;//取 b 的长度
cout<<a.substr(0,len1-x)<<' '<<a.substr(len1-x); //前面的是 a,后面的是 b
return 0;
}
绿钩后的第一篇题解,求过