C++plus

· · 科技·工程

前言

起因:这些东西好麻烦。

中间忘了,然后就有了它。

欢迎私信反馈qwq

正文

编译选项

若无特别说明,下文采用的编译选项均为

-std=c++11 -O2

:::warning[编译选项设置]{open}

以 Dev-C++ 编译器为例,点击“工具-编译选项”打开窗口。

其他默认选项不要动,把编译器和编译器配置修改为如图所示。

不执行此操作可能会导致编译错误。

:::

开始

导入必须的库。

:::info[导入库]

#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]

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;
    }
};

:::

理论上来说,只要你的内存足够,你可以使用 frint 类型存下无穷位的数据。

::::info{open}

如果你不喜欢打 frint,请使用宏定义。

例如,你想要用 fr 代替这个类型:

#define fr frint

:::warning{open}

如果你写了

#define int frint

请把主函数改为

signed main(){

    return 0;
}

:::

::::

复数实现

因为乘法使用 FFT,干脆就把复数实现了。

:::info[复数实现]

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;
    }
};

:::

数据类型名称替换

很简单的宏定义就可以做到。

:::info[数据类型名称替换]{open}

#define str string
#define float double

请不要在程序中删除它。

:::

测试用

此部分与正文无关,仅用于笔者测试相关功能。

:::info[测试]

//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}

#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;
}

:::