码迷,mamicode.com
首页 > 其他好文 > 详细

大数板子

时间:2020-05-31 15:45:43      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:printf   else   class   har   color   art   closed   pac   cli   

技术图片
#include<bits/stdc++.h>  
using namespace std;  
struct BigInteger  
{  
    static const int BASE=10000;  
    static const int WIDTH=4;  
    vector<long long>s;  
      
    BigInteger(long long num=0) { *this=num; }
    BigInteger operator = (long long num) 
    {  
        s.clear();  
        do  
        {  
            s.push_back(num%BASE);  
            num/=BASE;  
        }while(num>0);  
        return *this;  
    }  
    BigInteger operator = (const string& str)
    {  
        s.clear();  
        int x,len=(str.length()-1)/WIDTH+1;  
        for(int i=0;i<len;i++)  
        {  
            int end=str.length()-i*WIDTH;  
            int start=max(0,end-WIDTH);  
            sscanf(str.substr(start,end-start).c_str(),"%d",&x);  
            s.push_back(x);  
        }  
        return *this;  
    }  
          
    BigInteger operator + (const BigInteger& b) const
    {  
        BigInteger c;  
        c.s.clear();  
        for(int i=0,g=0;;i++)  
        {  
            if(g==0&&i>=s.size()&&i>=b.s.size()) break;  
            int x=g;  
            if(i<s.size()) x+=s[i];  
            if(i<b.s.size()) x+=b.s[i];  
            c.s.push_back(x%BASE);  
            g=x/BASE;  
        }  
        return c;  
    }  
      
    BigInteger operator - (const BigInteger& b) const
    {  
        BigInteger c;  
        c.s.clear();  
        for(int i=0,g=0;;i++)  
        {  
            if(g==0&&i>=s.size()&&i>=b.s.size()) break;  
            int x=g;g=0;  
            if(i<s.size()) x+=s[i];  
            if(i<b.s.size()) x-=b.s[i];  
            if(x<0)    
            {  
                x+=BASE;g=-1;  
            }  
            if(x!=0) c.s.push_back(x);  
        }  
        return c;  
    }  
      
    BigInteger operator * (const BigInteger& b) const
    {  
        BigInteger c;  
        c.s.clear();  
        int lena=s.size(),lenb=b.s.size(),lenc=lena+lenb;  
        for(int i=0;i<lenc;i++)  
        {  
            c.s.push_back(0);  
        }  
        for(int i=0;i<lena;i++)  
        {  
            for(int j=0;j<lenb;j++)  
            {  
                c.s[i+j]+=s[i]*b.s[j];  
            }  
        }  
        for(int i=0;i<lenc-1;i++)    
        {    
            c.s[i+1]+=c.s[i]/BASE;  
            c.s[i]%=BASE;  
        }  
          
        for(int i=lenc-1;c.s[i]==0;i--)  
        {  
            c.s.pop_back();  
        }  
        return c;  
    }  
      
    BigInteger operator / (const long long& b) const
    {  
        BigInteger c;  
        c.s.clear();  
        for(int i=0;i<s.size();i++)  
        {  
            c.s.push_back(0);  
        }  
        long long g=0;  
        for(int i=s.size()-1;i>=0;i--)    
        {     
            c.s[i]=(s[i]+g*BASE)/b;     
            g=s[i]+g*BASE-c.s[i]*b;     
        }  
        for(int i=s.size()-1;c.s[i]==0;i--)  
        {  
            c.s.pop_back();  
        }  
        return c;  
    }  
      
    BigInteger operator % (const long long& b) const
    {  
        long long ans=0,lena=s.size();  
        for(int i=lena-1;i>=0;i--)  
        {  
            ans=(ans*BASE+s[i])%b;  
        }  
        return ans;  
    }  
    /*BigInteger operator / (const BigInteger& b)   const// /运算符 (大数除大数) 
    { 
         
    } 
    BigInteger operator % (const BigInteger& b) const// %运算符 (大数取模大数) 
    { 
         
    }*/  
      
  
    BigInteger operator += (const BigInteger& b)
    {  
        *this=*this+b;return *this;  
    }  
    BigInteger operator -= (const BigInteger& b)
    {  
        *this=*this-b;return *this;  
    }  
    BigInteger operator *= (const BigInteger& b) 
    {  
        *this=*this*b;return *this;  
    }  
    BigInteger operator /= (const long long& b)
    {  
        *this=*this/b;return *this;  
    }  
    BigInteger operator %= (const long long& b) 
    {  
        *this=*this%b;return *this;  
    }  
    /*BigInteger operator /= (const BigInteger& b)
    { 
        *this=*this/b;return *this; 
    } 
    BigInteger operator %= (const BigInteger& b)
    { 
        *this=*this%b;return *this; 
    }*/  
      
    bool operator < (const BigInteger& b) const
    {  
        if(s.size()!=b.s.size()) return s.size()<b.s.size();  
        for(int i=s.size()-1;i>=0;i--)  
            if(s[i]!=b.s[i]) return s[i]<b.s[i];  
        return false;  
    }  
    bool operator > (const BigInteger& b) const { return b<*this; } 
    bool operator <= (const BigInteger& b) const { return !(b<*this); }
    bool operator >= (const BigInteger& b) const { return !(*this<b); }  
    bool operator != (const BigInteger& b) const { return b<*this||*this<b; } 
    bool operator == (const BigInteger& b) const { return !(b<*this)&&!(*this<b); }
};  
int tp;  
ostream& operator << (ostream &out,const BigInteger &x)
{  
    out<<x.s.back();  
    for(int i= x.s.size()-2;i>=0;i--)  
    {  
        char buf[20];  
        sprintf(buf,"%04d",x.s[i]);  
        for(int j=0;j<strlen(buf);j++) out<<buf[j];  
    }  
    return out;  
}  
istream& operator >> (istream &in, BigInteger &x)
{  
    string s;  
    if(!(in >> s)) return in;  
    x=s;  
    return in;  
}  
int main()  
{
    int t;
    cin>>t;
    while(t--)
    {
        BigInteger a;
        cin>>a;
        if(a==1)
            cout<<"0"<<endl;
        else
            cout<<a*(a-1)/2<<endl;
    }
    return 0;  
}
View Code

 

大数板子

标签:printf   else   class   har   color   art   closed   pac   cli   

原文地址:https://www.cnblogs.com/lipu123/p/13018918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!