Logo xuanxuan0604的博客

博客

新博客

2026-03-21 11:25:41 By xuanxuan0604

我发现在数据统计前缀和后缀和标反了

Complex(复数类)(半成品)

2026-01-01 11:51:50 By xuanxuan0604

代码只给展示这么长,分上下集,就将就着看吧

#include<iostream>
#include<cmath>
using namespace std;

/*
template<class T> class complex;
template<> class complex<float>;
template<> class complex<double>;
template<> class complex<long double>;
*/

/*
long double sqrt_long_double(const long double& n){
    long double x=n;
    long double px=0;
    while(px!=x){
        px=x;
        x=(x+n/x)/2.0;
    }
    return x;
}
*/

template<class T> //ONLY: float,double,long double
class complex{

private:

    T m_arg; //Arg(z)
    T m_abs; //sqrt(Re(z)^2+Im(z)^2)
    T m_real; //Re(z)
    T m_imag; //Im(z)

public:

//    const T e=2.718281828459045235;
//    const T pi=3.141592653589793438;
//    const T INF=1.7976931348e+150;

    complex(long double a=0.0,long double b=0.0,bool typ=true){ //true=real+imag,false=arg+abs
        if(typ){
            this->m_real=a;
            this->m_imag=b;
            if(a==0){
                if(b>0){
                    this->m_arg=3.141592653589793438/2;
                }
                else if(b==0){
                    this->m_arg=0;
                }
                else{
                    this->m_arg=-3.141592653589793438/2;
                }
            }
            else{
                this->m_arg=atan(b/a);
                if(a<0&&b>=0){
                    this->m_arg=this->m_arg+3.141592653589793438;
                }
                else if(a<0&&b<0){
                    this->m_arg=this->m_arg-3.141592653589793438;
                }
            }
            this->m_abs=sqrt(a*a+b*b);
        }
        else{
            this->m_arg=a;
            this->m_abs=b;
            this->m_real=b*cos(a);
            this->m_imag=b*sin(a);
        }
    }

    complex(const complex<T>& x){
        this->m_abs=x.m_abs;
        this->m_arg=x.m_arg;
        this->m_imag=x.m_imag;
        this->m_real=x.m_real;
    }

    ~complex(){

    }

    T real(){
        return this->m_real;
    }

    T imag(){
        return this->m_imag;
    }

    T arg(){
        return this->m_arg;
    }

    T abs(){
        return this->m_abs;
    }

    T norm(){
        return this->m_real*this->m_real+this->m_imag*this->m_imag;
    }

    complex conj(){
        return complex(m_real,-m_imag);
    }

    const complex operator+(const complex& t){
        double a=this->m_real;
        double b=this->m_imag;
        double c=t.m_real;
        double d=t.m_imag;
        complex res(a+c,b+d);
        return res;
    }

    const complex operator-(const complex& t){
        double a=this->m_real;
        double b=this->m_imag;
        double c=t.m_real;
        double d=t.m_imag;
        complex res(a-c,b-d);
        return res;
    }

   const complex operator*(const complex& t){
        double a=this->m_real;
        double b=this->m_imag;
        double c=t.m_real;
        double d=t.m_imag;
        complex res(a*c-b*d,a*d+b*c);
        return res;
    }

    const complex operator/(const complex& t){
        double a=this->m_real;
        double b=this->m_imag;
        double c=t.m_real;
        double d=t.m_imag;
        if(c==0&&d==0){
            throw runtime_error("0 CANNOT BE USED AS A DIVISOR!");
        }
        else{
            complex res((a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d));
            return res;
        }
    }

   bool operator==(const complex& t){
        return this->m_real==t.m_real&&this->m_imag==t.m_imag;
    }

    bool operator<(const complex& t){
        if(this->m_imag!=0||t.m_imag!=0){
            throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
        }
        else{
            return this->m_real<t.m_real;
        }
    }

    bool operator>(const complex& t){
        if(this->m_imag!=0||t.m_imag!=0){
            throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
        }
        else{
            return this->m_real>t.m_real;
        }
    }

   bool operator<=(const complex& t){
        if(this->m_imag!=0||t.m_imag!=0){
            throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
        }
        else{
            return this->m_real<=t.m_real;
        }
    }

    bool operator>=(const complex& t){
        if(this->m_imag!=0||t.m_imag!=0){
            throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
        }
        else{
            return this->m_real>=t.m_real;
        }
    }    
    friend istream& operator>>(istream& in,complex& z){
        in>>z.m_real>>z.m_imag;
        return in;
    }

