版本比较
C++plus
正文
增加 799 行删除 492 行
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671685 行未改动1741751761771781791801811821831841851861871881891901911921931941951961971981992007 行未改动2082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383394 行未改动344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867## 前言起因:这些东西好麻烦。中间忘了,然后就有了它——`C++plus`。~~请不要叫它CP。~~功能无法做到完全优秀或正确,欢迎私信反馈。## 广告欢迎想加入的同学[私信作者](https://www.luogu.com.cn/chat?uid=1706930)。## 正文### 编译选项若无特别说明,下文采用的编译选项均为```bash-std=c++11 -O2```### 开始导入必须的库。:::info[导入库]```cpp#include <iostream>#include <vector>#include <string>#include <map>#include <algorithm>#include <sstream>#include <fstream>#include <windows.h>#include <cmath>#include <stdexcept>```:::### 高精度实现通过C++的高精模板与重载运算符实现。你可能会问 `frint` 是什么意思,因为FR是我的用户名缩写(:::info[高精度]```cppclass frint{private:str v;bool ng;struct C{float x,y;C(float _x=0.0,float _y=0.0):x(_x),y(_y){}C operator+(const C&b)const{return C(x+b.x,y+b.y);}C operator-(const C&b)const{return C(x-b.x,y-b.y);}C operator*(const C&b)const{return C(x*b.x-y*b.y,x*b.y+y*b.x);}};static void FFT(vector<C>&a,int inv){int n=a.size();for(int i=1,j=0;i<n;i++){int bit=n>>1;for(;j&bit;bit>>=1)j^=bit;j^=bit;if(i<j)swap(a[i],a[j]);}for(int len=2;len<=n;len<<=1){float ang=2*acos(-1.0)/len*inv;C wlen(cos(ang),sin(ang));for(int i=0;i<n;i+=len){C w(1,0);for(int j=0;j<len/2;j++){C u=a[i+j],v=a[i+j+len/2]*w;a[i+j]=u+v;a[i+j+len/2]=u-v;w=w*wlen;}}}if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n;}static str mul_fft(const str&a,const str&b){if(a=="0"||b=="0")return "0";int n=1,tot=a.size()+b.size();while(n<tot)n<<=1;vector<C>c1(n),c2(n);for(int i=0;i<a.size();i++)c1[i]=C(a[a.size()-1-i]-'0',0);for(int i=0;i<b.size();i++)c2[i]=C(b[b.size()-1-i]-'0',0);FFT(c1,1);FFT(c2,1);for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i];FFT(c1,-1);vector<int>res(n);for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5);for(int i=0;i<n-1;i++){res[i+1]+=res[i]/10;res[i]%=10;}while(res.size()>1&&res.back()==0)res.pop_back();str ans;for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0');return ans;}static str rlz(const str&s){int i=0;while(i<s.size()-1&&s[i]=='0')i++;return s.substr(i);}static bool ca(const str&a,const str&b){if(a.size()!=b.size())return a.size()<b.size();return a<b;}static str aa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());int n=max(s1.size(),s2.size());while(s1.size()<n)s1+='0';while(s2.size()<n)s2+='0';int c=0;str r;for(int i=0;i<n;i++){int s=(s1[i]-'0')+(s2[i]-'0')+c;r+=char(s%10+'0');c=s/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str sa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());while(s2.size()<s1.size())s2+='0';int b0=0;str r;for(int i=0;i<s1.size();i++){int t=(s1[i]-'0')-(s2[i]-'0')-b0;if(t<0){t+=10;b0=1;}else b0=0;r+=char(t+'0');}while(r.size()>1&&r.back()=='0')r.pop_back();reverse(r.begin(),r.end());return rlz(r);}static str mul_small(const str&a,int b){if(b==0)return "0";str s1=a;reverse(s1.begin(),s1.end());int c=0;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')*b+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str add_one(const str&a){str s1=a;reverse(s1.begin(),s1.end());int c=1;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+='1';reverse(r.begin(),r.end());return rlz(r);}static str div2(const str&a){str r;int cur=0;for(int i=0;i<a.size();i++){cur=cur*10+(a[i]-'0');if(cur>=2||!r.empty())r+=char(cur/2+'0');cur%=2;}if(r.empty())r="0";return rlz(r);}static str mid_str(const str&a,const str&b){str sum=aa(a,b);return div2(sum);}static pair<str,str>div_opt(const str&a,const str&b){if(b=="0")throw runtime_error("Division by zero");if(ca(a,b))return{"0",a};str low="0",high=a,pow2="1";while(!ca(a,mul_fft(pow2,b))){low=pow2;pow2=aa(pow2,pow2);}if(high=="0"||ca(high,low))high=add_one(mul_small(low,2));str best="0";while(!ca(high,low)){str mid=mid_str(low,high),pro=mul_fft(mid,b);if(!ca(a,pro)){best=mid;low=add_one(mid);}else high=sa(mid,"1");}str rem=sa(a,mul_fft(best,b));return{best,rem};}public:frint():v("0"),ng(0){}frint(const str&s){str num=s;ng=0;if(!s.empty()&&s[0]=='-'){ng=1;num=s.substr(1);}else if(!s.empty()&&s[0]=='+')num=s.substr(1);v=rlz(num);if(v=="0")ng=0;}frint(long long n){if(n<0){ng=1;n=-n;}else ng=0;v=to_string(n);}frint(const frint&o):v(o.v),ng(o.ng){}frint&operator=(const frint&o){if(this!=&o){v=o.v;ng=o.ng;}return*this;}frint operator-()const{frint r=*this;if(v!="0")r.ng=!ng;return r;}frint operator+(const frint&o)const{if(ng==o.ng){frint r;r.v=aa(v,o.v);r.ng=ng;return r;}if(ng)return o-(-*this);else return*this-(-o);}frint operator-(const frint&o)const{return*this+(-o);}frint operator*(const frint&o)const{if(v=="0"||o.v=="0")return frint("0");frint r;r.v=mul_fft(v,o.v);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator/(const frint&o)const{if(o.v=="0")throw runtime_error("Division by zero");auto p=div_opt(v,o.v);frint r(p.first);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator%(const frint&o)const{if(o.v=="0")throw runtime_error("Modulo by zero");auto p=div_opt(v,o.v);frint r(p.second);r.ng=ng;if(r.v=="0")r.ng=0;return r;}frint&operator+=(const frint&o){*this=*this+o;return*this;}frint&operator-=(const frint&o){*this=*this-o;return*this;}frint&operator*=(const frint&o){*this=*this*o;return*this;}frint&operator/=(const frint&o){*this=*this/o;return*this;}frint&operator%=(const frint&o){*this=*this%o;return*this;}bool operator<(const frint&o)const{if(ng!=o.ng)return ng;if(ng)return ca(o.v,v);else return ca(v,o.v);}bool operator>(const frint&o)const{return o<*this;}bool operator<=(const frint&o)const{return!(*this>o);}bool operator>=(const frint&o)const{return!(*this<o);}bool operator==(const frint&o)const{return v==o.v&&ng==o.ng;}bool operator!=(const frint&o)const{return!(*this==o);}frint&operator++(){*this=*this+frint(1);return*this;}frint operator++(int){frint t=*this;++(*this);return t;}frint&operator--(){*this=*this-frint(1);return*this;}frint operator--(int){frint t=*this;--(*this);return t;}friend ostream&operator<<(ostream&os,const frint&f){if(f.ng&&f.v!="0")os<<'-';os<<f.v;return os;}friend istream&operator>>(istream&is,frint&f){str s;is>>s;f=frint(s);return is;}str ts()const{if(ng&&v!="0")return"-"+v;return v;}frint ab()const{frint r=*this;r.ng=0;return r;}bool iz()const{return v=="0";}bool ip()const{return!ng&&v!="0";}bool in()const{return ng;}};```:::5 行未改动如果你不喜欢打 `frint`,请使用宏定义。例如,你想要用 `fr` 代替这个类型:```cpp#define fr frint```:::warning{open}此行需要写在主函数上方、所有实现代码的下方,其他基于本代码实现的函数等也是同理,下文不再提醒。如果你写了```cpp#define int frint```请把主函数改为```cppsigned main(){return 0;}```7 行未改动因为乘法使用 `FFT`,干脆就把复数实现了。:::info[复数实现]```cpp// 复数类class Complex{private:frint a,b; // a+bipublic:Complex():a(0),b(0){}Complex(frint _a,frint _b):a(_a),b(_b){}Complex(str s){// 解析 a+bi 或 a-bi 格式size_t pos=s.find('+');if(pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos+1);sb.pop_back(); // 去掉最后的'i'a=frint(sa);b=frint(sb);}else{// 可能是 a-bi 或只有实数或虚数pos=s.find('-');if(pos!=0&&pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos);sb.pop_back(); // 去掉最后的'i'a=frint(sa);b=frint(sb);}else if(s.back()=='i'){// 纯虚数a=0;str sb=s;sb.pop_back();b=frint(sb);}else{// 纯实数a=frint(s);b=0;}}}// 四则运算Complex operator+(const Complex& o)const{return Complex(a+o.a,b+o.b);}Complex operator-(const Complex& o)const{return Complex(a-o.a,b-o.b);}Complex operator*(const Complex& o)const{// (a+bi)(c+di)=ac-bd+(ad+bc)ifrint ra=a*o.a-b*o.b;frint rb=a*o.b+b*o.a;return Complex(ra,rb);}Complex operator/(const Complex& o)const{// (a+bi)/(c+di)=[(ac+bd)/(c^2+d^2)]+[(bc-ad)/(c^2+d^2)]ifrint c=o.a,d=o.b;frint den=c*c+d*d; // 分母if(den.iz())throw runtime_error("Complex division by zero");frint ra=(a*c+b*d)/den;frint rb=(b*c-a*d)/den;return Complex(ra,rb);}// 共轭复数Complex conj()const{return Complex(a,-b);}// 模的平方(整数)frint mod2()const{return a*a+b*b;}// 输入输出friend ostream& operator<<(ostream& os,const Complex& c){if(c.a.iz()&&c.b.iz()){os<<"0";}else if(c.b.iz()){os<<c.a;}else if(c.a.iz()){if(c.b.ip())os<<c.b<<"i";else os<<c.b<<"i";}else{os<<c.a;if(c.b.ip())os<<"+"<<c.b<<"i";else os<<c.b<<"i";}return os;}friend istream& operator>>(istream& is,Complex& c){str s;is>>s;c=Complex(s);return is;}// 比较相等bool operator==(const Complex& o)const{return a==o.a&&b==o.b;}bool operator!=(const Complex& o)const{return!(*this==o);}// 获取实部虚部frint real()const{return a;}frint imag()const{return b;}// 其他运算Complex operator-()const{return Complex(-a,-b);}Complex& operator+=(const Complex& o){a+=o.a;b+=o.b;return*this;}Complex& operator-=(const Complex& o){a-=o.a;b-=o.b;return*this;}Complex& operator*=(const Complex& o){*this=*this*o;return*this;}Complex& operator/=(const Complex& o){*this=*this/o;return*this;}};```4 行未改动很简单的宏定义就可以做到。:::info[数据类型名称替换]{open}```cpp#define str string#define float double```请不要在程序中删除它。:::### To-do list- [ ] 私有成员函数在类外部的使用。### 测试用此部分与正文无关,仅用于作者测试相关功能。:::info[测试]```cppvoid test() {cout << "=== frint 高精度整数类测试 ===" << endl;// 基本测试cout << "\n1. 基本构造函数和输出测试:" << endl;frint a("12345678901234567890");frint b("98765432109876543210");frint c(-123456);frint d = 999999999;cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;cout << "d = " << d << endl;// 算术运算测试cout << "\n2. 算术运算测试:" << endl;cout << "a + b = " << a + b << endl;cout << "b - a = " << b - a << endl;cout << "a * b = " << a * b << endl;cout << "b / a = " << b / a << endl;cout << "b % a = " << b % a << endl;// 负数运算测试cout << "\n3. 负数运算测试:" << endl;frint e("-123456789");frint f("987654321");cout << "e = " << e << endl;cout << "f = " << f << endl;cout << "e + f = " << e + f << endl;cout << "e - f = " << e - f << endl;cout << "e * f = " << e * f << endl;cout << "f / e = " << f / e << endl;// 复合赋值测试cout << "\n4. 复合赋值运算测试:" << endl;frint g("1000000000000000000");frint h("3");g += h;cout << "g += 3: " << g << endl;g *= frint("2");cout << "g *= 2: " << g << endl;g /= frint("7");cout << "g /= 7: " << g << endl;// 比较运算测试cout << "\n5. 比较运算测试:" << endl;cout << "a < b: " << (a < b) << endl;cout << "a > b: " << (a > b) << endl;cout << "a == b: " << (a == b) << endl;cout << "a != b: " << (a != b) << endl;cout << "e < f: " << (e < f) << endl;// 递增递减测试cout << "\n6. 递增递减测试:" << endl;frint i("999999999999999999");cout << "i = " << i << endl;cout << "++i = " << ++i << endl;cout << "i++ = " << i++ << endl;cout << "i after i++ = " << i << endl;cout << "--i = " << --i << endl;cout << "i-- = " << i-- << endl;cout << "i after i-- = " << i << endl;// 大数性能测试cout << "\n7. 大数运算性能测试:" << endl;frint j("1" + string(100, '9')); // 101位数字frint k("1234567890");auto start = chrono::high_resolution_clock::now();frint l = j * k;auto end = chrono::high_resolution_clock::now();auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);cout << "100位数字 * 10位数字: " << duration.count() << " ms" << endl;// 异常处理测试cout << "\n8. 异常处理测试:" << endl;try {frint zero("0");frint result = a / zero;cout << "Should not reach here!" << endl;} catch (const runtime_error& err) {cout << "Caught exception: " << err.what() << endl;}// Complex类测试cout << "\n=== Complex 复数类测试 ===" << endl;Complex c1("3+4i");Complex c2("1+2i");Complex c3("-5-6i");cout << "\n1. 复数基本运算:" << endl;cout << "c1 = " << c1 << endl;cout << "c2 = " << c2 << endl;cout << "c3 = " << c3 << endl;cout << "c1 + c2 = " << c1 + c2 << endl;cout << "c1 - c2 = " << c1 - c2 << endl;cout << "c1 * c2 = " << c1 * c2 << endl;cout << "c1 / c2 = " << c1 / c2 << endl;cout << "\n2. 复数共轭和模平方:" << endl;cout << "conj(c1) = " << c1.conj() << endl;cout << "|c1|^2 = " << c1.mod2() << endl;cout << "\n3. 复数输入测试(请输入复数,如 5+3i 或 -2-4i): ";Complex c4;cin >> c4;cout << "你输入的复数是: " << c4 << endl;// 混合运算测试cout << "\n4. 混合运算测试:" << endl;Complex c5 = c1 * c2 + c3;cout << "c1 * c2 + c3 = " << c5 << endl;cout << "\n5. 大数复数测试:" << endl;Complex big1("12345678901234567890+98765432109876543210i");Complex big2("11111111111111111111+22222222222222222222i");cout << "big1 = " << big1 << endl;cout << "big2 = " << big2 << endl;cout << "big1 + big2 = " << big1 + big2 << endl;cout << "big1 * big2 = " << big1 * big2 << endl;cout << "\n=== 所有测试完成 ===" << endl;}```该测试函数使用deepseekR1生成。:::## 集成:::info[集成]```cpp#include <iostream>#include <vector>#include <string>#include <map>#include <algorithm>#include <sstream>#include <fstream>#include <windows.h>#include <cmath>#include <stdexcept>#include <chrono>#define str string#define float doubleusing namespace std;class frint{private:str v;bool ng;struct C{float x,y;C(float _x=0.0,float _y=0.0):x(_x),y(_y){}C operator+(const C&b)const{return C(x+b.x,y+b.y);}C operator-(const C&b)const{return C(x-b.x,y-b.y);}C operator*(const C&b)const{return C(x*b.x-y*b.y,x*b.y+y*b.x);}};static void FFT(vector<C>&a,int inv){int n=a.size();for(int i=1,j=0;i<n;i++){int bit=n>>1;for(;j&bit;bit>>=1)j^=bit;j^=bit;if(i<j)swap(a[i],a[j]);}for(int len=2;len<=n;len<<=1){float ang=2*acos(-1.0)/len*inv;C wlen(cos(ang),sin(ang));for(int i=0;i<n;i+=len){C w(1,0);for(int j=0;j<len/2;j++){C u=a[i+j],v=a[i+j+len/2]*w;a[i+j]=u+v;a[i+j+len/2]=u-v;w=w*wlen;}}}if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n;}static str mul_fft(const str&a,const str&b){if(a=="0"||b=="0")return "0";int n=1,tot=a.size()+b.size();while(n<tot)n<<=1;vector<C>c1(n),c2(n);for(int i=0;i<a.size();i++)c1[i]=C(a[a.size()-1-i]-'0',0);for(int i=0;i<b.size();i++)c2[i]=C(b[b.size()-1-i]-'0',0);FFT(c1,1);FFT(c2,1);for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i];FFT(c1,-1);vector<int>res(n);for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5);for(int i=0;i<n-1;i++){res[i+1]+=res[i]/10;res[i]%=10;}while(res.size()>1&&res.back()==0)res.pop_back();str ans;for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0');return ans;}static str rlz(const str&s){int i=0;while(i<s.size()-1&&s[i]=='0')i++;return s.substr(i);}static bool ca(const str&a,const str&b){if(a.size()!=b.size())return a.size()<b.size();return a<b;}static str aa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());int n=max(s1.size(),s2.size());while(s1.size()<n)s1+='0';while(s2.size()<n)s2+='0';int c=0;str r;for(int i=0;i<n;i++){int s=(s1[i]-'0')+(s2[i]-'0')+c;r+=char(s%10+'0');c=s/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str sa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());while(s2.size()<s1.size())s2+='0';int b0=0;str r;for(int i=0;i<s1.size();i++){int t=(s1[i]-'0')-(s2[i]-'0')-b0;if(t<0){t+=10;b0=1;}else b0=0;r+=char(t+'0');}while(r.size()>1&&r.back()=='0')r.pop_back();reverse(r.begin(),r.end());return rlz(r);}static str mul_small(const str&a,int b){if(b==0)return "0";str s1=a;reverse(s1.begin(),s1.end());int c=0;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')*b+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str add_one(const str&a){str s1=a;reverse(s1.begin(),s1.end());int c=1;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+='1';reverse(r.begin(),r.end());return rlz(r);}static str div2(const str&a){str r;int cur=0;for(int i=0;i<a.size();i++){cur=cur*10+(a[i]-'0');if(cur>=2||!r.empty())r+=char(cur/2+'0');cur%=2;}if(r.empty())r="0";return rlz(r);}static str mid_str(const str&a,const str&b){str sum=aa(a,b);return div2(sum);}static pair<str,str>div_opt(const str&a,const str&b){if(b=="0")throw runtime_error("Division by zero");if(ca(a,b))return{"0",a};str low="0",high=a,pow2="1";while(!ca(a,mul_fft(pow2,b))){low=pow2;pow2=aa(pow2,pow2);}if(high=="0"||ca(high,low))high=add_one(mul_small(low,2));str best="0";while(!ca(high,low)){str mid=mid_str(low,high),pro=mul_fft(mid,b);if(!ca(a,pro)){best=mid;low=add_one(mid);}else high=sa(mid,"1");}str rem=sa(a,mul_fft(best,b));return{best,rem};}public:frint():v("0"),ng(0){}frint(const str&s){str num=s;ng=0;if(!s.empty()&&s[0]=='-'){ng=1;num=s.substr(1);}else if(!s.empty()&&s[0]=='+')num=s.substr(1);v=rlz(num);if(v=="0")ng=0;}frint(long long n){if(n<0){ng=1;n=-n;}else ng=0;v=to_string(n);}frint(const frint&o):v(o.v),ng(o.ng){}frint&operator=(const frint&o){if(this!=&o){v=o.v;ng=o.ng;}return*this;}frint operator-()const{frint r=*this;if(v!="0")r.ng=!ng;return r;}frint operator+(const frint&o)const{if(ng==o.ng){frint r;r.v=aa(v,o.v);r.ng=ng;return r;}if(ng)return o-(-*this);else return*this-(-o);}frint operator-(const frint&o)const{return*this+(-o);}frint operator*(const frint&o)const{if(v=="0"||o.v=="0")return frint("0");frint r;r.v=mul_fft(v,o.v);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator/(const frint&o)const{if(o.v=="0")throw runtime_error("Division by zero");auto p=div_opt(v,o.v);frint r(p.first);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator%(const frint&o)const{if(o.v=="0")throw runtime_error("Modulo by zero");auto p=div_opt(v,o.v);frint r(p.second);r.ng=ng;if(r.v=="0")r.ng=0;return r;}frint&operator+=(const frint&o){*this=*this+o;return*this;}frint&operator-=(const frint&o){*this=*this-o;return*this;}frint&operator*=(const frint&o){*this=*this*o;return*this;}frint&operator/=(const frint&o){*this=*this/o;return*this;}frint&operator%=(const frint&o){*this=*this%o;return*this;}bool operator<(const frint&o)const{if(ng!=o.ng)return ng;if(ng)return ca(o.v,v);else return ca(v,o.v);}bool operator>(const frint&o)const{return o<*this;}bool operator<=(const frint&o)const{return!(*this>o);}bool operator>=(const frint&o)const{return!(*this<o);}bool operator==(const frint&o)const{return v==o.v&&ng==o.ng;}bool operator!=(const frint&o)const{return!(*this==o);}frint&operator++(){*this=*this+frint(1);return*this;}frint operator++(int){frint t=*this;++(*this);return t;}frint&operator--(){*this=*this-frint(1);return*this;}frint operator--(int){frint t=*this;--(*this);return t;}friend ostream&operator<<(ostream&os,const frint&f){if(f.ng&&f.v!="0")os<<'-';os<<f.v;return os;}friend istream&operator>>(istream&is,frint&f){str s;is>>s;f=frint(s);return is;}str ts()const{if(ng&&v!="0")return"-"+v;return v;}frint ab()const{frint r=*this;r.ng=0;return r;}bool iz()const{return v=="0";}bool ip()const{return!ng&&v!="0";}bool in()const{return ng;}};// 复数类class Complex{private:frint a,b; // a+bipublic:Complex():a(0),b(0){}Complex(frint _a,frint _b):a(_a),b(_b){}Complex(str s){// 解析 a+bi 或 a-bi 格式size_t pos=s.find('+');if(pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos+1);sb.pop_back(); // 去掉最后的'i'a=frint(sa);b=frint(sb);}else{// 可能是 a-bi 或只有实数或虚数pos=s.find('-');if(pos!=0&&pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos);sb.pop_back(); // 去掉最后的'i'a=frint(sa);b=frint(sb);}else if(s.back()=='i'){// 纯虚数a=0;str sb=s;sb.pop_back();b=frint(sb);}else{// 纯实数a=frint(s);b=0;}}}// 四则运算Complex operator+(const Complex& o)const{return Complex(a+o.a,b+o.b);}Complex operator-(const Complex& o)const{return Complex(a-o.a,b-o.b);}Complex operator*(const Complex& o)const{// (a+bi)(c+di)=ac-bd+(ad+bc)ifrint ra=a*o.a-b*o.b;frint rb=a*o.b+b*o.a;return Complex(ra,rb);}Complex operator/(const Complex& o)const{// (a+bi)/(c+di)=[(ac+bd)/(c^2+d^2)]+[(bc-ad)/(c^2+d^2)]ifrint c=o.a,d=o.b;frint den=c*c+d*d; // 分母if(den.iz())throw runtime_error("Complex division by zero");frint ra=(a*c+b*d)/den;frint rb=(b*c-a*d)/den;return Complex(ra,rb);}// 共轭复数Complex conj()const{return Complex(a,-b);}// 模的平方(整数)frint mod2()const{return a*a+b*b;}// 输入输出friend ostream& operator<<(ostream& os,const Complex& c){if(c.a.iz()&&c.b.iz()){os<<"0";}else if(c.b.iz()){os<<c.a;}else if(c.a.iz()){if(c.b.ip())os<<c.b<<"i";else os<<c.b<<"i";}else{os<<c.a;if(c.b.ip())os<<"+"<<c.b<<"i";else os<<c.b<<"i";}return os;}friend istream& operator>>(istream& is,Complex& c){str s;is>>s;c=Complex(s);return is;}// 比较相等bool operator==(const Complex& o)const{return a==o.a&&b==o.b;}bool operator!=(const Complex& o)const{return!(*this==o);}// 获取实部虚部frint real()const{return a;}frint imag()const{return b;}// 其他运算Complex operator-()const{return Complex(-a,-b);}Complex& operator+=(const Complex& o){a+=o.a;b+=o.b;return*this;}Complex& operator-=(const Complex& o){a-=o.a;b-=o.b;return*this;}Complex& operator*=(const Complex& o){*this=*this*o;return*this;}Complex& operator/=(const Complex& o){*this=*this/o;return*this;}};//void test() {// cout << "=== frint 高精度整数类测试 ===" << endl;// // 基本测试// cout << "\n1. 基本构造函数和输出测试:" << endl;// frint a("12345678901234567890");// frint b("98765432109876543210");// frint c(-123456);// frint d = 999999999;// cout << "a = " << a << endl;// cout << "b = " << b << endl;// cout << "c = " << c << endl;// cout << "d = " << d << endl;// // 算术运算测试// cout << "\n2. 算术运算测试:" << endl;// cout << "a + b = " << a + b << endl;// cout << "b - a = " << b - a << endl;// cout << "a * b = " << a * b << endl;// cout << "b / a = " << b / a << endl;// cout << "b % a = " << b % a << endl;// // 负数运算测试// cout << "\n3. 负数运算测试:" << endl;// frint e("-123456789");// frint f("987654321");// cout << "e = " << e << endl;// cout << "f = " << f << endl;// cout << "e + f = " << e + f << endl;// cout << "e - f = " << e - f << endl;// cout << "e * f = " << e * f << endl;// cout << "f / e = " << f / e << endl;// // 复合赋值测试// cout << "\n4. 复合赋值运算测试:" << endl;// frint g("1000000000000000000");// frint h("3");// g += h;// cout << "g += 3: " << g << endl;// g *= frint("2");// cout << "g *= 2: " << g << endl;// g /= frint("7");// cout << "g /= 7: " << g << endl;// // 比较运算测试// cout << "\n5. 比较运算测试:" << endl;// cout << "a < b: " << (a < b) << endl;// cout << "a > b: " << (a > b) << endl;// cout << "a == b: " << (a == b) << endl;// cout << "a != b: " << (a != b) << endl;// cout << "e < f: " << (e < f) << endl;// // 递增递减测试// cout << "\n6. 递增递减测试:" << endl;// frint i("999999999999999999");// cout << "i = " << i << endl;// cout << "++i = " << ++i << endl;// cout << "i++ = " << i++ << endl;// cout << "i after i++ = " << i << endl;// cout << "--i = " << --i << endl;// cout << "i-- = " << i-- << endl;// cout << "i after i-- = " << i << endl;// // 大数性能测试// cout << "\n7. 大数运算性能测试:" << endl;// frint j("1" + string(100, '9')); // 101位数字// frint k("1234567890");// auto start = chrono::high_resolution_clock::now();// frint l = j * k;// auto end = chrono::high_resolution_clock::now();// auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);// cout << "100位数字 * 10位数字: " << duration.count() << " ms" << endl;// // 异常处理测试// cout << "\n8. 异常处理测试:" << endl;// try {// frint zero("0");// frint result = a / zero;// cout << "Should not reach here!" << endl;// } catch (const runtime_error& err) {// cout << "Caught exception: " << err.what() << endl;// }// // Complex类测试// cout << "\n=== Complex 复数类测试 ===" << endl;// Complex c1("3+4i");// Complex c2("1+2i");// Complex c3("-5-6i");// cout << "\n1. 复数基本运算:" << endl;// cout << "c1 = " << c1 << endl;// cout << "c2 = " << c2 << endl;// cout << "c3 = " << c3 << endl;// cout << "c1 + c2 = " << c1 + c2 << endl;// cout << "c1 - c2 = " << c1 - c2 << endl;// cout << "c1 * c2 = " << c1 * c2 << endl;// cout << "c1 / c2 = " << c1 / c2 << endl;// cout << "\n2. 复数共轭和模平方:" << endl;// cout << "conj(c1) = " << c1.conj() << endl;// cout << "|c1|^2 = " << c1.mod2() << endl;// cout << "\n3. 复数输入测试(请输入复数,如 5+3i 或 -2-4i): ";// Complex c4;// cin >> c4;// cout << "你输入的复数是: " << c4 << endl;// // 混合运算测试// cout << "\n4. 混合运算测试:" << endl;// Complex c5 = c1 * c2 + c3;// cout << "c1 * c2 + c3 = " << c5 << endl;// cout << "\n5. 大数复数测试:" << endl;// Complex big1("12345678901234567890+98765432109876543210i");// Complex big2("11111111111111111111+22222222222222222222i");// cout << "big1 = " << big1 << endl;// cout << "big2 = " << big2 << endl;// cout << "big1 + big2 = " << big1 + big2 << endl;// cout << "big1 * big2 = " << big1 * big2 << endl;// cout << "\n=== 所有测试完成 ===" << endl;//}signed main(){// test();return 0;}```:::1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994005 行未改动4064074084094104114124134144154164174184194204214224234244254264274284294307 行未改动4384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615624 行未改动5675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174- [C++plus文档地址](https://www.luogu.com.cn/article/ykup0ycs)(洛谷)。## 前言起因:这些东西好麻烦。中间忘了,然后就有了它。欢迎私信反馈qwq## 正文### 编译选项若无特别说明,下文采用的**编译选项**均为```bash-std=c++11 -O2```:::warning[编译选项设置]{open}以 `Dev-C++` 编译器为例,点击“工具-编译选项”打开窗口。其他默认选项不要动,把编译器和编译器配置修改为如图所示。不执行此操作可能会导致编译错误。:::### 开始导入必须的库。:::info[导入库]```cpp line-numbers#include <iostream>#include <vector>#include <string>#include <map>#include <algorithm>#include <sstream>#include <fstream>// #include <windows.h> // 作为使用者,您不需要该库#include <cmath>#include <stdexcept>// #include <chrono> // 作为使用者,您不需要该库```:::### 新的数据类型通过C++的高精模板与重载运算符实现。你可能会问 `frint` 是什么意思,因为FR是我的用户名缩写(:::info[frint]```cpp line-numbersclass frint{private:str v;bool ng;struct C{float x,y;C(float _x=0.0,float _y=0.0):x(_x),y(_y){}C operator+(const C&b)const{return C(x+b.x,y+b.y);}C operator-(const C&b)const{return C(x-b.x,y-b.y);}C operator*(const C&b)const{return C(x*b.x-y*b.y,x*b.y+y*b.x);}};static void FFT(vector<C>&a,int inv){int n=a.size();for(int i=1,j=0;i<n;i++){int bit=n>>1;for(;j&bit;bit>>=1)j^=bit;j^=bit;if(i<j)swap(a[i],a[j]);}for(int len=2;len<=n;len<<=1){float ang=2*acos(-1.0)/len*inv;C wlen(cos(ang),sin(ang));for(int i=0;i<n;i+=len){C w(1,0);for(int j=0;j<len/2;j++){C u=a[i+j],v=a[i+j+len/2]*w;a[i+j]=u+v;a[i+j+len/2]=u-v;w=w*wlen;}}}if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n;}static str mul_fft(const str&a,const str&b){if(a=="0"||b=="0")return "0";int n=1,tot=a.size()+b.size();while(n<tot)n<<=1;vector<C>c1(n),c2(n);for(int i=0;i<a.size();i++)c1[i]=C(a[a.size()-1-i]-'0',0);for(int i=0;i<b.size();i++)c2[i]=C(b[b.size()-1-i]-'0',0);FFT(c1,1);FFT(c2,1);for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i];FFT(c1,-1);vector<int>res(n);for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5);for(int i=0;i<n-1;i++){res[i+1]+=res[i]/10;res[i]%=10;}while(res.size()>1&&res.back()==0)res.pop_back();str ans;for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0');return ans;}static str rlz(const str&s){int i=0;while(i<s.size()-1&&s[i]=='0')i++;return s.substr(i);}static bool ca(const str&a,const str&b){if(a.size()!=b.size())return a.size()<b.size();return a<b;}static str aa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());int n=max(s1.size(),s2.size());while(s1.size()<n)s1+='0';while(s2.size()<n)s2+='0';int c=0;str r;for(int i=0;i<n;i++){int s=(s1[i]-'0')+(s2[i]-'0')+c;r+=char(s%10+'0');c=s/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str sa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());while(s2.size()<s1.size())s2+='0';int b0=0;str r;for(int i=0;i<s1.size();i++){int t=(s1[i]-'0')-(s2[i]-'0')-b0;if(t<0){t+=10;b0=1;}else b0=0;r+=char(t+'0');}while(r.size()>1&&r.back()=='0')r.pop_back();reverse(r.begin(),r.end());return rlz(r);}static str mul_small(const str&a,int b){if(b==0)return "0";str s1=a;reverse(s1.begin(),s1.end());int c=0;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')*b+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str add_one(const str&a){str s1=a;reverse(s1.begin(),s1.end());int c=1;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+='1';reverse(r.begin(),r.end());return rlz(r);}static str div2(const str&a){str r;int cur=0;for(int i=0;i<a.size();i++){cur=cur*10+(a[i]-'0');if(cur>=2||!r.empty())r+=char(cur/2+'0');cur%=2;}if(r.empty())r="0";return rlz(r);}static str mid_str(const str&a,const str&b){str sum=aa(a,b);return div2(sum);}static pair<str,str>div_opt(const str&a,const str&b){if(b=="0")throw runtime_error("Division by zero");if(ca(a,b))return{"0",a};str low="0",high=a,pow2="1";while(!ca(a,mul_fft(pow2,b))){low=pow2;pow2=aa(pow2,pow2);}if(high=="0"||ca(high,low))high=add_one(mul_small(low,2));str best="0";while(!ca(high,low)){str mid=mid_str(low,high),pro=mul_fft(mid,b);if(!ca(a,pro)){best=mid;low=add_one(mid);}else high=sa(mid,"1");}str rem=sa(a,mul_fft(best,b));return{best,rem};}public:frint():v("0"),ng(0){}frint(const str&s){str num=s;ng=0;if(!s.empty()&&s[0]=='-'){ng=1;num=s.substr(1);}else if(!s.empty()&&s[0]=='+')num=s.substr(1);v=rlz(num);if(v=="0")ng=0;}frint(long long n){if(n<0){ng=1;n=-n;}else ng=0;v=to_string(n);}frint(const frint&o):v(o.v),ng(o.ng){}frint&operator=(const frint&o){if(this!=&o){v=o.v;ng=o.ng;}return*this;}frint operator-()const{frint r=*this;if(v!="0")r.ng=!ng;return r;}frint operator+(const frint&o)const{if(ng==o.ng){frint r;r.v=aa(v,o.v);r.ng=ng;return r;}if(ng)return o-(-*this);else return*this-(-o);}frint operator-(const frint&o)const{return*this+(-o);}frint operator*(const frint&o)const{if(v=="0"||o.v=="0")return frint("0");frint r;r.v=mul_fft(v,o.v);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator/(const frint&o)const{if(o.v=="0")throw runtime_error("Division by zero");auto p=div_opt(v,o.v);frint r(p.first);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator%(const frint&o)const{if(o.v=="0")throw runtime_error("Modulo by zero");auto p=div_opt(v,o.v);frint r(p.second);r.ng=ng;if(r.v=="0")r.ng=0;return r;}frint&operator+=(const frint&o){*this=*this+o;return*this;}frint&operator-=(const frint&o){*this=*this-o;return*this;}frint&operator*=(const frint&o){*this=*this*o;return*this;}frint&operator/=(const frint&o){*this=*this/o;return*this;}frint&operator%=(const frint&o){*this=*this%o;return*this;}bool operator<(const frint&o)const{if(ng!=o.ng)return ng;if(ng)return ca(o.v,v);else return ca(v,o.v);}bool operator>(const frint&o)const{return o<*this;}bool operator<=(const frint&o)const{return!(*this>o);}bool operator>=(const frint&o)const{return!(*this<o);}bool operator==(const frint&o)const{return v==o.v&&ng==o.ng;}bool operator!=(const frint&o)const{return!(*this==o);}frint&operator++(){*this=*this+frint(1);return*this;}frint operator++(int){frint t=*this;++(*this);return t;}frint&operator--(){*this=*this-frint(1);return*this;}frint operator--(int){frint t=*this;--(*this);return t;}friend ostream&operator<<(ostream&os,const frint&f){if(f.ng&&f.v!="0")os<<'-';os<<f.v;return os;}friend istream&operator>>(istream&is,frint&f){str s;is>>s;f=frint(s);return is;}str ts()const{if(ng&&v!="0")return"-"+v;return v;}frint ab()const{frint r=*this;r.ng=0;return r;}bool iz()const{return v=="0";}bool ip()const{return!ng&&v!="0";}bool in()const{return ng;}};```:::5 行未改动如果你不喜欢打 `frint`,请使用宏定义。例如,你想要用 `fr` 代替这个类型:```cpp line-numbers#define fr frint```:::warning{open}如果你写了```cpp line-numbers#define int frint```请把主函数改为```cpp line-numberssigned main(){return 0;}```7 行未改动因为乘法使用 `FFT`,干脆就把复数实现了。:::info[复数实现]```cpp line-numbersclass Complex{private:frint a,b;public:Complex():a(0),b(0){}Complex(frint _a,frint _b):a(_a),b(_b){}Complex(str s){size_t pos=s.find('+');if(pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos+1);sb.pop_back();a=frint(sa);b=frint(sb);}else{pos=s.find('-');if(pos!=0&&pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos);sb.pop_back();a=frint(sa);b=frint(sb);}else if(s.back()=='i'){a=0;str sb=s;sb.pop_back();b=frint(sb);}else{a=frint(s);b=0;}}}Complex operator+(const Complex& o)const{return Complex(a+o.a,b+o.b);}Complex operator-(const Complex& o)const{return Complex(a-o.a,b-o.b);}Complex operator*(const Complex& o)const{frint ra=a*o.a-b*o.b;frint rb=a*o.b+b*o.a;return Complex(ra,rb);}Complex operator/(const Complex& o)const{frint c=o.a,d=o.b;frint den=c*c+d*d;if(den.iz())throw runtime_error("Complex division by zero");frint ra=(a*c+b*d)/den;frint rb=(b*c-a*d)/den;return Complex(ra,rb);}Complex conj()const{return Complex(a,-b);}frint mod2()const{return a*a+b*b;}friend ostream& operator<<(ostream& os,const Complex& c){if(c.a.iz()&&c.b.iz()){os<<"0";}else if(c.b.iz()){os<<c.a;}else if(c.a.iz()){if(c.b.ip())os<<c.b<<"i";else os<<c.b<<"i";}else{os<<c.a;if(c.b.ip())os<<"+"<<c.b<<"i";else os<<c.b<<"i";}return os;}friend istream& operator>>(istream& is,Complex& c){str s;is>>s;c=Complex(s);return is;}bool operator==(const Complex& o)const{return a==o.a&&b==o.b;}bool operator!=(const Complex& o)const{return!(*this==o);}frint real()const{return a;}frint imag()const{return b;}Complex operator-()const{return Complex(-a,-b);}Complex& operator+=(const Complex& o){a+=o.a;b+=o.b;return*this;}Complex& operator-=(const Complex& o){a-=o.a;b-=o.b;return*this;}Complex& operator*=(const Complex& o){*this=*this*o;return*this;}Complex& operator/=(const Complex& o){*this=*this/o;return*this;}};```4 行未改动很简单的宏定义就可以做到。:::info[数据类型名称替换]{open}```cpp line-numbers#define str string#define float double```请不要在程序中删除它。:::### 测试用此部分与正文无关,仅用于笔者测试相关功能。:::info[测试]```cpp line-numbers//void test() {// cout << "=== frint 高精度整数类测试 ===" << endl;// // 基本测试// cout << "\n1. 基本构造函数和输出测试:" << endl;// frint a("12345678901234567890");// frint b("98765432109876543210");// frint c(-123456);// frint d = 999999999;// cout << "a = " << a << endl;// cout << "b = " << b << endl;// cout << "c = " << c << endl;// cout << "d = " << d << endl;// // 算术运算测试// cout << "\n2. 算术运算测试:" << endl;// cout << "a + b = " << a + b << endl;// cout << "b - a = " << b - a << endl;// cout << "a * b = " << a * b << endl;// cout << "b / a = " << b / a << endl;// cout << "b % a = " << b % a << endl;// // 负数运算测试// cout << "\n3. 负数运算测试:" << endl;// frint e("-123456789");// frint f("987654321");// cout << "e = " << e << endl;// cout << "f = " << f << endl;// cout << "e + f = " << e + f << endl;// cout << "e - f = " << e - f << endl;// cout << "e * f = " << e * f << endl;// cout << "f / e = " << f / e << endl;// // 复合赋值测试// cout << "\n4. 复合赋值运算测试:" << endl;// frint g("1000000000000000000");// frint h("3");// g += h;// cout << "g += 3: " << g << endl;// g *= frint("2");// cout << "g *= 2: " << g << endl;// g /= frint("7");// cout << "g /= 7: " << g << endl;// // 比较运算测试// cout << "\n5. 比较运算测试:" << endl;// cout << "a < b: " << (a < b) << endl;// cout<< "a> b: " << (a > b) << endl;// cout << "a == b: " << (a == b) << endl;// cout << "a != b: " << (a != b) << endl;// cout << "e < f: " << (e < f) << endl;// // 递增递减测试// cout << "\n6. 递增递减测试:" << endl;// frint i("999999999999999999");// cout << "i = " << i << endl;// cout << "++i = " << ++i << endl;// cout << "i++ = " << i++ << endl;// cout << "i after i++ = " << i << endl;// cout << "--i = " << --i << endl;// cout << "i-- = " << i-- << endl;// cout << "i after i-- = " << i << endl;// // 大数性能测试// cout << "\n7. 大数运算性能测试:" << endl;// frint j("1" + string(100, '9')); // 101位数字// frint k("1234567890");// auto start = chrono::high_resolution_clock::now();// frint l = j * k;// auto end = chrono::high_resolution_clock::now();// auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);// cout << "100位数字 * 10位数字: " << duration.count() << " ms" << endl;// // 异常处理测试// cout << "\n8. 异常处理测试:" << endl;// try {// frint zero("0");// frint result = a / zero;// cout << "Should not reach here!" << endl;// } catch (const runtime_error& err) {// cout << "Caught exception: " << err.what() << endl;// }// // Complex类测试// cout << "\n=== Complex 复数类测试 ===" << endl;// Complex c1("3+4i");// Complex c2("1+2i");// Complex c3("-5-6i");// cout << "\n1. 复数基本运算:" << endl;// cout << "c1 = " << c1 << endl;// cout << "c2 = " << c2 << endl;// cout << "c3 = " << c3 << endl;// cout << "c1 + c2 = " << c1 + c2 << endl;// cout << "c1 - c2 = " << c1 - c2 << endl;// cout << "c1 * c2 = " << c1 * c2 << endl;// cout << "c1 / c2 = " << c1 / c2 << endl;// cout << "\n2. 复数共轭和模平方:" << endl;// cout << "conj(c1) = " << c1.conj() << endl;// cout << "|c1|^2 = " << c1.mod2() << endl;// cout << "\n3. 复数输入测试(请输入复数,如 5+3i 或 -2-4i): ";// Complex c4;// cin >> c4;// cout << "你输入的复数是: " << c4 << endl;// // 混合运算测试// cout << "\n4. 混合运算测试:" << endl;// Complex c5 = c1 * c2 + c3;// cout << "c1 * c2 + c3 = " << c5 << endl;// cout << "\n5. 大数复数测试:" << endl;// Complex big1("12345678901234567890+98765432109876543210i");// Complex big2("11111111111111111111+22222222222222222222i");// cout << "big1 = " << big1 << endl;// cout << "big2 = " << big2 << endl;// cout << "big1 + big2 = " << big1 + big2 << endl;// cout << "big1 * big2 = " << big1 * big2 << endl;// cout << "\n=== 所有测试完成 ===" << endl;//}```该测试函数使用deepseek-R1生成。:::## 集成:::info[用户集成]{open}```cpp line-numbers#include <iostream>#include <vector>#include <string>#include <map>#include <algorithm>#include <sstream>#include <fstream>#include <cmath>#include <stdexcept>#define str string#define float double#define endl '\n'using namespace std;class frint{private:str v;bool ng;struct C{float x,y;C(float _x=0.0,float _y=0.0):x(_x),y(_y){}C operator+(const C&b)const{return C(x+b.x,y+b.y);}C operator-(const C&b)const{return C(x-b.x,y-b.y);}C operator*(const C&b)const{return C(x*b.x-y*b.y,x*b.y+y*b.x);}};static void FFT(vector<C>&a,int inv){int n=a.size();for(int i=1,j=0;i<n;i++){int bit=n>>1;for(;j&bit;bit>>=1)j^=bit;j^=bit;if(i<j)swap(a[i],a[j]);}for(int len=2;len<=n;len<<=1){float ang=2*acos(-1.0)/len*inv;C wlen(cos(ang),sin(ang));for(int i=0;i<n;i+=len){C w(1,0);for(int j=0;j<len/2;j++){C u=a[i+j],v=a[i+j+len/2]*w;a[i+j]=u+v;a[i+j+len/2]=u-v;w=w*wlen;}}}if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n;}static str mul_fft(const str&a,const str&b){if(a=="0"||b=="0")return "0";int n=1,tot=a.size()+b.size();while(n<tot)n<<=1;vector<C>c1(n),c2(n);for(int i=0;i<a.size();i++)c1[i]=C(a[a.size()-1-i]-'0',0);for(int i=0;i<b.size();i++)c2[i]=C(b[b.size()-1-i]-'0',0);FFT(c1,1);FFT(c2,1);for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i];FFT(c1,-1);vector<int>res(n);for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5);for(int i=0;i<n-1;i++){res[i+1]+=res[i]/10;res[i]%=10;}while(res.size()>1&&res.back()==0)res.pop_back();str ans;for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0');return ans;}static str rlz(const str&s){int i=0;while(i<s.size()-1&&s[i]=='0')i++;return s.substr(i);}static bool ca(const str&a,const str&b){if(a.size()!=b.size())return a.size()<b.size();return a<b;}static str aa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());int n=max(s1.size(),s2.size());while(s1.size()<n)s1+='0';while(s2.size()<n)s2+='0';int c=0;str r;for(int i=0;i<n;i++){int s=(s1[i]-'0')+(s2[i]-'0')+c;r+=char(s%10+'0');c=s/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str sa(const str&a,const str&b){str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end());while(s2.size()<s1.size())s2+='0';int b0=0;str r;for(int i=0;i<s1.size();i++){int t=(s1[i]-'0')-(s2[i]-'0')-b0;if(t<0){t+=10;b0=1;}else b0=0;r+=char(t+'0');}while(r.size()>1&&r.back()=='0')r.pop_back();reverse(r.begin(),r.end());return rlz(r);}static str mul_small(const str&a,int b){if(b==0)return "0";str s1=a;reverse(s1.begin(),s1.end());int c=0;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')*b+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);}static str add_one(const str&a){str s1=a;reverse(s1.begin(),s1.end());int c=1;str r;for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')+c;r+=char(cur%10+'0');c=cur/10;}if(c)r+='1';reverse(r.begin(),r.end());return rlz(r);}static str div2(const str&a){str r;int cur=0;for(int i=0;i<a.size();i++){cur=cur*10+(a[i]-'0');if(cur>=2||!r.empty())r+=char(cur/2+'0');cur%=2;}if(r.empty())r="0";return rlz(r);}static str mid_str(const str&a,const str&b){str sum=aa(a,b);return div2(sum);}static pair<str,str>div_opt(const str&a,const str&b){if(b=="0")throw runtime_error("Division by zero");if(ca(a,b))return{"0",a};str low="0",high=a,pow2="1";while(!ca(a,mul_fft(pow2,b))){low=pow2;pow2=aa(pow2,pow2);}if(high=="0"||ca(high,low))high=add_one(mul_small(low,2));str best="0";while(!ca(high,low)){str mid=mid_str(low,high),pro=mul_fft(mid,b);if(!ca(a,pro)){best=mid;low=add_one(mid);}else high=sa(mid,"1");}str rem=sa(a,mul_fft(best,b));return{best,rem};}public:frint():v("0"),ng(0){}frint(const str&s){str num=s;ng=0;if(!s.empty()&&s[0]=='-'){ng=1;num=s.substr(1);}else if(!s.empty()&&s[0]=='+')num=s.substr(1);v=rlz(num);if(v=="0")ng=0;}frint(long long n){if(n<0){ng=1;n=-n;}else ng=0;v=to_string(n);}frint(const frint&o):v(o.v),ng(o.ng){}frint&operator=(const frint&o){if(this!=&o){v=o.v;ng=o.ng;}return*this;}frint operator-()const{frint r=*this;if(v!="0")r.ng=!ng;return r;}frint operator+(const frint&o)const{if(ng==o.ng){frint r;r.v=aa(v,o.v);r.ng=ng;return r;}if(ng)return o-(-*this);else return*this-(-o);}frint operator-(const frint&o)const{return*this+(-o);}frint operator*(const frint&o)const{if(v=="0"||o.v=="0")return frint("0");frint r;r.v=mul_fft(v,o.v);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator/(const frint&o)const{if(o.v=="0")throw runtime_error("Division by zero");auto p=div_opt(v,o.v);frint r(p.first);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;}frint operator%(const frint&o)const{if(o.v=="0")throw runtime_error("Modulo by zero");auto p=div_opt(v,o.v);frint r(p.second);r.ng=ng;if(r.v=="0")r.ng=0;return r;}frint&operator+=(const frint&o){*this=*this+o;return*this;}frint&operator-=(const frint&o){*this=*this-o;return*this;}frint&operator*=(const frint&o){*this=*this*o;return*this;}frint&operator/=(const frint&o){*this=*this/o;return*this;}frint&operator%=(const frint&o){*this=*this%o;return*this;}bool operator<(const frint&o)const{if(ng!=o.ng)return ng;if(ng)return ca(o.v,v);else return ca(v,o.v);}bool operator>(const frint&o)const{return o<*this;}bool operator<=(const frint&o)const{return!(*this>o);}bool operator>=(const frint&o)const{return!(*this<o);}bool operator==(const frint&o)const{return v==o.v&&ng==o.ng;}bool operator!=(const frint&o)const{return!(*this==o);}frint&operator++(){*this=*this+frint(1);return*this;}frint operator++(int){frint t=*this;++(*this);return t;}frint&operator--(){*this=*this-frint(1);return*this;}frint operator--(int){frint t=*this;--(*this);return t;}friend ostream&operator<<(ostream&os,const frint&f){if(f.ng&&f.v!="0")os<<'-';os<<f.v;return os;}friend istream&operator>>(istream&is,frint&f){str s;is>>s;f=frint(s);return is;}str ts()const{if(ng&&v!="0")return"-"+v;return v;}frint ab()const{frint r=*this;r.ng=0;return r;}bool iz()const{return v=="0";}bool ip()const{return!ng&&v!="0";}bool in()const{return ng;}};class Complex{private:frint a,b;public:Complex():a(0),b(0){}Complex(frint _a,frint _b):a(_a),b(_b){}Complex(str s){size_t pos=s.find('+');if(pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos+1);sb.pop_back();a=frint(sa);b=frint(sb);}else{pos=s.find('-');if(pos!=0&&pos!=str::npos){str sa=s.substr(0,pos);str sb=s.substr(pos);sb.pop_back();a=frint(sa);b=frint(sb);}else if(s.back()=='i'){a=0;str sb=s;sb.pop_back();b=frint(sb);}else{a=frint(s);b=0;}}}Complex operator+(const Complex& o)const{return Complex(a+o.a,b+o.b);}Complex operator-(const Complex& o)const{return Complex(a-o.a,b-o.b);}Complex operator*(const Complex& o)const{frint ra=a*o.a-b*o.b;frint rb=a*o.b+b*o.a;return Complex(ra,rb);}Complex operator/(const Complex& o)const{frint c=o.a,d=o.b;frint den=c*c+d*d;if(den.iz())throw runtime_error("Complex division by zero");frint ra=(a*c+b*d)/den;frint rb=(b*c-a*d)/den;return Complex(ra,rb);}Complex conj()const{return Complex(a,-b);}frint mod2()const{return a*a+b*b;}friend ostream& operator<<(ostream& os,const Complex& c){if(c.a.iz()&&c.b.iz()){os<<"0";}else if(c.b.iz()){os<<c.a;}else if(c.a.iz()){if(c.b.ip())os<<c.b<<"i";else os<<c.b<<"i";}else{os<<c.a;if(c.b.ip())os<<"+"<<c.b<<"i";else os<<c.b<<"i";}return os;}friend istream& operator>>(istream& is,Complex& c){str s;is>>s;c=Complex(s);return is;}bool operator==(const Complex& o)const{return a==o.a&&b==o.b;}bool operator!=(const Complex& o)const{return!(*this==o);}frint real()const{return a;}frint imag()const{return b;}Complex operator-()const{return Complex(-a,-b);}Complex& operator+=(const Complex& o){a+=o.a;b+=o.b;return*this;}Complex& operator-=(const Complex& o){a-=o.a;b-=o.b;return*this;}Complex& operator*=(const Complex& o){*this=*this*o;return*this;}Complex& operator/=(const Complex& o){*this=*this/o;return*this;}};signed main(){return 0;}```:::