    friend ostream& operator<<(ostream& out,const complex& z){
        if(z.m_real!=0.0){
            out<<to_string(z.m_real);
            if(z.m_imag>0.0){
                out<<"+";
            }
        }
        if(z.m_imag!=0.0&&z.m_imag!=-0.0&&z.m_imag!=-1.0&&z.m_imag!=1.0){
            out<<to_string(z.m_imag)<<"i";
        }
        if(z.m_imag==1.0){
            out<<"i";
        }
        if(z.m_imag==-1.0){
            out<<"-i";
        }
        if(z.m_real==0.0&&z.m_imag==0.0){
            out<<z.m_real;
        }
        return out;
    }

};

template<class T> //ONLY: float,double,long double
const complex<T> type_i=complex<T>(0.0,1.0);

template<class T> //ONLY: float,double,long double
T Re(complex<T> z){
    return z.real();
}

template<class T> //ONLY: float,double,long double
T Im(complex<T> z){
    return z.imag();
}

template<class T> //ONLY: float,double,long double
T abs(complex<T> z){
    return z.abs();
}

template<class T> //ONLY: float,double,long double
T Arg(complex<T> z){
    return z.arg();
}

template<class T> //ONLY: float,double,long double
//sqrt(z) has 2 solutions,this function shows the first one(+,+),but another one(-,-) does not show
complex<T> sqrt(complex<T> z){ // come from: Euler's formula.
    T r=z.real();
    T i=z.imag();
    if(r==0.0){
        T t=sqrt(0.5*abs(i));
        return complex<T>(t,i<0?-t:t);
    }
    else if(i!=0.0){
        T t=2*z.abs(); // Thank https://       www.bilibili.com/opus/810197348175052823/ gives me the proof
        return complex<T>(sqrt(t+2*r)/2,sqrt(t-2*r)*abs(i)/2*i);
    }
    else{
        if(r>=0.0){
            return complex<T>(sqrt(r),0.0);
        }
        else{
            return complex<T>(0.0,sqrt(-r));
        }
    }
}

新博客

2025-12-20 11:10:38 By xuanxuan0604

class long_int{

private:

size_t m_length;
int m_num[10005];

public:

long_int(){
    memset(m_num,0,sizeof(m_num));
    m_length=1;
}

long_int(size_t x,bool flag){
    if(flag){
        memset(m_num,0,sizeof(m_num));
        m_length=x;
    }
    else{
        string str;
        while(x){
            str+=(x%10)+'0';
            x/=10;
        }
        for(int i=0;i<str.length();i++){
            m_num[i]=str[i]-'0';
        }
        m_length=str.length();
    }
}

void Measure_size(){
    for(int i=0;i<10005;i++){
        if(m_num[i]){
            m_length=i;
        }
    }
    m_length++;
}

void print_sys(){
    for(int i=0;i<m_length;i++){
        cout<<m_num[i];
    }
    cout<<endl;
}

size_t size(){
    return m_length;
}

size_t length(){
    return m_length;
}

bool operator<(long_int b){
    for(int i=max(m_length,b.length());i>-1;i--){
        if(m_num[i]>b[i]){
            return false;
        }
        if(m_num[i]<b[i]){
            return true;
        }
    }
    return false;
}

int operator[](int __index__){
    return m_num[__index__];
}

long_int operator*(long_int b){
    long_int res(m_length+b.length(),1);
    for(int i=0;i<m_length+b.length()-1;i++){
        for(int j=0;j<=i;j++){
            res.m_num[i]+=m_num[j]*b[i-j];
        }
        res.m_num[i+1]+=res.m_num[i]/10;
        res.m_num[i]%=10;
    }
    res.Measure_size();
    return res;
}

void operator=(int x){
    string str;
    while(x){
        str+=(x%10)+'0';
        x/=10;
    }
    for(int i=0;i<str.length();i++){
        m_num[i]=str[i]-'0';
    }
    m_length=str.length();
}

void operator=(string str){
    for(int i=0;i<str.length();i++){
        m_num[i]=str[str.length()-i-1]-'0';
    }
    m_length=str.length();
}

friend ostream& operator<<(ostream& out_put__,long_int& out_num){
    for(int i=out_num.length()-1;i>-1;i--){
        out_put__<<out_num[i];
    }
    return out_put__;
}

};

long_int max_(long_int a,long_int b){ return a<b?b:a; }

730题庆!

2025-11-22 11:20:57 By xuanxuan0604

好久没发过题庆了

欧拉筛

2024-08-08 09:56:51 By xuanxuan0604
void prime(ll maxx){
    am[0]=am[1]=1;
    for(int i=2;i<=maxx;i++){
        if(!am[i]){
            p[len++]=i;
        }
        for(int j=0;j<len;j++){
            if(p[j]*i<=maxx){
                am[p[j]*i]=1;
            }
            else{
                break;
            }
            if(i%p[j]==0){
                break;
            }
        }
    }
}

(am=isprime,p=prime,len=p的长度)

xuanxuan0604 Avatar